I have a calculator with this code at the end:
'Code for when more than 3 digits are pressed'
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Length > 3 Then
MsgBox("You can't add any more numbers!")
TextBox1.Text = TextBox1.Text.Remove(TextBox1.Text.Length - 1, 1)
But when I do a sum on the calculator the textbox is still limited to the digits so all the answers are 3 digits long.
How do I change the code so it limits the textbox when entering digits but not when answering a sum?
You can use e.Cancel = True
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Length > 3 Then
MsgBox("You can't add any more numbers!")
e.Cancel = True
Take a boolean variable 'IsModifiedByUser' at form level. By default, it should be true. When you are programmetically modifying the textbox, set value of IsModifiedByUser as false before that. After you have modified textbox programmetically, again set the value of IsModifiedByUser as true. Check the length of textbox value only if IsModifiedByUser is true.
Private IsModifiedByUser As Boolean = True
Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
If TextBox1.Text.Length > 3 AndAlso IsModifiedByUser Then
MsgBox("You can't add any more numbers!")
e.Cancel = True
End If
End Sub
Public Sub CalculateValue()
IsModifiedByUser = False
'Do the calculation and set the value in textbox
IsModifiedByUser = True
End Sub
Related
For Learning the coding I built this application. It has Three text boxes.1 and 2 to enter numbers and 3 is to sum of 1 and 2. Button 2 is to get sum and 1 to clear textboxes.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text >= 101 Then
MsgBox("Enter numbers between 0 and 100")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox3.Text = Int(TextBox1.Text) + Int(TextBox2.Text)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
End Sub
End Class
Here I have limited the TextBox1 to get numbers between 0 and 100. if it is over 100, displays a warning massage box.
When I click the clear button it gives an error. It doesn't clear. after deleting textbox 1 clearing code it works fine. I mean textbox 2 & 3 clear fine. There is a problem with Textbox 1. The reason i believe is the msgbox attached it. I need to keep Msgbox.
How do I clear them?
You need to suppress text changed event. See TextBoxBase.Clear Method
Public Class Form1
Private flag As Boolean
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
' Check the flag to prevent code re-entry.
If flag = False Then
' Set the flag to True to prevent re-entry of the code below.
If TextBox1.Text >= 101 Then
MsgBox("Enter numbers between 0 and 100")
End If
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox3.Text = Int(TextBox1.Text) + Int(TextBox2.Text)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
flag = True
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
flag = False
End Sub
End Class
TextBox1.Clear() will fire TextBox1_TextChanged().
This line will do an implicit conversion from text to integer - it will fail with an error on blank or text entries:
If TextBox1.Text >= 101 Then
Instead, try this:
Dim number As Integer
If Int32.TryParse(TextBox1.Text, number) Then
If number >= 101 Then
MsgBox("Enter numbers between 0 and 100")
End If
End If
This is my problem. In KeyUp
If Textbox1.Text.Length = 3 Then
Textbox1.Text = Textbox1.Text.Insert(3, "-")
Textbox1.SelectionStart = Textbox1.TextLength
End If
The Output is like This:
AAA-0123
For example, I entered wrong on the first 3 letters and I want change it, But still i cant delete the hyphen
AAA-
It is realy depending on how you are editing your string. Anyway this mught be what you are looking for:
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
If TextBox1.Text.Length = 3 And e.KeyCode <> Keys.Back Then
TextBox1.Text = TextBox1.Text.Insert(3, "-")
TextBox1.SelectionStart = TextBox1.TextLength
End If
End Sub
Maybe you are looking for the MaskedTextBox? So you can only enter text in this format.
Masked Textbox MSDN
Private Sub MaskedTextBox1_MaskInputRejected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MaskInputRejectedEventArgs) Handles MaskedTextBox1.MaskInputRejected
If (Me.MaskedTextBox1.MaskFull) Then
ToolTip1.ToolTipTitle = "Input Rejected - Too Much Data"
ToolTip1.Show("You cannot enter any more data into the date field. Delete some characters in order to insert more data.", Me.MaskedTextBox1, Me.MaskedTextBox1.Location.X, Me.MaskedTextBox1.Location.Y, 5000)
ElseIf (e.Position = Me.MaskedTextBox1.Mask.Length) Then
ToolTip1.ToolTipTitle = "Input Rejected - End of Field"
ToolTip1.Show("You cannot add extra characters to the end of this date field.", Me.MaskedTextBox1, 0, -20, 5000)
Else
ToolTip1.ToolTipTitle = "Input Rejected"
ToolTip1.Show("You can only add numeric characters (0-9) into this date field.", Me.MaskedTextBox1, 0, -20, 5000)
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.ToolTip1.IsBalloon = True
Me.MaskedTextBox1.Mask = "00/00/0000"
End Sub
Private Sub MaskedTextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MaskedTextBox1.KeyDown
' The balloon tip is visible for five seconds; if the user types any data before it disappears, collapse it ourselves.
Me.ToolTip1.Hide(Me.MaskedTextBox1)
End Sub
i have a text box to enter a certain digit , if the entered number is a one digit number i want a leading zero to be added automatically.How could I do this? whats the code for this ?
Please help
Use the Validating event to be sure that your code will always fire. And use the x2 format to specify that you need 2 digit number.
Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles TextBox1.Validating
If IsNumeric(TextBox1.Text) Then
TextBox1.Text = CInt(TextBox1.Text).ToString("x2")
End If
End Sub
Protected Sub TextBox1_TextChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Length = 1
TextBox1.Text = "0" + TextBox1.Text
End if
End Sub
This should work, and it should also have the text start from the end of the textbox.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Length = 1 Then
TextBox1.Text = "0" + TextBox1.Text
TextBox1.SelectionStart = TextBox1.TextLength + 1
End If
End Sub
I'm experimenting with a simple auto typer. I've made several of these before, but have never received this error. I just want to be able to send the text within textbox3 to the currently focused window. Here's the code :
code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Timer1.Enabled = False Then
Timer1.Enabled = True
Button2.Text = "Stop"
ElseIf Timer1.Enabled = True Then
Timer1.Enabled = False
Button2.Text = "Send"
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If CheckBox1.Checked = True Then
SendKeys.Send("{ENTER}")
SendKeys.Send(TextBox3.Text)
ElseIf CheckBox1.Checked = False Then
SendKeys.Send(TextBox3.Text)
End If
End Sub
Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
Timer1.Interval = NumericUpDown1.Value
End Sub
Thats the only portion involving the sendkeys method and timer. Some of the text within textbox3 comes from variables as well, if that helps to know.
In another (now deleted) question, you claim the error happens here:
SendKeys.Send(Textbox3.Text)
Well, what is the value of Textbox3.Text which produces the error? According to the documentation, specifying repeated keys would involve a key token and an integer separate by a space, enclosed in curly braces. Something like "{LEFT 42}" or "{h 10}". So if you're entering something like "{A B}" then it's going to fail.
Presumably the runtime value of Textbox3.Text has an invalid format for SendKeys.Send(), or is mis-using characters which are otherwise reserved for the format string.
I have two Textboxes in Visual Basic 2010
Textbox1
Textbox2
Now I want that if user enter value(integer) in Textbox2, if the value in Textbox2 is Greater than Textbox1, so this will do a beep and also avoid him to do like this
Example: If User write 5 in Textbox1 and now he is writing 8 in Textbox2 so as 8 is grater than 5, so i want that Textbox2 ignore with a beep.
I have this code but this is not working, please if some one help me:
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
Dim valx1 As Integer
Dim valx2 As Integer
valx1 = (Val(TextBox1.Text))
valx2 = (Val(TextBox2.Text))
If (valx1) > (valx2) Then
Beep()
e.Handled = True
End If
End Sub
This code will suits your requirement, By the way using a beeb sound for alerting the user, will make him/her disturbed.
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
if val(TextBox1.Text.trim) > val(TextBox2.Text.trim & e.keychar) then
Beep()
e.Handled = True
end if
End Sub