Windows Application Enhancements
Visual Studio.NET has added a variety of features to VB.NET to make working with projects much richer. There is improved support for features such as building menus, automatically adjusting to changes in the size of the text to display, better anchoring of controls for resized windows, and a much-improved mechanism for setting the tab order.
Resizing Controls Automatically
You can set certain controls to resize automatically based on what they need to display. This is easy to examine with a simple label. Open the Toolbox and add a label to your form. Drag it over to the left side of the form so that it is not in line with the button.
Now change some properties of that label. Change the BorderStyle to FixedSingle, and set AutoSize to True. Change the Text property to "This is a test." You might have noticed that you changed the Text property, and not a Caption property as you did in VB versions 1–6. Welcome to another one of those little changes that will trip you up.
Now modify the code for the Button1_Click event. Remove the msgbox call and add the following code:
label1.Text = "Put your hand on a hot stove " & _ "for a minute, and it seems like an hour. " & _ "Sit with a pretty girl for an hour, and " & _ "it seems like a minute. THAT'S relativity."
Before you run this, realize one more thing about the code. In VB6, it would have been legal to say this:
Label1 = "Put your hand on a hot stove..."
This would run just fine in VB6 because the default property of Label1 was the Caption property. Well, in VB.NET, there is no such thing as a default property. Therefore, you always have to specify the property when you write your code.
Go ahead and run the project. A form will appear that is similar to what you see in Figure 13. Now click Button1 and notice that the text in the label changed, which is no big surprise. However, notice that the label has grown to try to hold all the text. If you resize the form, you will see that the label has indeed grown to show all the text. Figure 14 shows this to be the case.
Figure 13 A form with a small amount of text in a label.
Figure 14 The same form, showing that the label has grown to contain all the text.
If you're saying that this isn't a big deal, think of the code you had to write before. You had to check the length of the string, but you also had to be aware of what font was in use. A group of characters in Arial is not the same length as the same group of characters in Courier. Therefore, resizing could be a hit-or-miss proposition. The AutoSize property removes that guesswork for you.
Anchoring Controls to the Form Edges
How many times did you create a VB form and set the border style to fixed so that people couldn't resize it? If you placed a series of buttons along the bottom of the form, you didn't want people to resize the form and suddenly have the bottom row of buttons in the middle of the form.
VB.NET allows you to anchor controls to one or more sides. This can allow a control to move as the form is resized so that it appears to stay in its proper place.
Back on Form1, delete the label you added, and remove the code inside the Button1_Click event procedure. Make the form slightly bigger and move Button1 toward the lower-right corner. Now add a TextBox to the form. Change the TextBox's Mutliline property to True and resize the textbox so that it fills up most of the form, except for the bottom portion that now contains the button. Your form should now look something like the one shown in Figure 15.
Figure 15 The form as it looks at design time.
Run the project. After the window is loaded, resize it by moving the lower-right corner down and to the right. You should end up with something that looks like Figure 16.
Figure 16 The form at runtime, showing what happens when the form is resized.
Obviously, this looks pretty strange, and it is exactly what you are accustomed to in VB6. Your answer in VB6, short of a third-party ActiveX control, is to write a lot of code that fires when the form is resized. You use that code to move the button and resize the textbox. VB.NET has a better way of handling this, so stop the application and return to the IDE.
Make sure that you are on the Form1.vb [Design] tab, and click on Button1. In the Properties window, scroll down and find the Anchor property. When you drop down this list, you get a strange box that shows a gray "something" in the middle and four small rectangles around it. By default, the top and left rectangles are darkened, while the right and bottom rectangles are empty. The darkened rectangles indicate that, right now, the button is anchored to the top and left sides of the form. As you resize the form, the button will stay the same distance from the top and left sides. That is not what you want, so click the top and left rectangles to clear them, and then click the bottom and right rectangles to select them. When you are done, the Anchor property should look like Figure 17. After you collapse the drop-down list, the Anchor property should be BottomRight.
Figure 17 The Anchor property tool.
Next, click on TextBox1 and choose its Anchor property. Click on the bottom and right rectangles, but leave the left and top rectangles chosen. Now the textbox is tied to all borders, and it will remain the same distance from them. The Anchor property for TextBox1 should now be All.
Run the project again. After the form is open, resize it. Notice that the button now stays in the lower-right corner, and the textbox automatically resizes with the form. You can see this behavior in Figure 18. The resizing and movement were accomplished without writing a line of code.
Figure 18 The form showing how the controls can resize or move as the form is resized.
If you hated building menus under previous versions of VB, you weren't alone. The VB menu editor has never won any awards for ease of use or for user-friendliness. The new menu editor in VB.NET is much improved, however.
Using the same form as in the previous example, go to the Toolbox and double-click the MainMenu control. This adds a menu bar to the form (and simply pushes down the textbox). The menu shows one item that says Type Here, as shown in Figure 19.
Figure 19 The new Menu Editor in action.
Click once in the box that says Type Here, and type &File. This creates a new Type Here box to the right and a new Type Here box below. Click once in the Type Here below the word &File, and type &Open. Just as in previous versions of VB, the ampersand (&) is used to signify an Alt+key selection. Typing &Open creates a new Type Here box to the right and a new Type Here below. As you can see, you are graphically building a menu. Click in the box below the Open and type &Close.
You can click in the Type Here box just to the right of the File menu choice to add another top-level menu. For example, you could add &Edit to the right of the File menu. Now the Edit menu gets new blank entries, and you could add items for copying and pasting.
Click on the Open menu choice under the File menu (on the menu you just created, not VB.NET's menus). If you look in the Properties window, you will see that the actual name of the object is MenuItem2. You can change that, if you want, but don't worry about it for now. Instead, click on the drop-down list for the Shortcut property. Scroll through the list until you find the CtrlO choice. The ShowShortcut property is set to True by default, but you won't see the shortcut at design time. Instead, run the application. Figure 20 shows you roughly what you should see. Notice that the underlining of the letters (signified by the ampersand) might not appear unless you hold down the Alt key.
Figure 20 The new menu being displayed at runtime.
Setting Tab Order
If you disliked creating menus in previous versions of VB, it's a safe bet that you hated setting the tab order, especially on complex forms. You had to click each control, one at a time, and make sure that the TabIndex property was correct. Take the form you've been working with and remove the textbox. Leave Button1 in the lower-right corner.
Add three more buttons to the form. Place them in such a way as to make Button1 the last in the series. In fact, mix them up, if you prefer. If you take a look at Figure 21, you'll see that the buttons have been placed in such a way that you will want the tab order to be Button2, Button4, Button3, and Button1. If you start the project and run it as is, however, the order will still be Button1, Button2, Button3, and Button4.
Figure 21 The form with buttons in the wrong order.
To set the tab order, click on the View menu and choose Tab Order. You now get small numbers on each control that can receive the focus, as shown in Figure 22. To reset the tab order, simply click on the controls in the order that you want them to receive focus. In this example, you just click on Button2, Button4, Button3, and Button1, in that order, and the tab order is set for you, as evidenced by the number in the upper-left corner of each button.
Figure 22 The new way to set the tab order.
You can choose View, Tab Order to turn off the display of the tab order. You can run the project to verify the new tab order.
Line and Shape Controls: You're Outta Here
If you examine the Toolbox, you might just find yourself searching and searching for the Line and Shape controls. They're gone. Yes, no more line and shape controls.
Microsoft removed them because they were actually windowless controls in VB6; that is, they didn't have an associated hWnd, but were painted directly on the form. In the new VB.NET forms engine, all controls must be windowed, and you no longer have control transparency.
Now that you've heard the technical explanation, what do you do about it? The easiest way to fake a line is to use a label. Turn on the border and then set the height (or width) to 1. This makes the label look like a line, and it works fine. For more complex shapes, Microsoft recommends that you use the GDI+ objects, which are very powerful. This is not something that will be covered in this book, but be aware of the change and what you need to know to get around it.
Forms now have an Opacity property. Is it a necessary feature? Probably not, but it could be used for some useful purpose. Probably. Somewhere. At least while you're working with it, you'll look busy.
On the Form1 you've been working with so far, find its Opacity property and change it to 50 percent. Now run the project and you'll notice that the form is now quite see-through. The form still works fine (not that this current form is doing anything). You can click and drag the form;, you can press the buttons. It works fine, but it is translucent at the moment. Close the project, and you can add some code to take advantage of this new, if not quite critical, feature.
Go to the Toolbox and add a timer to your form. Unlike VB6, the timer doesn't actually appear on the form, but it appears in a small window below the form. Click on the timer in this window and then set the timer's Enabled property to True. The interval of 100 is fine.
Now click on the Form1.vb tab to get to the code. In the Sub New routine, add the following line of code right after the TODO comment:
Form1.Opacity = 0
This merely sets the opacity to 0, which means that the form will be completely invisible when the form loads.
Next, use the Class Name and Method Name drop-down boxes at the top of the code window to choose the Tick event of the Timer1 control. This adds an event procedure for the Timer1_Tick event. Inside that event procedure, enter the following code:
Form1.Opacity = form1.Opacity + 0.01 If form1.Opacity >= 1 Then timer1.Enabled = False beep() End If
This code merely increments the opacity by .01 each time the Tick event fires. When it finally reaches 1 (100 percent), the timer is shut off by setting its Enabled property to false, and the beep is called just to make a sound so that you know when it is finished.
If you are at all confused by the code, Listing 2 shows how the whole listing will look (with the Windows Form Designer generated code hidden).
Listing 2: Your First VB.NET Application, with All the Code Discussed So Far
Imports System.ComponentModel Imports System.Drawing Imports System.WinForms Public Class Form1 Inherits System.WinForms.Form Public Sub New() MyBase.New Form1 = Me 'This call is required by the Win Form Designer. InitializeComponent 'TODO: Add any initialization after the InitializeComponent() call Form1.Opacity = 0 End Sub 'Form overrides dispose to clean up the component list. Overrides Public Sub Dispose() MyBase.Dispose components.Dispose End Sub ' Windows Form Designer generated code Public Sub Timer1_Tick(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Timer1.Tick form1.Opacity = form1.Opacity + 0.01 If form1.Opacity >= 1 Then timer1.Enabled = False beep() End If End Sub End Class
Run the project. You should see your form fade in slowly as the opacity is increased. You will hear a beep when the form is fully opaque.