One Textbox Value Greater then other, Avoid and Beep - vb.net

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

Related

Check for certain text in multiple TexBoxes

I have a prank antivirus program I'm making for a friend. Part of the software requires "activation" to remove the "virus". I have 4 TextBoxes when I click the button I want all 4 TexBoxes to be checked for the text "0000". When I have one TextBox it works great, but I need all 4 boxes to get checked before the message box appears. I hope this makes sense. See image here
[Edit] I'm going to mention i'm a total noob at programming.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "0000" Then
MsgBox("Registered")
Me.Hide()
End If
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
End Class
There are many ways to do what you want. Here's a very simple one which you can build on:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' check all the TextBoxes in the array. Return if one isn't valid
For Each textbox As TextBox In {TextBox1, TextBox2, TextBox3, TextBox4}
If textbox.Text <> "0000" Then
Return
End If
Next
' If all TextBox contains the valid string, this will appear
MsgBox("Registered")
Me.Hide()
End Sub
Have fun!
EDIT:
To have 4 different strings: just chain 4 checks. Like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' If all TextBox contains the valid string, this will appear
If TextBox1.Text = "0000" AndAlso TextBox2.Text = "1111" AndAlso TextBox3.Text = "2222" AndAlso TextBox4.Text = "3333" Then
MsgBox("Registered")
Me.Hide()
End If
End Sub

I want to Add Hypen on first 3 Characters

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

Editing existing key press event in visual basic

This is a small piece of code for a project I'm working on for my study.
We have to make a program in Visual Basic (with Visual Studio 2015) that copies text from the first text box and pastes it into the second one once you press the button that states: "Show Name".
We are meant to handle it so that if the value entered is between 'A' to 'Z' then it copies the text and pastes it normally once you press the button into the second text box.
We are also supposed to make it so that, if the value is a number (between 0 and 9) we are meant to have a message which pops up saying something like: "Error - You Entered a Number".
We are also supposed to make a box that pops up saying: "Error - You Entered Something Other Than a Number", if the value is a character other than a letter or a number. I am in kind of a rush and would appreciate any help soon.
Here is my code so far. (I know Keys.A and Keys.Z and the message box is wrong, which I need to fix, too):
Public Class MyFirstProgram
Private Sub DisplayTextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisplayTextButton.Click
ShowTextBox.Text = EnterTextBox.Text
End Sub
Private Sub me_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Dim letterEntered As Char
If e.KeyCode < Keys.A Or e.KeyCode > Keys.Z Then
MsgBox("Error - Use letter keys only!", MsgBoxStyle.OkOnly, )
Else
letterEntered = LCase(ChrW(e.KeyCode))
If ShowTextBox.Text = "" Then
ShowTextBox.Text = letterEntered
Else
ShowTextBox.Text = ShowTextBox.Text + letterEntered
End If
End If
End Sub
Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
EnterTextBox.Text = ""
End Sub
Private Sub MyFirstProgram_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
End Class
Oh, and I'm new to Visual Basic/programming; so, please try to be patient if you can. Sorry :/
Use KeyPress press event .. instead of keyDown
Private Sub me_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
'If e.KeyCode < Keys.A Or e.KeyCode > Keys.Z Then
' MsgBox("Error - Use letter keys only!", MsgBoxStyle.OkOnly, )
'Else
' Dim letterEntered = LCase(ChrW(e.KeyCode))
' ShowTextBox.Text = ShowTextBox.Text + letterEntered
'End If
'e.Handled = True
End Sub
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If Not Char.IsLetter(e.KeyChar) Then
MsgBox("Error - Use letter keys only!", MsgBoxStyle.OkOnly)
e.Handled = True
End If
End Sub
just look in to the IsNumeric Function if it could help you.

Advanced Calculator

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

Validating Textboxs

I am doing a simple aplication using Windows Forms and I have a question...
My form has 15 textboxs and I want to validate everyone using the event KeyPress or validating. I have this code that is working:
If Not IsNumeric(txtn1.Text) Then
e.Cancel = True
ErrorProvider1.SetError(txtn1, "")
Else
something(txtn1.text)
End If
But I have 15 textboxs (maybe more) and is a little humdrum copy/pase this code in every textbox event. Can you teach me to do this using a function?
Public Function isnum(ByVal txt As TextBox, ByVal errpro as ErrorProvider) As Double
If Not IsNumeric(txt.Text) Then
e.Cancel = True <-------------------------------This dont work
errpro.SetError(txt, "")
End If
End Function
Private Sub txtn1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtKLDC.Validating
if isnum(txtn1, ErrorProvider1) then
something(txtn1.text)
end if
I´m looking for the correct way to do this?
English is my second language and I'm learning programming too.
Use a common KeyPress event then use the sender object which is the TextBox that originated the Event and cast it to a TextBox.
Private Sub txt_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtn1.KeyPress, txtn2.KeyPress, txtn3.KeyPress, txtn4.KeyPress, txtn5.KeyPress
Dim tb As TextBox = CType(sender, TextBox)
If Not IsNumeric(tb.Text) Then
e.Handled = True
ErrorProvider1.SetError(tb, "")
Else
something(tb.Text)
End If
End Sub
With this code:
Private Sub NumericValidation_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles TextBox1.KeyPress, TextBox2.KeyPress, upto 16500 or so is possible...
Dim txt As TextBox = CType(sender, TextBox)
If Not IsNumeric(txt.Text) Then
e.Cancel = True
ErrorProvider1.SetError(txt, "")
Else
something(txt.text)
End If
End Sub
Notice how I assign each one of the Textbox's KeyPress Event Handler to the single NumericValidation_KeyPress sub routine. I cast the sender into a Textbox to find out which textbox keypress event fired.