» Home
» Tutorials
» Search  
» Contact us 

Home » Tutorials » Introduction to Visual Basic .NET » Chapter 6

Chapter 6: Building Windows Applications
6.1: Responding to Events
6.1.1: Setting Up a Button Event

Setting Up a Button Event

A good way to illustrate the event philosophy is to wire up a button to an event. An example would be the Click event, which is fired whenever the button is clicked. You have more events than just the Click event, although in day-to-day practice it’s unlikely you’ll use more than a handful of these. Even though you’ve already seen the Click event in action, this Exercise will go into some of the details of Code Editor and some more Button events that you have not seen up until this point.

 

Exercise - Using Button Events

1. Start Visual Studio 2005 and select File * New * Project from the menu. In the New Project dialog box, select Visual Basic as the Project Type and Windows Application as the Templates type. Enter a project name of Hello World 2 in the Name field and then click the OK button.

2. Click the form, and then, in the Properties window, change the Text property from Form1 to Hello, world! 2.0.

3. From the Toolbox, drag a Button control onto the form. Change its Text property to Hello, world! and its Name property to btnSayHello. Resize your button so that it looks similar to the one shown below:

4. Double-click the button and add the following highlighted code to the Click event handler:

Private Sub btnSayHello_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnSayHello.Click
‘Display a MessageBox
MessageBox.Show(“Hello, world!”, Me.Text)
End Sub

5. Drop down the list in the Class Name combo box at the top of the code window. You’ll see the options shown in below figure. Select Form1 in the Class Name combo box.

Notice that the last two items in the list are slightly indented. This tells you that (Form1 Events) and btnSayHello are all related to Form1. That is, the btnSayHello class is a member of Form1. As you add more members to the form, they will appear in this list.

6. Before you continue, take a quick look at the list in the Method Name combo box to the right of the Class Name combo box. Drop it down, and you’ll see the options shown in below figure. These options are described in the list that follows the figure.

  • The contents of the Method Name combo box change depending on the item selected in the Class Name combo box. This list lets you navigate through the methods related to the class you select in the Class Name combo box. In this case, its main job is to show you the methods and properties related to the class.

  • The (Declarations) entry takes you to the top of the class where you can change the definition of the class and add member variables.

  • The New method will create a new constructor for the class that you are working with. The constructor should contain any initialization code that needs to be executed for the class.

  • The Finalize method will create a new method called Finalize and add it to the class and will be called when your program ends to release any unmanaged resources.

  • The Dispose method takes you to the Dispose method for the class that you are working with and allows you to add any addition clean up code for your class.

  • The InitializeComponent method takes you to the code that initializes the controls for the class that you are working with. You should not modify this method directly. Instead, you should use the Form Designer to modify the properties of the controls on your form.

  • You’ll notice that Visual Basic .NET adds a small icon to the left of everything it displays in these lists. These can tell you what the item in the list actually is. A small purple box represents a method, a small blue box represents a member, four books stacked together represent a library, three squares joined together with lines represent a class, and a yellow lightning bolt represents an event.

 

7. Select btnSayHello in the Class Name combo box. Now, drop down the Method Name combo box, as shown:

Since you selected btnSayHello in the Class Name combo box, the Method Name combo box now contains items that are exclusively rated to that control. In this case, you have a huge list of events. One of those events, Click, is shown in bold because you provided a definition for that event. If you select Click, you’ll be taken to the method in Form1 that provides an event handler for this method.

8. Now add another event handler to the Button control. With btnSayHello still selected in the Class Name combo box, select the MouseEnter event in the Method Name combo box. A new event handler method will be created, and you need to add the following code to it as highlighted:

Private Sub btnSayHello_MouseEnter(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles btnSayHello.MouseEnter
‘Change the Button text
btnSayHello.Text = “The mouse is here!”
End Sub

The MouseEnter event will be fired whenever the mouse pointer “enters” the control, in other words, crosses its boundary.

9. To complete this exercise, you need to add another event handler. Select btnSayHello in the Class Name combo box and select the MouseLeave event in the Method Name combo box. Again, a new event will be created, so add the highlighted code here:

Private Sub btnSayHello_MouseLeave(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles btnSayHello.MouseLeave
‘Change the Button text
btnSayHello.Text = “The mouse has gone!”
End Sub

The MouseLeave event will be fired whenever the mouse pointer moves back outside of the control.

10. Run the project. Move the mouse over and away from the control, and you’ll see the text change, as shown:

 

click next Page - How It Works.

Home | Link to Us | Partner Links | About us | Contact us

Copyright © 2009-2012 F1tutorials.com | All Rights Reserved