Order of KeyEvents processing in Winforms with VB.NET - vb.net

I have subclassed form with 3 controls-NumericUpDown in it.
Here is main form' code:
Public Class Form1
Inherits cls_subform
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Private Sub NumericUpDown1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles NumericUpDown1.KeyDown
If e.KeyCode = Keys.Enter Then
Debug.Print "Control's ENTER is pressed"
End If
End Sub
End Class
Here is simplified frm_subform code:
Public Class cls_subform
Inherits System.Windows.Forms.Form
Public Sub New()
InitializeComponent()
End Sub
Protected Overrides Sub onKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = Keys.Enter Then
e.Handled = True
e.SuppressKeyPress = True
Debug.Print "Subclassed ENTER is pressed"
End If
MyBase.OnKeyDown(e)
End Sub
End Class
This obviously work propper.
But problem is that I would like to handle control's event handler NumericUpDown1_KeyDown FIRST. And then if it is not "handled" I would like to handle onKeyDown event handler on frm_subform.
If I set KeyPreview to False then I can catch control's event but then onKeyDown is not fired.
Is there a way to get desired functionality?
That FIRST will be fired control's _KeyDown handler and then form's onKeyDown handler?

Related

How to get keystroke

I want to go get keystrokes, and this code works. But when I add a button or textbox the code is not running. Can any one help me with this code:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If Asc(e.KeyChar) > 1 Then Label1.Text = e.KeyChar
End Sub
End Class
You need to set the Form's KeyPreview property to True, in which case the Form will raise those keyboard events

How to Set Enter is ok on Textbox in vb.net

I have a password textbox (Tbx1) and a Login button (Btn1). What code will i put where so that when the user
inputs password and presses enter, Btn1 clickevents will be exucuted?
Try this:
Private Sub TextBox1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
fun()
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
fun()
End Sub
Private Sub fun()
MessageBox.Show("Enter key pressed")
End Sub
on Form Misc Properties set the accept button to be Btn1. Equally, you can set a cancel button too to be executed whenever the user presses Esc button.
Code version (through the constructor):
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.AcceptButton = Btn1
End Sub

Automatic SelectAll on all textboxes and numericUpDown controls on gotfocus

I have such code to make all text in textbox selected on got_focus:
Private Sub myText_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles myText.GotFocus
myText.SelectAll()
End Sub
Is here a way in VB.NET to get that all TextBoxes and NumericUpDown controls selects his text on _GotFocus or _Enter without to explicitly set such behavior for every single control and no matter how this control gets a focus (keyboard, mouse or programmatic)?
Yes, there is and very simple.
Private Sub TextBox2_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox2.GotFocus
TextBox2.Select(0, TextBox2.Text.Length)
End Sub
Public Class MyTextBox
Inherits System.Windows.Forms.TextBox
Private _focused As Boolean
Protected Overrides Sub OnEnter(e As EventArgs)
MyBase.OnEnter(e)
If MouseButtons = MouseButtons.None Then
SelectAll()
_focused = True
End If
End Sub
Protected Overrides Sub OnLeave(e As EventArgs)
MyBase.OnLeave(e)
_focused = False
End Sub
Protected Overrides Sub OnMouseUp(mevent As MouseEventArgs)
MyBase.OnMouseUp(mevent)
If Not _focused Then
If SelectionLength = 0 Then
SelectAll()
End If
_focused = True
End If
End Sub
End Class

Capture Datagridview cell kepress event

