howdy folks
please am working on an Annual Crop Cashflow Calculator like an Excel
i have 3 text boxes.
textbox22 textbox31 and textbox71
if i multiply textbox22 by textbox31 it shows in textbox71
but the problem is i need an event jus like excel after calculating and user goes back to empty either textbox31 or textbox22 the answer in texbox71 should be empty...
but with what i have here d previous answer will still be there until user inputs another entry

solution please
below is my code

 Private Sub TextBox71_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox22.TextChanged, TextBox31.TextChanged
        If String.IsNullOrEmpty(TextBox22.Text) OrElse String.IsNullOrEmpty(TextBox31.Text) Then Exit Sub
        If Not IsNumeric(TextBox22.Text) OrElse Not IsNumeric(TextBox31.Text) Then Exit Sub
         TextBox71.Text = CDbl(TextBox22.Text) * CDbl(TextBox31.Text)
    End Sub

Recommended Answers

All 5 Replies

Most common issue? The coder forgot to connect the event to the code that handles the event. It's not automatic.

@rproffitt - Actually the Handlesclause in VB.net looks after that.

@Reginald Xavier - It looks to me that after checking for the empty/incorrect string, you're exiting the sub routine and the recalculation never happens. One way around this is to reset the text to a base amount, probably 0, instead of exiting.

You also might have better results by handling the Validating event instead of the TextChanged event

commented: I wish that always worked for me. I had this not firing and went to the lightning bolt and had to fix it. Well, it is a Microsoft product so I agree +0

tinstafaal Thanks I went through MSDN and i used the Keypress event.. Now my code works just fine.
i edited the snipet and it worked

thanks

 Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged 
        TextBox2.Clear() 
    End Sub 

    Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress 
        TextBox1.Text = TextBox1.Text 
    End Sub 

    Private Sub TextBox2_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress 
        TextBox1.Clear() 
    End Sub 

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged 
        TextBox2.Text = TextBox2.Text 
    End Sub 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.