How can I call keyDown event by passing arguments, Winforms Vb.net - 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

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

Key Press to Close Form VB.net

So I've been working on this project to autorun on my flash drive, whenever it gets plugged it. It just displays my contact info and a little message, so I can get it returned.
Because I want people to actually read the message, the close button is disabled. You have to check a little box, and then hit the "OK" button.
Of course, I don't want to do this whenever I plug in my own flash drive. I'm trying to make a keyboard shortcut to close it automatically. I saw this post here but it didn't work for me. Here's the code from that post:
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.KeyCode = Keys.X Then
Application.Exit()
End If
End Sub
Any help is very appreciated. Thanks!
Also, if you think my code is sloppy and want to clean it up, here it is.
Public Class Form1
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonOK.Click
End
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
ButtonOK.Enabled = True
Else
ButtonOK.Enabled = False
End If
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.KeyCode = Keys.X Then
End
End If
End Sub
End Class
Firstly, NEVER EVER use End. That is just plain bad under any circumstances.
What you should be doing is testing the Checked value of your CheckBox in the FormClosing event handler and cancelling if and only if it's not checked. Logically, you should then check the CheckBox and call Close on the form when you detect that desired key combination.
Actually, I'd probably not call Close either, but rather call PerformClick on the Button. That way, the key combination is guaranteed to do exactly the same thing as clicking the Button.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonOK.Click
Close()
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
ButtonOK.Enabled = CheckBox1.Checked
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.KeyCode = Keys.X Then
CheckBox1.Checked = True
ButtonOK.PerformClick()
End If
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = Not CheckBox1.Checked
End Sub

Button.Click event doesn't fire after TextBox.Leave event

I use the below code to verify if the text into a TextBox was changed and ask for saving changes:
Private TBoxTxtBefore As String = String.Empty
Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
Try
TBoxTxtBefore = CType(sender, TextBox).Text
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
Try
If CType(sender, TextBox).Text <> TBoxTxtBefore Then
Dim SN As MsgBoxResult = MsgBox("Save changes?", vbYesNo, "Save Changes")
If SN = vbYes Then Btn_SaveChanges.PerformClick()
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
But when I click a button (for example button1) while the cursor is inside TextBox1, only TextBox1.Leave event was raised.
How can I have Button?.click event raise after TextBox1.Leave event?
How can I have button1.click event raise after TextBox1.Leave event?
If TextBox1 has the focus and you click a button the leave event will fire before the click event. To test click in Texbox1, then click Button1.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Debug.WriteLine("B1C")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Debug.WriteLine("B2C")
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
Debug.WriteLine("TBL")
Button2.PerformClick()
End Sub
Try to clarify your question please.
edit: This recreates the problem I think you are having,
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Debug.WriteLine("B1C")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Debug.WriteLine("B2C")
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
MessageBox.Show("FOO")
Debug.WriteLine("TBL")
Button2.PerformClick()
End Sub
Did some searching and found that the Message box causes pending focus events to be lost.
Thanks to dbasnett's hints I was able to get the name of the control that fired the Leave event, so to call it (if it's a button) after the Message Box was showed:
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
Try
Dim FiringCtrl As Control = Me.Activecontrol
If CType(sender, TextBox).Text <> TBoxTxtBefore _
AndAlso FiringCtrl.Name <> "Btn_SaveChanges" Then
Dim SN As MsgBoxResult = MsgBox("Save changes?", vbYesNo, "Save Changes")
If SN = vbYes Then Btn_SaveChanges.PerformClick()
If TypeOf FiringCtrl Is Button Then
DirectCast(FiringCtrl, Button).PerformClick()
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Anyway Plutonix showed me a better solution here
You can call multiple events one after the another. Your TextBox1_Leave event could trigger Button1_click event.
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
'Take the control to another sub. Pass the sender control and the event details as arguments
Call Button1_Click(sender,e)
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'Do your stuff here
End Sub
Hope this helps.

How do you load an image to form from file via keydown?

I am trying to make a super basic program that involves a picture and sound popping up every time I click F4. I have the background of the program set to green, because I am going to be using it as a green screen for the picture. I don't have much experience with VB, but since I couldn't find a program to do this on the web, I decided to take a swing and try to make it myself. (Failed...) Anyways, this is what I got so far.
Public Class Form1
Private Sub Form1_KeyPress(KeyAscii As Integer)
If (Chr(KeyAscii) = "115") Then Form1.Picture = loadpicture("directory")
End Sub
End Class
Note: "Directory" is not what I have in loadpicture().
Try this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True 'This enable the key event on the form (me).
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.F4 Then Me.BackgroundImage = Image.FromFile("C:\image.jpg")
End Sub
This is the final code that also includes an audio clip that plays when the key is pressed as well!
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True 'This enable the key event on the form (me).
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F4 Then Me.BackgroundImage = Image.FromFile("C:\image.jpg")
If e.KeyCode = Keys.F4 Then My.Computer.Audio.Play("C:\audio.wav", AudioPlayMode.Background)
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.F4 Then Me.BackgroundImage = Nothing
End Sub
End Class

Combobox plus String name

Hello i have comboboxes with the name ("Combobox1", "Combobox1a", "Combobox2", "Combobox2a", "combobox3" "Combobox3a"). When the selectedindex in combobox1 changed, combobox1a will add 4 items. The same with combobox2 and 2a, combobox3 and 3a. When i do this i have to code it in combobox1, combobox2 and combobox3. So ill try to make a method so that when the selectedindexchange in combobox it will call this method.
heres my code.
Dim cmbName as String = ""
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
cmbName = "1"
cmbSelectedIndexChanged()
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
cmbName = "2"
cmbSelectedIndexChanged()
End Sub
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
cmbName = "3"
cmbSelectedIndexChanged()
End Sub
Private Sub cmbSelectedIndexChanged()
"combobox" & cmbName.Items.add("data1")
"combobox" & cmbName.Items.add("data2")
"combobox" & cmbName.Items.add("data3")
"combobox" & cmbName.Items.add("data4")
End Sub
I just want to make my code smaller so that everytime my combobox index change it will only call the method.
You can find the other combo in your controls collection like this
combo = CType(Me.Controls.Find("combobox1", True)(0), ComboBox)
You mean something like this?
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
cmbSelectedIndexChanged("1")
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
cmbSelectedIndexChanged("2")
End Sub
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
cmbSelectedIndexChanged("3")
End Sub
Private Sub cmbSelectedIndexChanged(cmbName as string)
Dim Combo as ComboBox = CType(Me.Controls.Find("combobox" & cmbName), ComboBox)
Combo.Items.add("data1")
Combo.Items.add("data2")
Combo.Items.add("data3")
Combo.Items.add("data4")
End Sub