Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

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