Sams Teach Yourself Visual Studio .NET 2003 in 21 Days

Sams Teach Yourself .Net in 21 Days

By Jason Beres

Hello .NET!

Every introduction to writing applications needs to start with the Hello World type application. This isn't because it's such a cool cliché. It's to get you familiar with the environment you're working in and to better explain what's going on when you're running the application. To get you started with Visual Studio .NET development, you're going to create the HelloNET application.

To begin, start Visual Studio .NET. On the Getting Started page, click the New Project button. You're prompted with the New Project dialog you learned about yesterday. Figure 3.1 demonstrates the New Project dialog.

03fig01.jpg

Figure 3.1 The New Project dialog box.

Select the Windows Application template from either the C# or Visual Basic folder, and change the Name to HelloNET. Click the OK button to create the application.

After the project has been created, you should be looking at something like Figure 3.2.

03fig02.jpg

Figure 3.2 The HelloNET application.

As you learned yesterday, there are many useful windows when developing applications. The key items you'll use when creating Windows Forms applications are the Solution Explorer, the Properties window, and the Toolbox. If you don't see these windows on your screen, you can get to them in the ways shown in Table 3.1.

Table 3.1. Shortcuts to Main Windows When Designing Forms

Window

Shortcut Options

Properties

F4 key

View, Properties from the View menu

The Properties button on the standard toolbar

Toolbox

Ctrl+Alt+X shortcut keys

View, Toolbox from the View menu

The Toolbox button on the standard toolbar

Solution Explorer

Ctrl+Alt+L shortcut keys

View, Solution Explorer from the View menu

The Solution Explorer button on the standard toolbar

The key to rapid development with Windows Forms is the ability to easily drag items from the Toolbox onto the forms, set properties on the controls that you add using the Properties window, and write code that responds to the controls and form events. That's why the three windows in Table 3.1 are so important.

Next, you must add some controls to the form. From the Toolbox, drag a Button control, Textbox control, and Label control to the form that's in the Windows Forms designer. Your form should look something like Figure 3.3 after the controls are added.

03fig03.jpg

Figure 3.3 Adding controls to the default form.

Next, single-click the form and press the F4 key to view its properties. Change the following properties on the form:

After you set the Font property, notice that the font sizes of the controls on the form change to the form's font properties. This is a new feature in Windows Forms; the controls inherit the font properties of the form. You can easily override these properties by setting properties on the individual controls. You learn more about the different controls a little later.

Next, double-click Button1 to get to the Code Editor window. This works like Visual Basic 6 did: You double-click controls and you're taken to their default event.

You're now looking at the Form_Load event. Notice that the load event accepts two arguments—System.Object and System.EventArgs—as the following code snippet demonstrates:

