Form keydown event and textbox keydown event - vb.net

Private Sub AccountType_KeyDOWN(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Return Then
ProcessTabKey(True)
End If
End Sub
Private Sub anytextbox_Enter(sender As Object, e As EventArgs) Handles Twitter.Enter, Email.Enter, PhoneNoOffice.Enter, PhoneNoHome.Enter, PhoneNoMobile.Enter, EName.Enter, AName.Enter, IDCode.Enter, ExpiryDate.Enter, BankAccount.Enter
Dim txt As TextBox = DirectCast(sender, TextBox)
stroldvalue = txt.Text
End Sub
Private Sub anytextbox_KeyDown(sender As Object, e As KeyEventArgs) Handles Twitter.KeyDown, Email.KeyDown, PhoneNoOffice.KeyDown, PhoneNoHome.KeyDown, PhoneNoMobile.KeyDown, EName.KeyDown, AName.KeyDown, IDCode.KeyDown, ExpiryDate.KeyDown, BankAccount.KeyDown
Dim txt As TextBox = DirectCast(sender, TextBox)
If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab Then
If txt.Text <> stroldvalue Then
connectunauth()
cmd = New SqlCommand("Insert into changes values(#id,#txtname,#oldval,#newval)", Variables.UnauthCon)
cmd.Parameters.AddWithValue("#id", CustomerNo.Text)
cmd.Parameters.AddWithValue("#txtname", txt.Name)
cmd.Parameters.AddWithValue("#oldval", stroldvalue)
cmd.Parameters.AddWithValue("#newval", txt.Text)
cmd.ExecuteNonQuery()
UnauthCon.Dispose()
NOCHANGE = True
End If
End If
End Sub
In the above code i face the following error:
On entry into one textbox, the value is saved in stroldvalue.
On pressing enter after changing in the above, the form_keydown event triggers first, which runs the anytextbox_enter event causing the stroldvalue to be overwritten by the next textbox value. and then the textbox keydown event triggers.
I tried keypress event for the form, but keypress event does not capture enter key press on combobox.
What should i do so that the keydown of form triggers after textbox keydown event.

Related

Clear focused textbox in multiple textboxes in vb.net using delete key

I want a good example of clearing focused textbox among multiple textboxes using delete key. Please help me providing vb.net code.
The public sub is as such :
Public Sub EmptyTxt(ByVal Frm As Form)
Dim Ctl As Control
For Each Ctl In Frm.Controls
'If TypeOf Ctl Is TextBox Then Ctl.Text = ""
Next
End Sub
Then I call this sub from delete keydown event. But it clears all textboxes rather than clearing the focused one.
There's an easier way to do that.
Simply assign all the textbox keyDown events to the same handler. In that handler, cast sender to a textbox and clear it.
Private Sub Form1_Load (sender As Object, e As EventArgs) Handles MyBase.Load
For Each tb As TextBox In Controls.OfType(Of TextBox)
AddHandler tb.KeyDown, AddressOf TbKeyDown
Next
End Sub
Private Sub TbKeyDown (sender As Object, e As KeyEventArgs)
Dim tb = CType(sender, TextBox)
If e.KeyCode = Keys.Delete Then
tb.Clear()
End If
End Sub

vb.net can't use button.PerformClick after control Validated

in the form, i need check the textbox detail,so i use the Validated method.i also have a button to save form detail.and the button bind F10.
the text box Validated method
Private Sub txtReportCD_Validated(sender As Object, e As EventArgs) Handles txtReportCD.Validated
If txtReportCD.Text <> String.Empty Then
cboReportName.Value = txtReportCD.Text.Trim
Else
CommonMsg.showMsg("the CD not be empty")
End If
End Sub
the form keydown
Private Sub form_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.Control AndAlso e.Shift Then
Exit Sub
End If
Select Case e.KeyCode
'save data
Case Keys.F10
ClickButton(Me.btnSaveData, e)
e.Handled = True
End Select
End Sub
the ClickButton method
Private Sub ClickButton(ByVal btn As ButtonKdcCtrl, e As KeyEventArgs)
e.Handled = True
If e.Alt OrElse e.Control OrElse e.Shift Then
Exit Sub
End If
If btn.Enabled Then
btn.PerformClick()
End If
End Sub
the btnSaveData method
Private Sub btnSaveData_Click(sender As Object, e As EventArgs) Handles btnSaveData.Click
CommonMsg.showMsg("begin save data")
'do save form data
End Sub
when focus in txtReportCD,and i press F10 key. the form first do [btn.PerformClick], then do [txtReportCD_Validated] twice,last do [btnSaveData_Click].
but when focus in txtReportCD,and i click btnSavaData button.
the form first do [txtReportCD_Validated] once and not do [btnSaveData_Click].
how can i do,make when i press F10,the form do [txtReportCD_Validated] once
and the [btnSaveData_Click] not be call
PS: the txtReportCD text always empty.

Prevent raising Click event on RadioButton while moving from one to another

I have a Form with a Panel containig 5 RadioButton
I've only handled the Click event of all RadioButtons.
When I use arrow keys to move from a RadioButton to another, the Click event is raised at each move.
Is it possible to only Select the RadioButton and Click it using SpaceBar?
I would you use the Form.KeyDown event of the parent form and do something like this:
Private Sub Form1_KeyPress(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
REM Check if space is the button pressed
If e.KeyCode = Keys.Space Then
REM See if a Radio Button has focus to know which was selected
If RadioButton1.ContainsFocus Then
RunMyCode(RadioButton1)
ElseIf RadioButton2.ContainsFocus Then
RunMyCode(RadioButton2)
End If
End If
End Sub
''' <summary>
''' Method which executes the code you want run when a radio button is selected
''' </summary>
''' <param name="rdButtona"></param>
Private Sub RunMyCode(ByRef rdButtona As RadioButton)
REM Check which radio button was selected and execute the apprpriate code
If rdButtona.Equals(RadioButton1) Then
MsgBox("Radio Button 1")
ElseIf rdButtona.Equals(RadioButton2) Then
MsgBox("Radio Button 2")
End If
End Sub
To prevent the RadioButton from being checked, handle the RadioButton.Click event and uncheck the RadioButton using radioButton1.checked = false. If you handle the RadioButton.CheckChanged event, radioButton1.checked = false will raise another CheckChanged event which we don't need. I would also do something there to show which radio button has been selected. In My example I change the text color but you can do whatever you want.
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
RadioButton2.Checked = False
ResetColors()
RadioButton2.ForeColor = Color.DarkRed
End Sub
Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
RadioButton3.Checked = False
ResetColors()
RadioButton3.ForeColor = Color.DarkRed
End Sub
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
RadioButton1.Checked = False
ResetColors()
RadioButton1.ForeColor = Color.DarkRed
End Sub
Private Sub ResetColors()
RadioButton2.ForeColor = Color.Black
RadioButton1.ForeColor = Color.Black
RadioButton3.ForeColor = Color.Black
End Sub
Of course the code could be simplified, but this is the general idea.
The following code runs RadioButtons Click Event only on mouse click (or on spacebar press) and not on arrow keys press.
It also gives focus to RadioButton selected with Arrow Key
Dim HandleRBtnClick As Boolean = True
Private Sub RadioButtons_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles RadioButton1.Click, RadioButton2.Click, RadioButton3.Click, RadioButton4.Click
If Not HandleRBtnClick Then
With CType(sender, RadioButton)
.Checked = False
.Focus()
End With
Exit Sub
End If
'Code for RadioButton Checked (on mouse click or spacebar pressed)
Me.TextBox1.Text = CType(sender, RadioButton).Name
End Sub
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If keyData = Keys.Left OrElse
keyData = Keys.Right OrElse
keyData = Keys.Up OrElse
keyData = Keys.Down Then
HandleRBtnClick = False
Else
HandleRBtnClick = True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function

Can't get KeyDown event on Tabpage

I have a TabControl with several TabPages, each containing a series of PictureBox. I want to be able to navigate with the arrow keys inside those TabPages. I tried to give .Focus() to the TabPage when the user selects it, and then trap the PreviewKeyDown and KeyDown events, but it does not work. I've put a breakpoint at the PreviewKeyDown event handler, and it is not event reached.
Here is the activation of the TabPage :
Sub TilesetsTab_Selected(ByVal sender As Object, ByVal e As TabControlEventArgs) Handles TilesetsTab.Selected
Dim myTab As TabControl = CType(sender, TabControl)
If myTab.TabCount = 0 Then Exit Sub
Active.Tileset = Project.Tileset(CInt(myTab.SelectedTab.Tag))
Active.Tileset.Tab.Focus()
End Sub
And here is the Key event handlers
Sub Tab_KeyPreviewDown(ByVal sender As Object, ByVal e As PreviewKeyDownEventArgs) Handles Tab.PreviewKeyDown
If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Or e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down Then e.IsInputKey = True
End Sub
Sub Tab_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Tab.KeyDown 'BUG
Dim TilesWidth As Integer = Me.Tab.Width \ Me.ScreenSize
Select Case e.KeyCode
Case Keys.Left
If Active.Tile.Index = 0 Then Exit Sub
Me.Tile(Active.Tile.Index - 1).Selected()
Case Keys.Right
If Active.Tile.Index = Project.Tileset.Count - 1 Then Exit Sub
Me.Tile(Active.Tile.Index + 1).Selected()
Case Keys.Up
If Active.Tile.Index < TilesWidth Then Exit Sub
Me.Tile(Active.Tile.Index - TilesWidth).Selected()
Case Keys.Down
If Active.Tile.Index + TilesWidth > Active.Tileset.Count - 1 Then Exit Sub
Me.Tile(Active.Tile.Index + TilesWidth).Selected()
End Select
End Sub
Why can't I fire a KeyDown event on the TabPage ?
I don't know why the answer posted yesterday by someone (and the comments I made) were deleted, but it was said that TabPages could not have focus, and that it is restricted to TabControl. Following that advice, I shifted the PreviewKeyDown and KeyDown event handlers to TabControl events and it worked.
I'm losing some of the arrow navigation between TabPages, but it suits me.

KeyDown Event to next row

I created this form in VS
The NumPlace TextBox is intalized to 1 automatically, I want when I press on the Enter key to increment the NumPlace automatically in the next row.
This is the code I wrote :
Private Sub DataGridView1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
If e.KeyCode = Keys.Enter Then
MsgBox("Enter key is pressed !")
End If
End Sub
But It doesn't work it only works when I'm selecting some row end press on "Enter" but when I change the value of the ComboBox or when I select the NumPlace TextBox and then I press on Enter the event doesn't work.
You need to check ColIndex and RowIndex......................
Dim currCell As DataGridViewCell = DataGridView1.CurrentCell
The DataGridView1_KeyDown event will only fire when the focus is on DataGridView1. try handling Form1_KeyDown Event instead.
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Enter Then
MsgBox("Enter key is pressed !")
End If
End Sub
However, you should set Form1 KeyPreview property to True in the Form Designer or in Form Load so that the form receives key events before the event is passed to the control that has focus.
see this for more info
The solution is to use the event KeyUp instead of KeyDown