1. Home
  2. Computing & Technology
  3. Visual Basic

Visual Basic .NET for Beginners
Using a Visual Basic control

By Dan Mabbutt, About.com

Jul 4 2009

The MonthCalendar control is a fairly complex piece of work, but there are actually controls that are even more complex in the Toolbox. We'll use this control to learn about another part of VB.NET: the Properties window.

The individual windows, such as Properties, can be resized and moved around to make them easier to work with. I dragged the Properties window out into the middle of VB.NET in the illustration below. Some of the Properties for the MonthCalendar control are shown, but there are a lot more that you can scroll down to see.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The Properties, Methods, and Events are what gives a control the power to do what you want done. Properties include the information about the control, like the Name. Methods are what the control can do, like ShowToday. Events make parts of your program start running, like Keypress. We'll go over Methods and Events later.

Read through the Properties for the MonthCalendar control. (The Design window and the MonthCalendar control must be selected first.) You'll see that there is a lot of stuff there. But we don't have to change any of it to complete our program. You might notice that some of the properties, like Location, change automatically as you design the form (move and resize the controls). You can change them in the Properties window and it will change in the Design Window automatically too.

Here are all the properties that you should change for the rest of the controls. You can change the value of the property by clicking in the second column of the properties window beside the property that you want to change. The RadioButton1 Checked property is changed to True to make that the default.

Form
Name: SkipDays
Text: Skip Days

Button
Name: skipAhead
Text: Skip Ahead

GroupBox
Name: daysToSkip
Text: Days To Skip

RadioButton1
Name: skip30
Text: 30 Days
Checked: True

RadioButton2
Name: skip90
Text: 90 Days

RadioButton3
Name: skip180
Text: 180 Days

Those are the only properties you have to change! Most properties can be changed at "runtime" (when your program is running) too. For example, you can change the Text property at runtime.

skip90.Text = "Skip Town"

Both Methods (explained later) and Properties are identified in program code using the "dot" notation. This statement changes the Text property for the control named skip90.

Understanding "event oriented" programming

Most programs today are "event oriented". This means that after the program starts running, it normally just waits around for something to happen that it "knows" how to respond to. For example, clicking a button. A program "knows" how to respond to an event when there is an "event subroutine" for that event in VB.NET.

A subroutine is one of the fundamental units of program code in a VB.NET program. Here's a Click event subroutine for a Button control named Button1:

Private Sub Button1_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles Button1.Click
   ' ... program code goes here ...
End Sub

The underscore characters at the ends of the lines are "continuation characters". They allow you to break one long statement into several shorter lines. I have to use them here because space is limited on the web page.

All of the code above is entered for you by VB.NET. All you have to do is double click on the control in the Design window and the Code window will open automatically with the default event for that control already coded. (Click is the default event for a Button control.) The automatically entered code can be very useful (other articles here explain how to use it) but when you're just getting started, you can ignore it all and just enter your program code.

You can see all of the available events for a control in the Code window. Let's look at the events you can use for a Button control. This isn't necessary to write the rest of the program, but it's a good time to pause and see what's available.

  1. Click the View Code icon in the Solution Explorer toolbar.
    (It's just to the left of the View Designer icon that was shown in a previous illustration.)

    There are two "dropdown textboxes" above the code window. The one on the left is called the "Class Name" box and the one on the right is the "Method Name" box.
  2. Click the down arrow on the right side of the Class Name box to display the selection list and then select skipAhead (the name we gave the Button control) in the dropdown that pops open.
  3. Click the down arrow on the right side of the "Method Name" box. A long list of events will pop up. In general, you can write your own code for any of these events and make your program do just about whatever you can think of.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

If you then click any of the events in the Method Name box, VB.NET will automatically enter all the event subroutine code you need for that event into the code window too. This is an alternative to double-clicking the control in design view as described a little earlier. Double-clicking the control in design view will only work for the default event.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Learn VS.NET
  6. Beginning Tutorial to Learn Visual Basic .NET

©2009 About.com, a part of The New York Times Company.

All rights reserved.