vb.netbasic

How do i make this textbox update in real time


Basically i want one textbox to do this:

TextBox5.Text = TextBox4.Text / TextBox2.Text

It works but if i don't try to input something to the textbox it doesn't update. How do i ?


Solution

  • You can handle both TextBox_TextChanged events, and try to parse the strings as doubles. If both succeed, then do the math and update the TextBox.

    Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
        doMath()
    End Sub
    
    Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged
        doMath()
    End Sub
    
    Private Sub doMath()
        Dim tb2 As Double, tb4 As Double
        If Double.TryParse(TextBox2.Text, tb2) AndAlso Double.TryParse(TextBox4.Text, tb4) Then
            TextBox5.Text = (tb4 / tb2).ToString()
        Else
            TextBox5.Text = ""
        End If
    End Sub
    

    The TextBox will be updated immediately with the quotient when both other TextBoxes are valid doubles.

    Remember not to try to do math using strings. You may want to put Option Strict On at the top of your code file so the compiler tells you you can't do math with strings like this TextBox4.Text / TextBox2.Text