Click here to Skip to main content
15,900,565 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have two textbox...
on TextBox1_TextChanged event i set
textbox2.text=textbox1.text
the problem when i clear all number on textbox1..
in textbox2 still have one number if i press 0 on textbox1,
then textbox2 is clear ..
why like that? and how to solved it?

What I have tried:

If TextBox1.Text = "" Or Not IsNumeric(TextBox1.Text) Then
           Exit Sub
       End If
       Dim a As Double
       a = TextBox1.Text
       TextBox1.Text = Format(a, "#,###")
       TextBox1.SelectionStart = Len(TextBox1.Text)


       TextBox2.Text = TextBox1.Text
Posted
Updated 28-Jul-17 2:40am

1 solution

You need to test the key before the text is changed and cancel/suppress if invalid.
VB
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

    Dim a As Double
    a = TextBox1.Text
    TextBox1.Text = Format(a, "#,###")
    TextBox1.SelectionStart = Len(TextBox1.Text)
    TextBox2.Text = TextBox1.Text

End Sub

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown

    If e.KeyCode < Keys.D0 Or e.KeyCode > Keys.D9 Then
        e.SuppressKeyPress = True
    End If

End Sub

Update: If you want to allow edit keys as well, here is an updated version of the KeyDown event.
VB
Private validKeys() As Integer =
{
    Keys.Delete,
    Keys.Back,
    Keys.Clear,
    Keys.Home,
    Keys.Left,
    Keys.Right,
    Keys.End,
    Keys.D0,
    Keys.D1,
    Keys.D2,
    Keys.D3,
    Keys.D4,
    Keys.D5,
    Keys.D6,
    Keys.D7,
    Keys.D8,
    Keys.D9
}

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown

    If Not validKeys.Contains(e.KeyCode) Then
        e.SuppressKeyPress = True
    End If

End Sub
 
Share this answer
 
v2
Comments
Khabibb Mubarakk 28-Jul-17 9:01am    
still not clear on textbox2
Graeme_Grant 28-Jul-17 9:06am    
here is why:

TextBox1.SelectionStart = Len(TextBox1.Text) ' does nothing
TextBox2.Text = TextBox1.Text ' mirrors TextBox1

If you want to clear TextBox2 if TextBox1 has changed then:

If Len(TextBox1.Text) > 0 then
TextBox2.Text = ""
End If
Khabibb Mubarakk 28-Jul-17 9:15am    
i try but when i type on tb1 then on tb2 just blank..
Graeme_Grant 28-Jul-17 9:19am    
I gave an example ... You need to change my example to what you need:

If Len(TextBox1.Text) > 0 then
' if condition met, set TextBox2.Text to what is required...
End If

Or change the test to the condition that you want ... This exercise I left for you to do. ;)
Khabibb Mubarakk 28-Jul-17 9:30am    
you know if i use this

If TextBox2.Text = "" Or Not IsNumeric(TextBox2.Text) Then
Exit Sub
End If

b = TextBox2.Text
TextBox2.Text = Format(b, "#,###")
TextBox2.SelectionStart = Len(TextBox2.Text)
TextBox4.Text = TextBox2.Text
If Len(TextBox2.Text) = 0 Then
TextBox4.Text = ""
End If
is nothing happen same cannot clear
but if i use this
If Double.TryParse(TextBox1.Text, a) Then
TextBox1.Text = Format(a, "#,###")
TextBox1.SelectionStart = Len(TextBox1.Text)
TextBox6.Text = TextBox1.Text
End If
If Len(TextBox1.Text) = 0 Then
TextBox6.Text = ""
End If
this work ..
can u explain to me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900