I have the below code on the KeyPress event of a textbox howver a need has now come in meaning I have to do myTextbox.Text = “A input string”
Private Sub ChequeAmountKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtChequeAmount.KeyPress
Dim tb As TextBox = sender
If (e.KeyChar = "." AndAlso tb.Text.Length = 0) Then
e.Handled = True
End If
If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Or (e.KeyChar = "." And tb.Text.IndexOf(".") < 0)) Then
e.Handled = True
End If
If (tb.SelectionStart > tb.Text.Length - 2 And tb.Text.IndexOf(".") >= 0 And tb.Text.IndexOf(".") + 3 = tb.Text.Length) Then
e.Handled = True
End If
If (tb.Text.IndexOf(".") < 0 And tb.Text.Length >= 4 And e.KeyChar <> ".") Then
e.Handled = True
End If
If (tb.Text.IndexOf(".") >= 0 And tb.Text.Length >= 7) Then
e.Handled = True
End If
If (e.KeyChar = ControlChars.Back) Then
e.Handled = False
End If
End Sub
This input string could contain a format that I do not want and whilst the keypress event caters for the need when the user is typing I need to handle it in the case of a text assignment.
I thought firing the keypress event after I assigned the text property but I dont think that will offer what I want.
Thanks
What you need to do is refactor this code into a method and then call that method from the relevant events.