Capture Datagridview cell kepress event - vb.net

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

Related

Add/modify handler in multiple ComboBoxes

I want to disable the mousewheel to prevent scrolling in the ComboBoxes.
For one ComboBox this works:
Private Sub CmbDienst_MouseWheel(sender As Object, e As MouseEventArgs) Handles CmbDienst.MouseWheel
Dim HMEA As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
HMEA.Handled = True
End Sub
But how can I add this to ALL ComboBoxes? There are a lot of them in the form.
I was looking for something like
Private Sub Combo_Mouse()
For Each c As Control In Me.Controls.OfType(Of ComboBox)()
'And then...?
Next
End Sub
Thanks!
It works. The problem I had was that the comboboxes are in several containers, such as Panels and Datagridviews. Then is "me.controls, etc" not enough.
So I finally made this out of it:
In the Form load:
EnumControls(Me)
In the Programm:
Private Sub ComboBoxes_MouseWheel(sender As Object, e As MouseEventArgs)
Dim hmea = DirectCast(e, HandledMouseEventArgs)
hmea.Handled = True
End Sub
Private Sub EnumControls(ByVal ctl As Control)
If ctl.HasChildren Then
For Each c As Control In ctl.Controls
For Each comboBox In c.Controls.OfType(Of ComboBox)()
AddHandler comboBox.MouseWheel, AddressOf ComboBoxes_MouseWheel
Next
EnumControls(c)
Next
End If
End Sub
It works. Suggestions are welcome!
Try this
Private Sub buttonHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
mwe.Handled = True
End Sub
and in FormLoad
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each c As Control In Me.Controls.OfType(Of ComboBox)()
'You cann acces to ComboBox her by c
AddHandler c.MouseWheel, AddressOf buttonHandler
Next
End Sub

VB.NET - Check if the key pressed is the same as the text in label

I am wondering if anyone can help me to do the above question.
Basically what i have is a label which has had a randomly generated letter placed into it.
What I want to do is to show a msgbox (just to show that it works for now) when the same key in the label is pressed. I have tried two methods but none seem to work, could anyone point me in the right direction? It doesn't seem like it would be that hard, i am just new to coding.
Private Sub speedtyping_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (e.KeyChar = lblletter1.Text.ToString) Then
MsgBox("WORKS")
ElseIf (e.KeyChar = lblletter2.Text) Then
MsgBox("words")
End If
Many thanks!
In order to handle the form's KeyPress event, you need to set it's KeyPreview property. You can do that in the Form Designer or in the form's Load event handler as below.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = lblletter1.Text(0) Then
MessageBox.Show("WORKS")
ElseIf e.KeyChar = lblletter2.Text(0) Then
MessageBox.Show("words")
End If
End Sub
End Class
Try this
Private Sub speedtyping_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (e.KeyChar.ToString = lblletter1.Text) Then
MsgBox("WORKS")
ElseIf (e.KeyChar.ToString = lblletter2.Text) Then
MsgBox("words")
End If
End Sub
Try this
Private Sub speedtyping_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (e.KeyChar.Trim() = lblletter1.Text.Trim()) Then
MsgBox("WORKS")
ElseIf (e.KeyChar.Trim() = lblletter2.Text.Trim()) Then
MsgBox("words")
End If
End Sub

How can I call keyDown event by passing arguments, Winforms Vb.net

