Creating Windows Forms Applications


Now that you have developed a simple console application, you can continue developing your first series of applications by developing a Windows application or an application that has a Graphical User Interface (GUI). Because you already have the solution created, you can simply add a new project that uses Visual Basic .NET Projects and then select Windows Application (see Figure 5.8).

Figure 5.8. Creating a simple Windows application using Visual Basic .NET.

Again, you can choose the default name , WindowsApplication1. After you click OK, you will notice a change in the Visual Studio environment. Because you have developed a Windows application, a WYSIWYG designer view is placed instead of code (see Figure 5.9).

Figure 5.9. Windows Application ”Form Designer view.

Because this is a Windows application, you can drag and drop some controls to it. To do that you click Toolbox to show the various controls and then drag and drop a set of controls to the form designer canvas (in this case, a label, as shown in Figure 5.10).

Figure 5.10. Adding controls to Windows forms from the toolbox.

Properties of the form elements can be set up by right-clicking the element, selecting Properties, and changing the appropriate properties (in this case, change the value of the label to show First Windows Application, as shown in Figure 5.11).

Figure 5.11. Setting form elements Properties.

That is all that's required for your basic application; go ahead and save it and build the solution. Notice that if you select Start Application, the first application, Console Application, would still be executed. Basically, the first application was set as the default by Visual Studio .NET as the Startup Project. To set the second project as the Startup Project, right-click the Windows application project in the Solution Explorer view and set the project as the Startup Project (shown in Figure 5.12).

Figure 5.12. Specifying a Startup Project.

Now if you select Start Application (Figure 5.13), the Windows application will be started.

Figure 5.13. Windows Form application.

Before you finish this example and go to the next one, add a bit of complexity to the Windows Form application. Because this is a GUI application, you can add a basic menu with two menu items, File and Exit, under it (Figure 5.14).

Figure 5.14. Adding a Menu control to a Windows form.

I am sure you must be thinking that these menu items have been added for a purpose, so you'll need to associate some logic with the menu Exit. In Visual Studio, the best way to accomplish that is to double-click the Exit Menu item, which opens the code view for the event handler MenuItem2_Click. By double-clicking the menu item, you can code in the corresponding event-handler logic (see Figure 5.15).

Figure 5.15. Event handler for a menu item.

This completes the section for creating Windows applications. The source code for the completed Windows application is shown in Listing 5.1.

Listing 5.1 Using Menus in Visual Basic .NET Windows Forms Applications
 Public Class Form1     Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code "     Public Sub New()         MyBase.New()         'This call is required by the Windows Form Designer.         InitializeComponent()         'Add any initialization after the InitializeComponent() call     End Sub     'Form overrides dispose to clean up the component list.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)         If disposing Then             If Not (components Is Nothing) Then                 components.Dispose()             End If         End If         MyBase.Dispose(disposing)     End Sub     'Required by the Windows Form Designer     Private components As System.ComponentModel.IContainer     'NOTE: The following procedure is required by the Windows Form Designer     'It can be modified using the Windows Form Designer.     'Do not modify it using the code editor.     Friend WithEvents Label1 As System.Windows.Forms.Label     Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu     Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem     Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()         Me.Label1 = New System.Windows.Forms.Label         Me.MainMenu1 = New System.Windows.Forms.MainMenu         Me.MenuItem1 = New System.Windows.Forms.MenuItem         Me.MenuItem2 = New System.Windows.Forms.MenuItem         Me.SuspendLayout()         '         'Label1         '         Me.Label1.Location = New System.Drawing.Point(56, 48)         Me.Label1.Name = "Label1"         Me.Label1.Size = New System.Drawing.Size(136, 24)         Me.Label1.TabIndex = 0         Me.Label1.Text = "First Windows Application"         '         'MainMenu1         '         Me.MainMenu1.MenuItems.AddRange(            New System.Windows.Forms.MenuItem() {Me.MenuItem1})         '         'MenuItem1         '         Me.MenuItem1.Index = 0         Me.MenuItem1.MenuItems.AddRange(            New System.Windows.Forms.MenuItem() {Me.MenuItem2})         Me.MenuItem1.Text = "File"         '         'MenuItem2         '         Me.MenuItem2.Index = 0         Me.MenuItem2.Text = "Exit"         '         'Form1         '         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)         Me.ClientSize = New System.Drawing.Size(292, 273)         Me.Controls.Add(Me.Label1)         Me.Menu = Me.MainMenu1         Me.Name = "Form1"         Me.Text = "Form1"         Me.ResumeLayout(False)     End Sub #End Region     Private Sub MenuItem2_Click(ByVal sender As System.Object,_        ByVal e As System.EventArgs) Handles MenuItem2.Click         System.Environment.Exit(0)     End Sub End Class 


Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net