Windows Forms with VB using Visual Studio 2010

About this Tutorial –

Objectives –

Delegates will learn to develop web applications using VB 4.0. After completing this course, delegates will be able to:

  • Use Visual Studio 2010 effectively
  • Windows Forms with VB using Visual Studio 2010

Audience

This course has been designed primarily for programmers new to the .Net development platform. Delegates experience solely in Windows application development or earlier versions of ASP.Net will also find the content beneficial.

Prerequisites

Before attending this workshop, students must:

  • Be able to manage a solution environment using the Visual Studio 2010 IDE and tools
  • Be able to program an application using a .NET Framework 4.0 compliant language

Contents

Copyright 20/12/12 – David Ringsell

Download Solutions

Java tutorial

Lab 1. Building Applications with Windows Forms

Exercise 1

Creating the Customer Form
In this exercise, you will create a form that will retrieve customer information. You will add a MenuStrip and other controls to this form.

  1. Create the Windows Application project
    • Start Visual Studio 2010.
    • Select the File menu, then New, then Project.
    • Make sure (if you are using the Professional Version) that Visual C# and then Windows is selected at the left of the screen under Installed Templates.
    • Name the project and the solution: Customers.
    • Browse to the place on your computer where you wish Visual Studio to create the directory for your solution (keeping the tick box selected).
    • Select Windows Forms Application.
  2. Add a Windows Form to the project called frmCustomers:
    • Right-click on the project name: Customers.
    • Select Add.
    • Select New Item.
    • Select Windows form.
  3. Add the list box and text boxes to the form as shown above.
  4. Set the Anchor properties of the text box controls.
  5. Test the project confirm that all controls are anchored correctly.
  6. Using the Toolbox, add a MenuStrip control.
  7. Add a File and a Help menu to the MenuStrip control.
  8. Add the following sub-menus:
    • File: Load Customers, Exit.
    • Help: About.

Exercise 2

Enhancing the Customer Form
In this exercise, you will enhance the customer form by adding a ToolStrip and a ToolTip control. You will also add functionality to the MenuStrip and ToolStrip.

  1. Using the Toolbox, add a ToolStrip control.
  2. Add 3 buttons to the ToolStrip.
  3. Set the Text and the ToolTipText properties for the buttons to:
    • Load Customers
    • Exit
    • Help
  4. Add an About box to the project by right clicking the project name in Solution Explorer.
  5. Add event handlers for the menu items. For example:
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System. _
    Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    Me.Close()
    End Sub
  6. Add event handlers for the tool strip button. For example:
    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles ToolStripButton2.Click
    ‘The Exit toolstrip button calls the Exit menu event handler
    ExitToolStripMenuItem_Click(sender, e)
    End Sub
  7. Add ToolTip user assistance by dragging a ToolTip control on to the form designer, then setting the list box’s ToolTip property.

Exercise 3

Explore further Windows Forms features
In this exercise, you will enhance the customers form to open a second form. You will add container and provider controls to this second form.

    1. Add another form to the project called frmCustomerDetails and open this form from the customers form:
      • using the ShowDialog method (modal).
      • as an owned form (non-modal).
    2. You can do this by double-clicking the button and adding this code to the event handler:
'Show a second form
Dim f As New frmCustomerDetails
'Show as a dialog box (modal)
f.ShowDialog()
'Show as a owned form (non-modal)
'Me.AddOwnedForm(f)
'f.Show()

By adding the apostrophe to the beginning of the sentence, the following text in the sentence is not treated as code by the compiler; it is commented out. For it to compile and execute as code, just remove the apostrophe characters from the sentence. By commented out code you can choose which code you want to have activated in your program.

  • Add some container controls to the new form, for example:
  • SplitContainer
  • TableLayoutPanel
  • FlowLayoutPanel

 

  • Populate the container controls with further controls.
  • Add an ErrorProvider and HelpProvider control to the form.
  • Configure the ErrorProvider and HelpProvider control to show appropriate messages for associated controls.
  • View code file.

 

Lab 2. Customising Windows Forms and Controls

Exercise 1