The Following is a combo box keydown event
Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
If e.KeyCode = Keys.Enter Then
TextBox2.Text = ComboBox1.Text
TextBox2.Focus()
End If
End Sub
I would like to trigger same event from combobox_leave by passing 'enter key' I did as follows but not working, how to achieve this?
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1_KeyDown(Me, Keys.Enter)
End Sub
Why not just extract the method from the actual event?
Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
performAction(e.KeyCode);
End Sub
Private Sub performAction(e as Keys)
If e = Keys.Enter Then
TextBox2.Text = ComboBox1.Text
TextBox2.Focus()
End If
End Sub
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
performAction(Keys.Enter);
End Sub
You could also use the SendKeys.Send Method
When the user leaves the Combobox (like in your example),
You could set back the Focus to the combobox
and then use SendKeys.Send("{ENTER}") to trigger the enter keydown.
much like this:
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1.Focus()
SendKeys.Send("{ENTER}")
End Sub
However this prevents users from focusing to another component. To prevent this, you could use an if statement that if the user clicks or focuses on another component after focusing on the combobox, the user can still "leave" the combobox.
Your kind of approach is not advisable and this leads to a misunderstanding in the part of the user.
try this :
Private Sub ComboBox1_KeyDown(sender As Object, e As
keyEventArgs) Handles ComboBox1.KeyDown
Dim _KeyCode As Short
If e Is Nothing Then
_KeyCode = 13
Else
_KeyCode = Keys.Enter
End If
If _KeyCode = Keys.Enter Then
TextBox2.Text = ComboBox1.Text
TextBox2.Focus()
End If
End Sub
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs)
Handles ComboBox1.Leave
Dim keypress As System.Windows.Forms.KeyPressEventArgs
ComboBox1_KeyDown(sender, keypress)
End Sub

Visual Studio Last 4 lines are wrong and I donĀ“t know why

Public Class Form1
Private _zkouska1 As New Bitmap("C:\Users\w\Desktop\zkouska1.gif")
Private _zkouska2 As New Bitmap("C:\Users\w\Desktop\zkouska2.gif")
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
e.Graphics.DrawImageUnscaled(New Bitmap(_zkouska1), 0, 0)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyNewSub()
End Sub
Sub MyNewSub()
BackColor = Color.Red
TransparencyKey = BackColor
End Sub
Private Sub Example_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles Me.ControlAdded
AddHandler e.Control.MouseClick, AddressOf Example_MouseClick
End Sub
Private Sub Example_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
If True Then e.Graphics.DrawImageUnscaled(New Bitmap(_zkouska2), 0, 0) = True
End Sub
End Class
You haven't indicated specifically which lines are causing the issue, but the issue with this line:
If True Then e.Graphics.DrawImageUnscaled(New Bitmap(_zkouska2), 0, 0) = True
is that MouseEventArgs doesn't have a Graphics member. Perhaps the thing to do here is to use the Mouse_Clicked event to set a variable that keeps tracks of whether the mouse was clicked (and where), and use the Paint event to do the actual drawing.
So you would add a boolean variable to the class:
Private _clicked As Boolean
Change the MouseClicked event to something like this:
Private Sub Example_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
_clicked = True
End Sub
Add a Paint event and make that event so it's something like this:
Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
If _clicked Then
e.Graphics.DrawImageUnscaled(New Bitmap(_zkouska2), 0, 0)
End If
End Sub

Is it possible to detect a Form mouseclick from a User Control

I have created a User Control and would like to be able to detect when the user clicks on the Form.
I have seen this question which is related but the suggestion to use the the Leave event doesn't always do what I want because the focus doesn't necessarily change when the user clicks the Form (my control could be the only control on the Form in which case focus stays with my control).
Any ideas?
I want to be able to do something like this from within the User Control:
Private Sub ParentForm_Click(sender As Object, e As System.EventArgs) _
Handles Me.Parent.Click
End Sub
I would do it slightly differently:
Private _form As Control
Private Sub UserControl_ParentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.ParentChanged
If _form IsNot Nothing Then
RemoveHandler _form.Click, AddressOf ParentOnClick
End If
_form = Me.FindForm()
If _form IsNot Nothing Then
AddHandler _form.Click, AddressOf ParentOnClick
End If
End Sub
Private Sub ParentOnClick(ByVal sender As Object, ByVal e As EventArgs)
'...
End Sub
This gives it a little more resillience - if it is not a direct child of a Form, if its parent changes etc.
I figured out how to do this myself - for anyone interested I am doing the following:
Private _parentForm As Form
Private Sub UserControl_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
_parentForm = CType(Parent, Form)
AddHandler _parentForm.Click, AddressOf ParentForm_Click
End Sub
Private Sub ParentForm_Click(sender As Object, e As System.EventArgs)
debug.writeline("Parent form clicked")
End Sub