If ElseIF Statement in Visual Basic .Net

Problem:

Create a vb.net windows application from program that will determine if the user enters a word admin or user, a message box will be prompted “welcome admin” if admin and “welcome user” if user, the program will display a “message who are you?” if the user enters a different name.

Solution:

We are going to use the if elseif statement in order for the program to determine if the user enters admin, user or a different name.

the tutorial uses visual studio 2010 but it will also work for the newer version of the visual studio.

Let’s start the tutorial

  1. Open your Microsoft Visual Studio 2010
  2. In the File Menu select New Project.
  3. Select Visual Basic and Windows Form Application and click OK
  4. We will add controls to our form:
    1. 1 label
    2. 1 textbox
    3. 1 button
  5. The form should look like the image below.
    vb6-14-1
  6. Double click the Check name button and paste the code.
If TextBox1.Text = "admin" Then
MessageBox.Show("welcome admin")
ElseIf TextBox1.Text = "user" Then
MessageBox.Show("welcome user")
Else
MessageBox.Show("who are you?")
End If

Code Explanation:

The program will get the input of the user from the textbox then compares it if it is admin, user or another name. A message box will display welcome admin if the user enters admin, welcome user will be displayed if the user enters user in the textbox, and who are you? if it enters a different value.

  1. Save and Run the project (press F5).

vb6-14-2
Download

, , ,

Post navigation