Forgive me if this is not possible, here's the scenario.
I have 2 textboxes, I type something on textbox1 and press enter, the text goes to textbox2 and the program presses the enter key on textbox2(user will not press the enter key).
Here's what I have in textbox1_keydown
if e.keycode = keys.enter then
str = textbox1.text
textbox2.focus()
textbox2.text = str
'Make textbox2 press the enter key here, without user pressing it on keyboard
end if
Found the answer
if e.keycode = keys.enter then
str = textbox1.text
textbox2.focus()
textbox2.text = str
'Make textbox2 press the enter key here, without user pressing it on keyboard
SendKeys.Send("{ENTER}")
end if
If e.keycode = Keys.Enter Then
'/* Check whether textbox1 is empty or not */
If textbox1.text <> "" Then
textbox2.text = ""
textbox2.text = textbox1.text
SendKeys.Send("{ENTER}")
Else
'/* if textbox1 is empty then cursor focus on textbox1 only */
textbox1.focus()
End If
End If
OR
If e.keycode = Keys.Enter Then
textbox2.text = ""
textbox2.text = textbox1.text
SendKeys.Send("{ENTER}")
End If
Related
I am doing calculator and I wrote codes if user doesn't enter value to num1 and num2 it will give message to the screen and I did it with if-else. It is working for num1 and operator but not num2. How can I fix it?
If TextBox1.Text = "" Or TextBox2.Text = "" Then
If TextBox1.Text = "" Then
MsgBox("Please enter data to number 1 ")
TextBox1.Focus()
Label1.ForeColor = Color.Red
Else
Label1.ForeColor = SystemColors.ControlText
End If
ElseIf TextBox1.Text = "" Or TextBox2.Text = "" Then
If TextBox2.Text = "" Then
MsgBox("Please enter data to number 2")
TextBox2.Focus()
Label2.ForeColor = Color.Red
Else
Label1.ForeColor = SystemColors.ControlText
End If
End If
Your code should be something like:
If TextBox1.Text = "" Or TextBox2.Text = "" Then
If TextBox1.Text = "" Then
MsgBox("Please enter data to number 1 ")
TextBox1.Focus()
Label1.ForeColor = Color.Red
End If
' Seperate the conditions because TextBox1 and TextBox2 can both be empty, so an ElseIf will not handle it
If TextBox2.Text = "" Then
MsgBox("Please enter data to number 2")
TextBox2.Focus()
Label2.ForeColor = Color.Red
End If
Else ' Neither of them (so TextBox1 and TextBox2 won't be empty)
Label1.ForeColor = SystemColors.ControlText
End If
This will popup a message when the user leaves TextBox1 empty or TextBox2 empty, if he doesn't leave any of the two empty, the Else will kick in.
For more informations about conditions in VB.NET, take a look here: https://www.dotnetperls.com/if-vbnet
I have about 5 hidden textbox, and 1 button that when I click It it shows the hiddentext 1 by 1, but what I want to do is when I click the button once only 1 textbox appears and then a message box will appear saying "Do you want to Continue" YES or NO? if I press Yes, then the 2nd textbox will appear, but when I press No the messagebox should be closed.
I have this Code on the Button:
Private Sub revealtxtbox_Click(ByVal senders As System.Object, ByVal e As System.EventArgs) Handles revealtxtbox.Click
txtbox1.visible = True
If MsgBox("Do you Want to Continue", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Sample System") = MsgBoxResult.Yes then
txtbox2.visible = true
Elseif MsgBoxResult.Yes then
txtbox3.visible = true
Elseif MsgBoxResult.Yes then
txtbox4.visible = true
Elseif MsgBoxResult.Yes then
txtbox5.visible = true
End if
the code above somewhat works but when I press NO, the txtbox3 shows and the msgbox closed, it should not show txtbox3, it should only close the msgbox.
Try something more like this instead:
Private Sub revealtxtbox_Click(ByVal senders As System.Object, ByVal e As System.EventArgs) Handles revealtxtbox.Click
txtbox1.visible = True
If MsgBox("Do you Want to Continue", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Sample System") <> MsgBoxResult.Yes then
Exit Sub
End If
txtbox2.visible = true
If MsgBox("Do you Want to Continue", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Sample System") <> MsgBoxResult.Yes then
Exit Sub
End If
txtbox3.visible = true
If MsgBox("Do you Want to Continue", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Sample System") <> MsgBoxResult.Yes then
Exit Sub
End If
txtbox4.visible = true
If MsgBox("Do you Want to Continue", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Sample System") <> MsgBoxResult.Yes then
Exit Sub
End If
txtbox5.visible = true
End if
VISUAL BASIC: You know how you can make your own settings in your program setting/properties, well I made a register system for a software. You register by entering a password and username then pressing 'Register' to save the username and password to My.Settings.Username and My.Settings.Password using My.Settings.Save().
The code: My.Settings.Username = TextBox1.Text
My.Settings.Password = TextBox2.Text
My.Settings.Save()
This all works perfectly but how can I prevent the user from changing it again.
This is my code so far:
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim res As MsgBoxResult
If Not My.Settings.Username = String.Empty And
My.Settings.Password = String.Empty Then
MsgBox("You have registered a WCUE account, please use 'CHANGE PASSWORD' in 'Settings' to do so.", MsgBoxStyle.Exclamation, "Not possible.")
ElseIf TextBox1.Text = String.Empty Or
TextBox2.Text = String.Empty Then
MsgBox("Don't press the register button if the text boxes are empty!", MsgBoxStyle.Critical, "Fill all required fields please.")
Else
If TextBox1.Text = My.Settings.Username And
TextBox2.Text = My.Settings.Password Then
MsgBox("If you are trying to log in then press the 'Log-in' text below the register button!", MsgBoxStyle.Exclamation, "You have already registered!")
Else
res = MsgBox("Register a WCUE account?", MsgBoxStyle.YesNo, "Create")
If res = MsgBoxResult.Yes Then
My.Settings.Username = TextBox1.Text
My.Settings.Password = TextBox2.Text
My.Settings.Save()
MsgBox("You can now login to WCUE and set the time limit on this user!", MsgBoxStyle.Information, "YEAH - Register successful!")
ElseIf res = MsgBoxResult.No Then
MsgBox("You should register as quick as possible before someone unauthorized sets a password!!", MsgBoxStyle.Exclamation, "Suit yourself.")
End If
End If
Exit Sub
End If
End Sub
The FIRST if statement is what I am having trouble with, I tried to check if My.Settings.Username and My.Setting.Password contains a value (string) which should be the username and password, if it does not then you can register but if it does contain a value then it tells you that someone has already registered. Nothing happens?
I hope this is clear enough, please help!
I ~think~ this is what you're trying to do...
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
If My.Settings.Username <> String.Empty AndAlso My.Settings.Password <> String.Empty Then
MsgBox("You have registered a WCUE account, please use 'CHANGE PASSWORD' in 'Settings' to do so.", MsgBoxStyle.Exclamation, "Not possible.")
Else
If TextBox1.Text = String.Empty OrElse TextBox2.Text = String.Empty Then
MsgBox("Don't press the register button if the text boxes are empty!", MsgBoxStyle.Critical, "Fill all required fields please.")
Else
Dim res As MsgBoxResult
res = MsgBox("Register a WCUE account?", MsgBoxStyle.YesNo, "Create")
If res = MsgBoxResult.Yes Then
My.Settings.Username = TextBox1.Text
My.Settings.Password = TextBox2.Text
My.Settings.Save()
MsgBox("You can now login to WCUE and set the time limit on this user!", MsgBoxStyle.Information, "YEAH - Register successful!")
ElseIf res = MsgBoxResult.No Then
MsgBox("You should register as quick as possible before someone unauthorized sets a password!!", MsgBoxStyle.Exclamation, "Suit yourself.")
End If
End If
End If
End Sub
for some reason every time i try to press control + enter I get "send message" why is this? I am pressing control so I should get create line..
If e.KeyCode = Keys.Control And e.KeyCode = Keys.Enter Then
Debug.Print("create a new line")
ElseIf e.KeyCode = Keys.Enter Then
Debug.Print("send message")
End Sub
You can just use this
If e.Control And e.KeyCode = Keys.Enter Then
This is my code:
Private Sub prices_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles wholeprice_input_new_item.KeyPress, dozenprice_input_new_item.KeyPress, detailprice_input_new_item.KeyPress, costprice_input_new_item.KeyPress
Dim TxtB As TextBox = CType(sender, TextBox)
Dim rex As Regex = New Regex("^[0-9]*[.]{0,1}[0-9]{0,1}$")
'MsgBox(TxtB.Text())
If (Char.IsDigit(e.KeyChar) Or e.KeyChar.ToString() = "." Or e.KeyChar = CChar(ChrW(Keys.Back))) Then
If (TxtB.Text.Trim() <> "") Then
If (rex.IsMatch(TxtB.Text) = False And e.KeyChar <> CChar(ChrW(Keys.Back))) Then
e.Handled = True
End If
End If
Else
e.Handled = True
End If
End Sub
The textbox's Text property doesn't include the last character pressed, example:
Text entered = "12.1"
TxtB.Text = "12."
Text entered = "11.."
TxtB.Text = "11."
Text entered = "12"
TxtB.Text = "1"
I want to validate all the characters. How I can make the event keypress validate all characters in a text box?
The problem is that in the KeyPress event, the key that's being pressed is not yet added to the textbox. You could add the character that's being pressed to the existing text, something like this:
Dim TxtB As TextBox = CType(sender, TextBox)
If (Char.IsDigit(e.KeyChar) OrElse e.KeyChar = "."c Then
Dim fullText As String = TxtB.Text & e.KeyChar
'Do validation with fullText
End If
Actually, it's a little more complicated than that - what if the user pressed the backspace key, for instance? You might be better advised to use the TextChanged event instead, which fires after the Text property has been updated by the last key pressed.