Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

C# ПІДРУЧНИКИ / c# / Premier Press - C# Professional Projects

.pdf
Скачиваний:
475
Добавлен:
12.02.2016
Размер:
14.7 Mб
Скачать

868

Part X

 

APPENDIXES

 

 

 

 

 

 

 

 

[STAThread]

 

 

 

 

static void Main()

 

 

 

 

{

 

 

 

 

 

 

Application.Run(new frmAcceptUserInput());

 

 

}

 

 

 

 

 

private void btnSubmit_Click(object sender, System.EventArgs e)

 

 

{

 

 

Y

 

 

 

 

L

 

 

 

MessageBox.Show(“The user name and password that you have specified is

 

 

 

accepted.”);

F

 

 

 

 

 

 

 

}

 

M

 

 

}

 

 

 

 

 

 

A

 

 

 

private void btnExit_Click(object sender, System.EventArgs e)

 

 

{

E

 

Application. xit();

} } T

After creating an application in Visual C# .NET, you can create this application in Visual Basic .NET.

Creating a Simple Application

in Visual Basic .NET

The steps for creating an application in Visual Basic .NET are similar to the steps for creating an application in Visual C# .NET. Similar to Visual C# .NET, Visual Studio .NET also provides you with a template to create an application in Visual Basic .NET. To create the SampleWindowsApplication by using Visual Basic

.NET, perform the following steps:

1.On the File menu, point to the New option.

2.In the displayed list, select the Project option. The New Project dialog box is displayed.

3.In the Project Types: pane of the New Project dialog box, select the Visual Basic Projects option.

Team-Fly®

INTRODUCTION TO VISUAL BASIC .NET Appendix B 869

4.In the Templates: pane, select the Windows Application option.

5.In the Name: text box, type the name of the Windows application as

SampleWindowsApplication1.

6.Accept the default location as specified in the Location: text box. You may also choose to browse for the location where you want to save the application by clicking on the Browse button.

7.Click on the OK button to close the New Project dialog box.

Figure B-2 shows the New Project dialog box for the SampleWindowsApplication1 project in Visual Basic .NET.

FIGURE B-2 The New P roject dialog box for SampleWindowsApplication1

When you click on the OK button, Visual Studio .NET automatically creates the default files and a blank Windows form for you. Click on the Show All Files button in the Solution Explorer window to view a list of all the files created by Visual Studio .NET. Figure B-3 shows the default files and the blank form created by Visual Studio .NET.

870

Part X

APPENDIXES

 

 

 

FIGURE B-3 The default files and the blank form created by Visual Studio .NET

As you can see in Figure B-3, the blank form in Visual Basic .NET is created with an extension .vb. In addition, Visual Studio .NET creates some reference files for the SampleWindowsApplication1 project. Similar to Visual C# .NET, Visual Studio .NET creates a solution with the same name as that of the Windows application. Inside the solution, the project with the name SampleWindowsApplication1 is created.

Now, proceed with the creation of the SampleWindowsApplication1 application. To create the application, you need to add controls to the Windows form. Because Visual Basic .NET and Visual C# .NET are languages based on the .NET Framework, the IDE for the Visual Basic .NET applications is the same as that of the Visual C# .NET applications. The IDE for the Visual Basic .NET applications contains a toolbox that contains controls that you can use to create the Windows application. Figure B-4 shows the toolbox that contains standard controls for creating a Windows application in Visual Basic .NET.

INTRODUCTION TO VISUAL BASIC .NET Appendix B 871

FIGURE B-4 The toolbox for creating Windows application

From the toolbox, drag two label controls, two text box controls, and two button controls and place them on the form. Now in the Properties window of the controls, change the properties of the controls.

TIP

If the Properties window is not displayed, select the control and press the F4 key. Alternatively, you can select the Properties Window option on the View menu.

Change the following properties of the controls:

Form1

Name: Form1

Text: Accept User Input

872

Part X

APPENDIXES

 

 

 

Label1

Name: lblUserName

Text: User Name

Label2

Name: lblPassword

Text: Password

Textbox1

Name: txtUserName

Textbox2

Name: txtPassword

PasswordChar: [*]

Button1

Name: btnSubmit

Text: submit

Button2

Name: btnExit

Text: Exit

After adding the controls, you need to add the code to the button controls to make them functional.The following sections discuss how to write code in Visual Basic .NET.

Adding Code to the Submit Button

When the user clicks on the Submit button, a message box is displayed. To do this, add the following code to the Click event of the Submit button.

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles btnSubmit.Click

INTRODUCTION TO VISUAL BASIC .NET Appendix B 873

MessageBox.Show(“The user name and password that you have specified is

accepted.”)

End Sub

The preceding code creates a Sub procedure with the private access modifier for the Click event of the Submit button. As you can see in the preceding code, the event handler declaration for the Click event includes two parameters, a sender object and an event argument. In addition, the statement includes a Handles keyword. This keyword indicates that whenever a Click event occurs for the Submit button, the event is handled by the Sub procedure, btnSubmit_Click.

Inside the Sub procedure, the Show() method of the MessageBox class is used to display a message. Figure B-5 shows the message box when the user clicks on the Submit button.

FIGURE B-5 The message box displayed when user clicks on the Submit button

Adding Code to the Exit Button

On clicking the Exit button, the Windows application should exit.To do this,add the following code to the Click event of the Exit button.

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles btnExit.Click

Application.Exit()

End Sub

The preceding code creates an event handler Sub procedure for the Click event of the Exit button. Inside the Sub procedure, the Exit() sub of the Application class is used to exit the application. Figure B-6 shows the Exit button in the Accept User Input form.

874

Part X

APPENDIXES

 

 

 

FIGURE B-6 The Exit button in the Accept User Input form

As you can see, writing code for a Visual Basic .NET application is very similar to adding code to the Visual C#. NET application. However, to have a better understanding of the code of Visual Basic .NET as compared to the code of Visual C# .NET, look at the complete code for Visual Basic .NET. The entire code for the SampleWindowsApplication1 application is as follows:

Public Class Form1

Inherits System.Windows.Forms.Form

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles btnSubmit.Click

MessageBox.Show(“The user name and password that you have specified is accepted.”)

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles btnExit.Click

Application.Exit()

End Sub

End Class

The overall code for the Visual Basic .NET application is slightly different from that of the Visual C# .NET. As you can see, the preceding code creates a class Form1, which is inherited from the Form class. The Form class is present in the System.Windows.Forms namespace. The Inherits keyword is used to inherit a class

INTRODUCTION TO VISUAL BASIC .NET Appendix B 875

from a base class. Next, the code contains the declarations for the event handlers for the Submit and Exit buttons.

TIP

A major difference in the code of Visual Basic .NET and Visual C# .NET is that the statements in Visual Basic .NET are not followed by a semicolon (;) as in Visual C#

.NET.

Summary

In this chapter, you learned about the various languages of Visual Studio .NET. Visual C# .NET, Visual Basic .NET, and Visual C++ .NET are the three main languages of Visual Studio .NET. In addition, you looked at an overview of Visual Basic .NET.

Next, you learned about the different features of an object-oriented programming language. These features include abstraction, encapsulation, inheritance, and polymorphism.

You also learned about the various components of Visual Basic.NET, such as variables, constants, operators, arrays, collections, procedures, arguments, and functions. In addition, you learned to create a simple Visual C# .NET Windows application. Finally, you learned to create the same application by using Visual Basic .NET.

This page intentionally left blank

Appendix C

Visual Studio .NET

Integrated

Development

Environment