Could any body kindly give me code example how to capture datagridview cell keypress event?
Datagridview_keypress does not help.
Thanks
As per Fco Navarro's answer, except that using e.Control does not always work because e is passed in to the EditingControlShowing event ByVal meaning any changes to the control (eg changing the .Text property) are NOT reflected in the DataGridView. If you need to do anything with the actual TextBox control in your event handler, you can use DataGridView1.EditingControl instead of e.Control.
Private WithEvents txtNumeric As New DataGridViewTextBoxEditingControl
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
txtNumeric = CType(DataGridView1.EditingControl, DataGridViewTextBoxEditingControl)
End Sub
Private Sub txtNumeric_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles txtNumeric.KeyPress
txtNumeric.Text = txtNumeric.Text.ToUpper()
End Sub
Private WithEvents txtNumeric As New DataGridViewTextBoxEditingControl
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
txtNumeric = CType(e.Control, DataGridViewTextBoxEditingControl)
End Sub
Private Sub txtNumeric_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNumeric.KeyDown
If (e.KeyData >= Keys.A And e.KeyData <= Keys.Z) Then
e.SuppressKeyPress = True
End If
End Sub
Try this:
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If Me.DataGridView1.CurrentCell.ColumnIndex = 0 And Not e.Control Is Nothing Then
Dim tb As TextBox = CType(e.Control, TextBox)
AddHandler tb.KeyDown, AddressOf TextBox_KeyDown
AddHandler tb.KeyPress, AddressOf TextBox_KeyPress
End If
End Sub
Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = Keys.Space Then
flag = True
End If
End Sub
Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
e.Handled = flag
flag = False
End Sub
Extracted from here.
I use the KeyUp event instead of KeyPress. The trick is to attach the handler event to the ActiveControl property of form when the cell has the active state.
Private Sub grid_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles grid.CellBeginEdit
AddHandler Me.ActiveControl.KeyUp, AddressOf Cell_KeyUp
End Sub
Private Sub Cell_KeyUp(sender As Object, e As KeyEventArgs)
Console.WriteLine(sender.Text) 'content of cell
End Sub
If you want to use also RemoveHandler method, you can add a global variable to the class (using this variable instead of Me.ActiveControl) and call RemoveHandler for example into the CellEndEdit event of DataGridView.
if your using your gridview in asp.net (Website) its not possible. there is no keypress Event.
its possible indeed but you have to use JavaScript to make a postback on every keychanged (Client side). but this is not a nice programming style and should not be used (you are getting rly many postbacks which are slowing down the complete System).
if your using Windows forms: have a look at the answer from sysdragon.
hope this helps a bit.
best regards, noone
the following code work perfectly:
Private WithEvents txtmontant As DataGridViewTextBoxEditingControl
Private Sub DGdivers_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DGdivers.EditingControlShowing
If DGdivers.CurrentCell.ColumnIndex = 1 Then
Dim txtmontant = CType(e.Control, DataGridViewTextBoxEditingControl)
AddHandler txtmontant.KeyPress, AddressOf txtmontant_keypress
Else
RemoveHandler txtmontant.KeyPress, AddressOf txtmontant_keypress
End If
End Sub
Private Sub txtmontant_keypress(sender As Object, e As KeyPressEventArgs) Handles txtmontant.KeyPress
If e.KeyChar = vbCr Then
DGdivers.Rows.Add()
Exit Sub
End If
If e.KeyChar = vbBack Then
Exit Sub
End If
If InStr("0123456789.,", e.KeyChar) = 0 Then
e.KeyChar = ""
End If
End Sub

Show on hover issue

So I have a simple form with a button on it. On the form's MouseEnter event I am setting the button to visible. On the form's MouseLeave event I'm setting the button to hidden. In effect, only when you hover over the form should the button be seen. The problem is that when you put the cursor over the button it disappears. Even if the button is directly in the center of the form it still exhibits this same behavior.
Is there a solution other than putting MouseEnter/Exit events on the button and everything else inside the form?
Public Class VerticalStrip
Private Sub VerticalStrip_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
btnAdd.Visible = False
End Sub
Private Sub VerticalStrip_MouseEnter(sender As Object, e As System.EventArgs) Handles Me.MouseEnter
btnAdd.Visible = True
End Sub
Private Sub VerticalStrip_MouseLeave(sender As Object, e As System.EventArgs) Handles Me.MouseLeave
btnAdd.Visible = False
End Sub
End Class
Yes; in the MouseLeave event, first check if the mouse has in fact left the form:
Public Class VerticalStrip
Private Sub VerticalStrip_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
btnAdd.Hide()
End Sub
Private Sub VerticalStrip_MouseEnter(sender As Object, e As System.EventArgs) Handles Me.MouseEnter
btnAdd.Show()
End Sub
Private Sub VerticalStrip_MouseLeave(sender As Object, e As System.EventArgs) Handles Me.MouseLeave
If Not Me.ClientRectangle.Contains(Me.PointToClient(Windows.Forms.Cursor.Position)) Then
btnAdd.Hide()
End If
End Sub
End Class