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

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.

Related

VB.NET 2010 - Windows Ding.wav playing when pressing Enter inside a TextBox

I have a program where you type something into a TextBox and press Enter, then it will add the text into a ListBox and erase the TextBox.
It does work!
However, when Enter is pressed, the C:\Windows\Media\Windows Ding.wav audio plays.
It's honestly just annoying...
Anyone know how to stop that?
Thanks!
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
ListBox1.Items.Add(TextBox1.Text)
TextBox1.Text = ""
End If
End Sub
Do you want to disable the beep after Enter pressed? You can try to set property SuppressKeyPress to True.
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
ListBox1.Items.Add(TextBox1.Text)
TextBox1.Text = ""
e.SuppressKeyPress = True
End If
End Sub

How to pass Click event handler as a parameter to another sub?

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.

Trying to use keyboard whilst text box is on screen

In Visual Basic, I try to move 2 Picture Boxes in my pong game using my keyboard ( 'W' and 'S' keys and up and down arrows).
They won't move if there are any buttons or text boxes on the screen, however they will when I remove the the button/text box. I think it might have to do with being able to use the arrow keys to navigate between them but I don't know how exactly to stop that from happening.
Using these events to move up, down,
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.Up
moveUp()
e.Handled = True
Case Keys.Down
moveDown()
e.Handled = True
End Select
End Sub
Private Sub Form1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs)
Select Case e.KeyCode
Case Keys.Up
moveUp()
Case Keys.Down
moveDown()
End Select
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
Select Case e.KeyCode
Case Keys.Up, Keys.Down
stopMoving()
e.Handled = True
End Select
End Sub
Private Sub moveUp()
Me.Text = "Moving Up"
End Sub
Private Sub moveDown()
Me.Text = "Moving Down"
End Sub
Private Sub stopMoving()
Me.Text = "Idle"
End Sub
you can also use the same events to handle up, down on the TextBoxes and Buttons. Add the handlers programmatically
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each c As Control In Me.Controls
AddHandler c.KeyDown, AddressOf Form1_KeyDown
AddHandler c.PreviewKeyDown, AddressOf Form1_PreviewKeyDown
AddHandler c.KeyUp, AddressOf Form1_KeyUp
Next
End Sub
Note: it will only add the handlers to controls directly on your form, not controls inside containers. Though, you can use an extension method to get all children, no matter how buried in containers. See https://stackoverflow.com/a/21388034/832052

How to suppress sound when opening a dialogform in vb.net

I have a form with a textbox, when pressing enter another form2 is opened.
How can I suppress sound when form2 is opened?
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
e.Handled = True
e.SuppressKeyPress = True
My.Forms.Form2.ShowDialog()
End If
End Sub
You could do this by changing the code slightly and moving it into the KeyPress event instead of KeyDown
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyKeyPress
If e.KeyChar = ChrW(Keys.Enter) Then
e.Handled = True
My.Forms.Form2.ShowDialog()
End If
End Sub

Disabling Alt+F4 function in key down event

How to prevent form being closed by alt+f4 keys in keydown event? problem i am facing is keeping ALT key pressed and pressing F4 closes form but i want to prevent form from closing. Below is my code
Private Sub frminstituteselect_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.Alt = True And e.KeyCode = Keys.F4 Then
e.Handled = True
End If
End Sub
Try like this
Private ALT_F4 As Boolean = False
Private Sub frminstituteselect_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Form1.FormClosing
If ALT_F4 Then
e.Cancel = True
Return
End If
End Sub
Private Sub frminstituteselect_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Form1.KeyDown
ALT_F4 = (e.KeyCode.Equals(Keys.F4) AndAlso e.Alt = True)
End Sub
or simply
Private Sub frminstituteselect_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Form1.FormClosing
e.Cancel = True
End Sub