vbnet_icon.gif
Private Sub Form1_Load(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

c_icon.gif
        private void Form1_Load(object sender, System.EventArgs e)
        {

        }

Every control that you add to a form (and the form itself) always has to accept the object that's passing it the data and then the event arguments for that object. Depending on the control and the event, it won't always be System.EventArgs, but there will always be an event arguments parameter.

In the Form_Load event, add the code in Listing 3.1, which displays a message box welcoming you to Visual Studio .NET.

Example 3.1. The Form_Load Event Code for HelloNET

vbnet_icon.gif
Private Sub Form1_Load(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles MyBase.Load

    MessageBox.Show("Welcome to .NET!")

End Sub

c_icon.gif
private void Form1_Load(object sender, System.EventArgs e)
{
    MessageBox.Show("Welcome to .NET!");
}

You'll notice that the MessageBox class is used to prompt information back to the user. The functionality of the MessageBox class is very similar to that of the MsgBox function in Visual Basic 6. You can pass a prompt, respond to button events, and set the title for the message box that's displayed.

Next, you need to add code for the click event of the Button you added to the form earlier. There are several ways to write code that responds to events in Windows Forms. The easiest way to write code for an event is to double-click a control on the Forms Designer and you're taken to the default event for that control in the Code Editor.

If you're writing in Visual Basic, you can select the control from the Class Name drop-down list in the upper-left corner of the Code Editor. After you select the control you want to write an event for, you can select the correct method from the Method Name drop-down list, which is next to the Class Name drop-down list. Figure 3.4 demonstrates the Method Name drop-down list for some of the Button1 events. Notice that Button1 was selected from the Class Name list.

03fig04.jpg

Figure 3.4 The Button1 Method Name options.

You'll also notice the (Overrides) and (Base Class Events) options in the Class Name drop-down. The (Overrides) option gives you all the methods, properties, and events that you can programmatically override for the form. The (Base Class Events) option gives you the methods for the Form class. A little later today you'll better understand how classes work and what they'll mean to you as you develop not only in Windows Forms, but also in .NET as a whole.

If you're a C# developer, you don't have the Class Name and Method Name drop-down lists in the Code Editor for events that haven't been added to the form. To add new events for the form and for controls on the form, you must select the control on the form while you're in the Forms Designer. Then, on the toolbar of the Properties window for the selected control, click the lightning bolt button, which gives you the list of events for the selected control. When you find the event you want to write code for, you can double-click the event name in the list, and you're taken to the Code Editor for that event. Figure 3.5 demonstrates the button1_Click event selected in the Properties window.

03fig05.jpg

Figure 3.5 The button1_Click event of the Properties window for a C# application.

Now that you know how to add events with the IDE, add the code in Listing 3.2 to the button1_Click event.

Example 3.2. button1_Click Event for the HelloNET Application

vbnet_icon.gif
Private Sub Button1_Click(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles Button1.Click

    Label1.Text = TextBox1.Text

End Sub

c_icon.gif
private void button1_Click(object sender,System.EventArgs e)
{
    label1.Text = textBox1.Text;
}

There are a few items to note on the code you just wrote:

Now that the code has been added and you're getting an idea of how to work with the IDE, press the F5 button to run the application. Pressing F5 has the same effect as selecting Start from the Debug menu or clicking the Start button on the Standard toolbar.

When the application starts, you're prompted with Welcome to .NET! that you added in the Form_Load event. After the form is active, type Hello .NET into the TextBox and click the Button on the form. The Label control is filled with the contents of the TextBox.

You also set some properties in the form, such as MinimumSize, MaximumSize, AutoScroll, and StartPosition. When the application starts, the form shows up in the middle of the screen. If you now resize the form, you'll see that it can only grow to a certain size and shrink to a certain size. This is a cool feature that wasn't in Visual Basic 6. The bigger-than-life feature is AutoScroll. If you resize the form and the controls can't remain visible, scrollbars appear automatically. Figure 3.7 demonstrates AutoScroll in action.

03fig07.jpg

Figure 3.7 The AutoScroll property for a form in action.

You can click the X in the upper-right of the form to close the form and get back to the designer.

The AutoScroll property is one of many cool properties that forms and controls have in .NET. To see some of the new properties, follow Table 3.2 and change the corresponding properties.

Table 3.2. Properties to Change on the Form1 Controls

Control

Property

Value

Form1

Name

Opacity

FormBorderStyle

SizeGripStyle

Text

TopMost

frmMain

80%

SizeableToolWindow

Auto

Wow This Is Great!

True

Button1

Anchor

Cursor

FlatStyle

BackColor

Text

ForeColor

Name

Top, Left, Right

Hand

Flat

SteelBlue

Click Me!

LightGray

ClickMe

Label1

Dock

TextAlign

Bottom

BottomRight

Textbox1

CharacterCasing

MultiLine

AcceptsReturn

Anchor

Size

Location

TextAlign

Cursor

Upper

True

300,300

Top, Bottom, Left, Right

292, 140

28, 68

Right

Cross

After you've changed the properties for the controls, press the F5 key to run the application. Figure 3.8 shows the form after typing some text in the box and resizing the form.

03fig08.jpg

Figure 3.8 Running the HelloNET application after changing object properties.

You'll notice some very cool things happening with this form:

Besides making a form that has no real use, the goal of setting the properties for the controls on the form was to introduce you to some of the cooler features of Windows Forms. When you changed properties such as Anchor, Dock, Cursor, and TextAlign, you learned that the Properties window options are visual and easy to use. In addition to the many new properties coming from a Visual Basic 6 environment, using the Properties window is easier than ever. As usual, the description for each property is still visible on the lower portion of the Properties window, so you don't need to press the F1 key for help when you're not sure about a property's functionality.

Now that you have your feet wet with the IDE, you can learn about how the applications you write using Windows Forms actually work.

Share ThisShare This

Informit Network