jump to navigation

Commonly Used Controls in VB.NET June 26, 2008

Posted by prasu in : Technical, Tools, VB.NET , trackback
  1. Start a New Project or select an existing Project from the Start Page

  2. Add a New Windows Form to your Project

    To do this-

    Click on  Project—>Add Windows Form —> Select   Windows Form

    Give any name that you desire to the form.

  3. Make sure your Toolbox and Properties palette are visible.

  4. Now, drag a Label,Textbox and a Button from the Toolbox to the Form.

  5. Select the  ‘Label1’ by clicking it once, go to the Properties palette and clear the text from the Text  property. Similarly change the Text property of the  ‘Button1’ as Enter.

  6. As mentioned, VB.NET is about event programming. Let us write some code for the ‘Form Load’ event. This code will be executed as soon as the Form is opened. To do so, double click anyhere inside the Form (Aliter: Rt Click -> View Code)

    It will take you to the code window with the following code already placed-

  7. Public Class Form2
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    End Class
    
  8. The cursor will be blinking inside the method Form2_Load. By now you would have figured out that this is where we need to code for the Form Load event.
  9. Now write the following code inside the method-
  10. Me.button1.Enabled = False
    
  11. This line is used to disable the button1 control. i.e the Button will be visible but it cannot be clicked.
  12. Now go back to the Design Window for Form2 (from the Tabs at the top of the page) and double click on the Textbox

    Now you will be taken to the code window and the cursor will be blinking inside  the method  Textbox1_TextChanged

  13. The default event for a Textbox is the ‘Text Changed’ event. This can of course be changed. To view all the events associated with a Control and code for other events, you can see a dropdown at the top-right in the Code Window. Now write the following code for our Textbox:
  14. If Me.TextBox1.Text.Length > 0 Then
    Me.button1.Enabled = True
    End If
    
  15. ‘Me’ refers to the current form (analogus to ‘this‘ in Java).
    me.textbox1.text.length
    gives the length of the string typed in the Textbox.

    We are going to make the Button clickable (enabled) only if some text exists inside in the Textbox.

  16. Go back to the design window and then double click on the Button you will be taken to the method  Button_Click.

    Now write the code:

  17. Me.Label1.Text = "The text is " & vbCr & Me.TextBox1.Text & vbCr & "The length is " & Me.TextBox1.Text.Length
    
  18. In the above code we are setting the Text property of the Label programmatically.

    Any string is to be written inside the quotation marks.

    ‘&’  is the concatenation character

    vbCr is the new line character in VB.NET

  19. To summarize, we show here the complete code:
  20. Public Class Form2
    
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.button1.Enabled = False
    
        End Sub
    
        Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
            Me.Label1.Text = "The text is " & vbCr & Me.TextBox1.Text & vbCr & "The length is " & Me.TextBox1.Text.Length
    
        End Sub
    
        Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            If Me.TextBox1.Text.Length > 0 Then
                Me.button1.Enabled = True
            End If
        End Sub
    End Class
    
  21. Now Debug the program by clicking on the green run button (F5)

  22. Now write some text in the Textbox and click on the Button

    For Example I have written ‘Welcome to VB.net’.

Note:

The above message can also be made to show in a Message Box (’Hello World’ example like)
For that in the method button_click instead of using

Me.Label1.Text = "The text is " & vbCr & Me.TextBox1.Text & vbCr & "The length is " & Me.TextBox1.Text.Length

The following code has to be used

Msgbox("The text is " & vbCr & Me.TextBox1.Text & vbCr & "The length is " & Me.TextBox1.Text.Length)

You have now learnt some commonly used Controls in VB.NET

Comments»

1. Sifek - August 21, 2008

thanks…was lookin for this!



Warning: stristr() [function.stristr]: Empty delimiter in /home/tekyt17/public_html/dotnet/wp-content/plugins/wassup/wassup.php on line 2093