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
Related
I know the question has been asked and answered many times, but what if none of the answers work? I'm using Visual Studio 2017 to rewrite a vb6 app that catches the key combination "ctrl+enter" to load a dialogue.
I catch the key combination just fine from a textbox called CourtName, but can't get rid of the annoying "ding" that goes with it.
I have googled for many hours but everywhere the answer is to use e.Handled and/or e.SuppressKeyPress, which I have done without success.
Here is my code:
Private Sub CourtName_KeyDown(sender As Object, e As KeyEventArgs) Handles CourtName.KeyDown
If e.KeyCode = Keys.Enter AndAlso e.Control Then
e.Handled = True
e.SuppressKeyPress = True
CourtsBtn.PerformClick()
End If
End Sub
The ding still persists, no matter whether the e.Handled and e.SuppressKeyPress statements are before or after the PerformClick() statement.
What magic am I missing?
Try replacing the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Multiline = True
MsgBox("Pressed!")
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyData = Keys.Control + Keys.Enter Then
TextBox1.Multiline = False
Button1.PerformClick()
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Multiline = True
End Sub
I've tested this code on mine, it works perfect!
Enjoy it!
I just tested this code and there was no ding, no matter what modifiers were used or not with Enter:
Public Class Form1
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
'Suppress Windows audio feedback.
e.Handled = True
e.SuppressKeyPress = True
End If
If e.KeyData = (Keys.Control Or Keys.Enter) Then
Button1.PerformClick()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Console.WriteLine("Button1_Click")
End Sub
End Class
Can you test the same code and see whether it works for you?
i am getting really annoyed by this piece of code for visual basic
please can you help. i have looked on YouTube and everywhere else even the vb website!!! i would really appreciate it if someone could help me out
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show("Hi!!")
Timer1.Start()
End Sub
Private Sub ProgressBar1_Click(sender As Object, e As EventArgs) Handles ProgressBar1.Click
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(1)
If ProgressBar1.Value = 100 Then
Timer1.Stop()
MsgBox("Jeff")
End If
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs)
End Sub
Private Function GetNumericUpDown1(v As Integer) As Boolean
End Function
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
If ( : ( ) Then
MessageBox.Show("Well Done!")
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
MessageBox.Show("!!Stopped!!")
Timer2.Stop()
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
ProgressBar2.Increment(0.6)
If ProgressBar2.Value = 100 Then
Timer2.Stop()
MsgBox("Jeff")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
MessageBox.Show("!!Started!!")
Timer2.Start()
End Sub
Private Sub SplitContainer1_Panel1_Paint(sender As Object, e As PaintEventArgs) Handles SplitContainer1.Panel1.Paint
End Sub
Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs)
End Sub
Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs)
End Sub
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
ProgressBar3.Increment(1)
If ProgressBar3.Value = 100 Then
Timer3.Stop()
End If
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Timer3.Start()
End Sub
Private Sub ProgressBar3_Click(sender As Object, e As EventArgs) Handles ProgressBar3.Click
End Sub
End Class
BTW:this is my first post so it is probably rubbish!!
I don't have enough points to comment.
First off, as others have said we can't recreate this since we have no idea what your Form looks like or what you are expecting it to do.
Secondly, you should turn on Option Strict in the Compile section of Project properties to avoid technical errors like ProgressBar2.Increment(0.6) since 0.6 isn't a valid integer.
I threw together a TabControl (which you never mentioned in the OP) with 3 tabs and the various buttons and progress bars you list in what seemed like a logical fashion to me and it ran just fine for what code you have. Clicked the button on each tab and each progress bar eventually got to 100%. I have no idea what else you were expecting.
If you remove all the empty subs and action listeners, it would be easier to detect the problem
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
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
i have a flyout as a login form, but whenever i presses the esc key the flyout will exit and i dont want that kind of thing so is there anyway to trap that esc key thingy in flyouts ? i did test this stuff but not of them works.
Private Sub ItemAdd_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
If login = True Then
MsgBox("haha")
End If
End Sub
Private Sub ItemAdd_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If login = True Then
MsgBox("haha")
End If
End Sub
Private Sub ItemAdd_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Space Then
MsgBox("haha")
End If
End Sub
OR HOW CAN I DISABLE THE ESC KEY in a FORM ?
I haven't tried this myself, but you give it a try:
Private Sub ItemAdd_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
e.Handled = True
End If
End Sub