Creating a custom text box that only shows characters in upper case
In this exercise you create a custom text box by inheriting from the text box class. The new text box will convert characters to upper case by overriding the OnTextChanged method.

    1. Create a new Windows Class Library project UpperText.
    2. Add a reference to the System.Windows.Forms namespace:
      • Select the Project Menu or right-click the solution name.
      • Then select Add Reference.
      • Make sure the .NET tab is selected and search for the above namespace.
      • Select it and then click ok.
      • Type Imports System.Windows.Forms into the beginning of the Class1.vb file.
    3. Rename the Class1.vb file to UpperText.vb.
    4. In the class declaration include an inherits statement:

Public Class UpperTextBox Inherits TextBox.

    1. At the top of the UpperText.cs file import the forms namespace:

Imports System.Windows.Forms;

    1. Override the base textbox’s OnTextChanged() method with:

Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
Dim sel As Integer = SelectionStart
Text = Text.ToUpper
SelectionStart = sel
MyBase.OnTextChanged(e)
End Sub

  1. Build the project.
  2. View code file.

Exercise 2

Creating a Windows Application that tests the custom text box on a form
In this exercise you will test the custom text box from a new Windows Application project.

  1. Add a Windows Applications project to the solution and call it ControlTester.
  2. Set this project as the start-up project.
  3. Drag the upper text box from the toolbox to the form designer.
  4. Test the control by running the project and typing text in lower case in to the text box.
  5. Create a new Windows Applications project in a separate .Net solution.
  6. Add the custom text box to the designer toolbox by right clicking, then selecting Choose Items … from the context menu.
  7. Browse to the .dll file for the custom text box.
  8. Drag the custom text box on to the form and again test the project.

Exercise 3

Creating a user control that shows product information
In this exercise you create a user control. The control contains three buttons; each will display different product information.

  1. Create a new Windows Control Library project called ControlTester.
  2. Rename the UserControl1.vb file to ProductDetails.vb.
  3. Drag three button from the toolbox on to the user controls designer.
  4. Add this code to the each of the button’s event handlers (the 1st statement goes in the 1st event handler, etc.):
    MessageBox.Show("The company name is PCTalk")
    MessageBox.Show("The product name is customers application")
    MessageBox.Show("The product version is 1.0.0.0")
  5. Run the project. The user control is shown in a TestContainer dialog box.
  6. View code file.

Exercise 4

Creating a Windows Application that tests the user control on a form
In this exercise you will test the user control from a new Windows Application project.

  1. Add a Windows Forms Applications project to the solution and call it Controls. This is achieved by:
    • Right-clicking the solution name: ControlTester.
    • Select Add.
    • Select New Project.
  2. Set this project as the start-up project.
  3. Drag the custom control from the CustomUserControl tab on toolbox to the form designer.
  4. Run the project and test the user control by clicking the buttons.

Exercise 5

Creating a custom about box form
In this exercise you will create a custom About Box with an OK button. You will then add another form that inherits from the About Box, but changes the behavior of the button.

  1. Create a new Windows Forms Application project called VisualInheritance.
  2. Add an About Box to the project called frmBase. Make the scope of the class frmBase public.
  3. Customize the About Box with appropriate labels.
  4. Add code to the button’s event handler to close the form, make the event handler virtual and have protected scope.
    Protected Overrides Sub OkButton_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)
  5. Make this form the start up form for the project and test it.
  6. Add a second Windows Form to the project called frmInherited.
  7. In this form’s code file (frmInherited.vb), edit the class definition to:
    Public Class frmInherited Inherits VisualInheritance.frmBase
  8. Override the OK button’s event handler, so that it also displays a message:
    Protected Overrides Sub OkButton_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    MessageBox.Show("Closing the inherited form.", "Close")
    MyBase.OKButton_Click(sender, e)
    End Sub
  9. Run the project and test the inherited About Box by clicking the OK button.
  10. View code file.

Back to the beginning

If you liked this post, please comment with your suggestions to help others.
If you would like to see more content like this in the future, please fill-in our quick survey.
Scroll to Top