Windows Application Enhancements

Visual Studio .NET has added a variety of features to VB .NET to make the IDE more powerful and to enhance the functionality of Windows forms. There is improved support for features such as building menus, controls that automatically resize based on 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 from VB to VB .NET that might 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 2.13. Now, click Button1 and notice that the text in the label changed, which is no big surprise. How ever, 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 2.14 shows this to be the case.

Figure 2.13. A form with a small amount of text in a label.


Figure 2.14. The same form, showing 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 this 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 Multiline property to True and resize the text box 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 2.15.

Figure 2.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 will end up with something that looks like Figure 2.16.

Figure 2.16. The form at run time, showing what happens when the form is resized.


Obviously, this looks pretty strange, and it is exactly what you are used to with VB6. Your answer in VB6, short of a third-party ActiveX control, is to write a lot of code that runs when the form's Resize event is fired. You use that code to move the button and resize the text box. 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, whereas 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 2.17. After you collapse the drop-down list, the Anchor property should be Bottom, Right.

Figure 2.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 text box is tied to all borders, and it will remain the same distance from them. The Anchor property for TextBox1 should now be Top, Bottom, Left, Right.

Run the project again. After the form is open, resize it. Notice that the button now stays in the lower-right corner, and the text box automatically resizes with the form. You can see this behavior in Figure 2.18. The resizing and movement were accomplished without writing a line of code.

Figure 2.18. The form showing how the controls can resize or move as the form is resized.


Easier Menus

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 nor for user friendliness. The new Menu Editor in VB .NET might just make you enjoy building menus.

Using the same form as in the previous example, go to the Toolbox and double-click the MainMenu control. The MainMenu control is added to an area below the form, called the Component Tray, which you'll see more of later. If you click once on the MainMenu1 control in the Component Tray, you'll see a menu bar added to the form, which simply pushes down the text box. The menu shows one item that says Type Here, as shown in Figure 2.19.

Figure 2.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 for the Shortcut property. Scroll through the list until you find the Ctrl+O 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 2.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 2.20. The new menu being displayed at run time.


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 2.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 2.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 2.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 2.22. The new way to set the tab order.


You can choose View, Tab Order again 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. The Line and Shape controls are gone, but you do have an alternative.

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. The GDI+ objects are in the System.Drawing namespace, and will be examined briefly in Chapter 3, “Major VB .NET Changes.”

Form Opacity

Forms now have an Opacity property. Is it a necessary feature? Probably not, but it could be used for some useful purpose. For example, you can use it to have forms fade in or out, and you can turn forms translucent while they are being dragged.

On the Form1 you've been working with so far, find its Opacity property and change it to 50%. 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, in the Component Tray for 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 Add any initialization... comment:

Me.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:

Me.Opacity = Me.Opacity + 0.01
If Me.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%), 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.2 shows how the whole listing will look (with the Windows Form Designer-generated code hidden).

Listing 2.2. Your First VB .NET Application, with All the Code Discussed So Far
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

   Private Sub button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles button1.Click   End Sub

   Private Sub timer1_Tick(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles timer1.Tick
       Me.Opacity = Me.Opacity + 0.01
      If Me.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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.31.50