Results 1 to 3 of 3

Thread: Problem with writing a code to clear the result for TextBoxes using TextChanged

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2012
    Posts
    6

    Problem with writing a code to clear the result for TextBoxes using TextChanged

    Hi All,

    I am a newbie to VB. I use Visual Basic 2010. I am trying to build a basic Average Calculator Application. I can make the application calculate the average.
    However, I am unable to write the code to clear the resultLabel after the user enters new input into any of the TextBoxes. Could anyone please help and point me to the right direction here? Below is my codes:

    Public Class AverageCalculator

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


    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim number1 As Integer
    Dim number2 As Integer
    Dim number3 As Integer
    Dim total As Integer
    Dim average As Integer

    number1 = TextBox1.Text
    number2 = TextBox2.Text
    number3 = TextBox3.Text

    total = number1 + number2 + number3
    average = total / 3
    resultLabel.Text = average

    End Sub

    'clear result
    Private Sub TexBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    resultLabel.clear()

    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    resultLabel.clear()

    End Sub

    Private Sub Textbox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
    resultLabel.Clear()
    End Sub

    End Class

    Thanks in advance!
    TheZealous
    Last edited by TheZealous; Sep 9th, 2012 at 03:48 PM.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Problem with writing a code to clear the result for TextBoxes using TextChanged

    vb.net Code:
    1. resultLabel.Text = ""

    You could also save yourself a bit of code like this

    vb.net Code:
    1. 'Handles all three textbox events in one sub
    2. Private Sub TexBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
    3.  
    4.         resultLabel.Text = ""
    5.  
    6.     End Sub
    Last edited by dunfiddlin; Sep 9th, 2012 at 04:47 PM.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2012
    Posts
    6

    Re: Problem with writing a code to clear the result for TextBoxes using TextChanged

    Thanks for your help. The code does work!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width