Export (0) Print
Expand All

Interacting with the User: Using Buttons

In this lesson, you will learn how to add a Button to a form, how to change the appearance of the button, and how to write code that runs when it is clicked.

The easiest way for users to interact with your program is through buttons. For example, many programs have Exit buttons. As you saw in the previous lesson, the Button control in Visual Basic looks and behaves like a push button. The Button control also has predefined events that can be used to initiate actions such as ending a program.

Buttons are, generally, rectangular controls with a raised appearance on the form. There are many properties, however, that can be set to change their appearance. The most obvious is the Text property, which determines the text displayed, and this text is displayed in the font or typeface determined by the Font property. The BackColor property determines the button's color, and the ForeColor property determines the text's color.

When the user clicks a button at run time, the Button raises the Click event. When an event occurs, controls run code in response to those events. You can write code that should run when the user clicks the button by creating an event handler.

An event handler is a method that executes when an event occurs. When a user clicks a button, the button's Click event has an event handler. It is easier than it sounds, and you'll learn how to write an event handler in the following example. Events and event handlers will be covered in more detail in Making Your Program React to the User: Creating an Event Handler.

To use buttons

  1. On the File menu, click New Project.

  2. In the New Project dialog box, in the Templates pane, click Windows Application.

  3. In the Name box, type ButtonExample, and then click OK.

    A new Windows Forms project opens.

  4. From the Toolbox, drag a Button onto the form.

  5. In the Properties window, change the Text property to read: What time is it? and then press ENTER.

    Notice that the text doesn't fit on the button.

  6. In the Properties window, select the AutoSize property and set it to True.

    The button resizes itself to fit the text.

  7. In the form, double-click the button to open the Code Editor.

    The Code Editor opens in the middle of a method called Button1_Click. This is the Button1.Click event handler. The code you write here will execute when the button is clicked.

  8. In the Button1_Click event handler, type the following line of code.

    MsgBox("The current time is " & Now.ToShortTimeString)
  9. Press F5 to run your program.

    The program starts and the form appears. When you click the Button, a message box displaying the current time appears.

In this lesson, you learned how to add a button to a form and how to add code that will execute when the user clicks the button with the mouse. In the next lesson, you will learn how to work with the controls designed for displaying and receiving text: Label and TextBox.

Next Lesson: Displaying and Receiving Text: Using Labels and Text Boxes

© 2014 Microsoft