How to pass Click event handler as a parameter to another sub? - vb.net

In VB.Net I’m writing a program but I don’t know how to pass Click event handler as a parameter to another sub and how to call it from there. Can I do that? If yes, how?
E.g.: I have the following code. On two forms I have 1 ListView and 1 Button. If I press the button, it calls the click event of the button. In addition, if I press CTRL+P in the ListView, it calls the same event. I do it on both forms.
On form1:
Private Sub MyPrintButton1_Click(sender As Object, e As EventArgs) Handles MyPrintButton1.Click
' Print MyListView1 data...
End Sub
Private Sub MyListView1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyListView1.KeyUp
' CTRL+P = Print
If e.Modifiers = Keys.Control AndAlso e.KeyCode = Keys.P Then
e.Handled = True
MyPrintButton1_Click(sender, e)
End If
End Sub
On form2:
Private Sub MyPrintButton2_Click(sender As Object, e As EventArgs) Handles MyPrintButton2.Click
' Print MyListView2 data...
End Sub
Private Sub MyListView2_KeyUp(sender As Object, e As KeyEventArgs) Handles MyListView2.KeyUp
' CTRL+P = Print
If e.Modifiers = Keys.Control AndAlso e.KeyCode = Keys.P Then
e.Handled = True
MyPrintButton2_Click(sender, e)
End If
End Sub
I want to create only one sub in a different (common) file and want to call that passing the variables sender & e, and the event handler of the two Click events.
In this case the two KeyUp events would be the same, only the name of the passed event would be different:
On form1:
Private Sub MyListView1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyListView1.KeyUp
' Handle
AllListViews_KeyUp(sender, e, AddressOf MyPrintButton1_Click)
End Sub
On form2:
Private Sub MyListView2_KeyUp(sender As Object, e As KeyEventArgs) Handles MyListView2.KeyUp
' Handle
AllListViews_KeyUp(sender, e, AddressOf MyPrintButton2_Click)
End Sub
And the common sub would be similar like this:
Public Sub AllListViews_KeyUp(sender As Object, e As KeyEventArgs, PassedPrintButton_Click As ???? Of(????))
' CTRL+P = Print
If e.Modifiers = Keys.Control AndAlso e.KeyCode = Keys.P Then
e.Handled = True
PassedPrintButton_Click(sender2, e2 ????)
End If
End Sub
So: How do I need to pass the events as parameter and how to call the event? When I call the PassedPrintButton_Click event, how can I pass its own sender and e parameters (sender2, e2)? And how and where can I declare them?)
And, when I pass the address of MyPrintButton1_Click and addressMyPrintButton2_Click, from where will the sub know the click event’s sender and e parameters?
Thanks is advance.

Related

keypress event instead click in vb.net

I'm trying to implement some events using keys instead of clicks, I know that this code:
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
My.Computer.Audio.Play("D:\VisualStudio\JuegoDIF\sonidos\sonidoPerro.wav")
End Sub
is playing the sound when i click the picturebox2, but how can you do this using a key ? example:
presing the key "f5" should play the same sound as the click event
so far i tried keyDown like this:
Private Sub PictureBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles PictureBox2.KeyDown
If e.KeyCode.Equals(Keys.F5) Then
My.Computer.Audio.Play("D:\VisualStudio\JuegoDIF\sonidos\sonidoPerro.wav") '
End If
End Sub
But it's not working, and the keypress im not sure how to implement it:
Private Sub PictureBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles
PictureBox2.KeyPress
End Sub
How can I do this implementation ?
Set the form properties to Key Preview to True
Under the Form KeyDown Event
Private Sub frm_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
Handles MyBase.KeyDown
If e.KeyCode = Keys.F1 Then
'Enter your Code here
End If
If e.KeyCode = Keys.F2 Then
'Enter Code here
End If
End Sub
This method should be the easiest one

Specific keyboard characters to call specific button actions

When I start my program, and push the specified keys to call my button commands, it does not do anything.
The focus remains on one of the buttons and nothing occurs.
I've tried multiple codes from using KeyDown to KeyPress to codes that include vbKey.
I am very new to vb, so it is very likely that I just do understand what I am doing. :(
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = "/" Then
Call Button1_Click(sender, e)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnExit.Click
'Close Program
Me.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPMN.Click
'Add +1 to PMN Textbox (txtPMN)
txtPMN.Text = (Val(txtPMN.Text) + 1).ToString()
End Sub
End Class
I would like to simulate the clicking of certain buttons (activate the button code) when I press specific keys on the keyboard. For example: If I press "/" I would like Button1_Click to activate as if I clicked the button with the mouse. Then, if I push my next key ".", I would like Button2_Click to activate.
I do not want to use modifiers like: SHIFT, CTRL, ALT
Please try with keychar like this:
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = "/" Then
Call Button1_Click(sender, New EventArgs)
End If
end sub
when use keydown, the keycode for "/" key is OEM, depend the keyboard type, so char(e.keycode) may be not "/"

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.

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

VB.Net detect keydown on control before keydown on form event

I have enabled the KeyPreview option on the form and on the keydown of the form I am detecting the Escape keydown to popup a msgbox requesting whether the user wants to exit from the application.
But the problem is, I have one textbox that needs to clear its contents on Escape keypress rather than asking whether the user needs to exit. And I tried using the keydown event on this control but the form keydown event occurs before the control keydown (I want this to happen the other way around).
Code on the Textbox
Private Sub txtAmount(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtAmount.KeyDown
If e.KeyCode = Keys.Escape Then
'//Escape keypress
MsgBox("TEXTBOX ESCAPE")
sender.Text = ""
End If
End Sub
Code used on the Main menu Form
Private Sub frmMainMenu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then Application.Exit()
End Sub
I can't use the previewkeydown either because the MaskTextBox doesn't raise the previewkeydown event..
Check if txtAmount is focused in the Form_KeyDown:
Private Sub txtAmount_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtAmount.KeyDown
If e.KeyCode = Keys.Escape Then
'//Escape keypress
MsgBox("TEXTBOX ESCAPE")
sender.Text = ""
End If
End Sub
Private Sub frmMainMenu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If txtAmount.Focused Then Return
If e.KeyCode = Keys.Escape Then Application.Exit()
End Sub
Edit: Alternatively, if you don't want to specify each control individually, you could do it by type:
If TypeOf Me.ActiveControl Is MaskTextBox Then Return
Or even more simpler, change your Form_KeyDown-Event to a KeyUp Event.
Public Class frmMainMenu
Private KeyHandled As Boolean
Private Sub txtAmount_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtAmount.KeyDown
If e.KeyCode = Keys.Escape Then
sender.Text = ""
KeyHandled = True
End If
End Sub
Private Sub frmMainMenu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
KeyHandled = False
End Sub
Private Sub frmMainMenu_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If KeyHandled Then Return
If e.KeyCode = Keys.Escape Then Application.Exit()
End Sub
End Class
Here is my code
Private Sub When_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, MyBase.KeyDown
If sender.name = "Your form name" Then
If e.KeyCode = Keys.Escape And Not TextBox1.Focused Then
MsgBox("me")
End If
ElseIf sender.name = "your textBox name" Then
If e.KeyCode = Keys.Escape Then
MsgBox("txt")
End If
End If
End Sub
I hope this can help you