Home > Articles > Programming > Windows Programming

Like this article? We recommend

Like this article? We recommend

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.

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

Form Opacity

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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020