Editing existing key press event in visual basic - vb.net

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.

Related

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

cursor is not at end of text after routine adds 1 char

I type a couple of letters in the textbox in vb.net and then hit the up arrow. This adds an accented e to the textbox but the cursor is not at end of text.
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Up Then
MessageBox.Show("Up arrow key")
TextBox1.Text = TextBox1.Text & "é"
TextBox1.SelectionStart = TextBox1.Text.Length + 1
End If
End Sub
It is a very strange behaviour but it seems than vb.net is not updating the positions until TextBox1_KeyDown has finished.
What I propose until anyone propose something better is starting a timer and doing the updating of the SelectionStart in the Timer.
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Up Then
MessageBox.Show("Up arrow key")
TextBox1.Text = TextBox1.Text & "é"
Timer1.Start()
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Stop()
TextBox1.SelectionStart = TextBox1.Text.Length
End Sub
And by the way, you do not need the +1 in TextBox1.SelectionStart = TextBox1.Text.Length + 1

Event for if text cursor is in textbox

I am sure the answer is out there, but I am unsure how to word it...
So I am looking for an event or a way , if the user has clicked on textbox1 (and put their input in) they can press enter and it will do a task. But if they did that with textbox2, then it would do a different task.
Sorry for the wording, cannot think of a better way to explain
Code currently trying. But all it is doing is making the 'ding' sound on enter.
Private Sub StoreNumberPT_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles StoreNumberPT.KeyDown
If e.KeyCode = Keys.Enter Then
If Trim(StoreNumberPT.Text) <> vbNullString Then
MsgBox("success")
Else
MsgBox("success")
End If
End If
End Sub
Private Sub textbox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles textbox1.KeyDown
If e.KeyCode = Keys.Enter Then
If Trim(textbox1.Text) <> vbNullString Then
' code
Else
'code
End If
End If
End Sub
or you can handle keydown event for each TextBox under one method like below
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles _
TextBox1.KeyDown, TextBox2.KeyDown
Dim txtbx = DirectCast(sender, TextBox)
If e.KeyCode = Keys.Enter And Trim(txtbx.Text) <> vbNullString Then
Select Case txtbx.Name
Case "TextBox1"
'your code when user type in TextBox1, as a sample
MsgBox(txtbx.Text)
Case "TextBox2"
'your code when user type in TextBox2
MsgBox(txtbx.Text)
End Select
End If
End Sub
use
Private Sub textbox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles textbox1.KeyDown,textbox2.KeyDown
If e.KeyCode = Keys.Enter Then
If sender.name="Textbox1"
If Trim(textbox1.Text) <> vbNullString Then
' code
Else
'code
End If
Elseif sender.name="Textbox2"
If Trim(textbox2.Text) <> vbNullString Then
' code
Else
'code
End If
End if
End if
End Sub
Hope this helps.

Need hints for completing code:

This question has been answered.
Do not want to leave it exposed.
Private Sub btnTTL_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles findzipButton.Click
Dim zipCode As String
'forgot
If (ListBox1.FindString(findzipButton.Text) >= 0) Then
ttlTextBox.Text = "$15"
ElseIf (ListBox2.FindString(findzipButton.Text) >= 0) Then
ttlTextBox.Text = "$20"
Else
MessageBox.Show("The zipcode was not found!")
End If
End Sub
End Class
So what I think your trying to do is match the input a user puts into a text box with a selection in the either ListBoxA or ListBoxB. I just tried this in VS 2012, and it seems to work in the way the problem above describes, but I'm only trying to find and display the shipping cost:
Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub ListBox1_Load(sender As Object, e As EventArgs) Handles Me.Load
ListBox1.Items.Add("60611")
ListBox1.Items.Add("60234")
ListBox1.Items.Add("56789")
ListBox1.Items.Add("23467")
ListBox1.Items.Add("60543")
ListBox1.Items.Add("60561")
ListBox1.Items.Add("55905")
ListBox1.Items.Add("89567")
ListBox2.Items.Add("50978")
ListBox2.Items.Add("78432")
ListBox2.Items.Add("98432")
ListBox2.Items.Add("97654")
ListBox2.Items.Add("20245")
End Sub
Private Sub btnFind_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFind.Click
Dim zipCode As String = txtZipCode.Text
If (ListBox1.FindString(zipCode) >= 0) Then
txtShipping.Text = "$15"
ElseIf (ListBox2.FindString(zipCode) >= 0) Then
txtShipping.Text = "$20"
Else
MessageBox.Show("The zipcode was not found!")
End If
End Sub
You were on the right track. What you needed to do was compare what the user actually input to the textbox to what was available in the Listbox. The FindItem() method will then result a Long. If it did find your search string it will build the shipping text box.

textbox.clear can somebody check where it is wrong?

Im making a textbox which wont allow the user to use spaces but rather then clear the textbox it simply deleats the last character (which is the space) my code dosnt work can sombody look at it please?
Private Sub Textbox1_keyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
Dim UNDONE_TEXT As String
If e.KeyCode = Keys.Space Then
UNDONE_TEXT = ((TextBox1.Text) - 1)
TextBox1.Clear()
TextBox1.Text = UNDONE_TEXT
MsgBox("Invalid character. No spaces Permited...")
End If
End Sub
this returns 3????
UPDATE:
Private Sub Textbox1_keyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
Dim UNDO_TEXT As String
If e.KeyCode = Keys.Space Then
UNDO_TEXT = TextBox1.Text
TextBox1.Clear()
TextBox1.Text = UNDO_TEXT
TextBox1.Text.TrimEnd()
MsgBox("Invalid character. No spaces Permited...")
End If
End Sub
only now the focus is on the entire textbox when returned after MSGbox?
UNDONE_TEXT = left(TextBox1.Text, len(TextBox1.text)-1)
I'm no VBA expert, but would TextBox1.Text.Trim() be what you're looking for? This removes spaces at the beginning and at the end of a string. Possibly, you could try TrimEnd(). I don't know if that exists in VBA, it does in VB.NET:
Private Sub Textbox1_keyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
Dim UNDO_TEXT As String
If e.KeyCode = Keys.Space Then
TextBox1.Text = TextBox1.Text.TrimEnd()
MsgBox("Invalid character. No spaces Permited...")
End If
End Sub