Return the currently selected object - vb.net

The code says everything, if I press F1 and I'm already with Button1 selected, then it should select Button2 and vice-versa.
If keyData = Keys.F1 Then
If Button1.Select() = True Then
Button2.Select()
ElseIf Button2.Select() = True Then
Button1.Select()
Else
Button1.Select()
End If
End If
But the expression "Button2.Select() = True" doesn't return a value.

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyValue = Keys.F1 Then
If ActiveControl.Name = "Button1" Then
Button2.Select()
Else
Button1.Select()
End If
End If
End Sub
Actually, F1 is a poor choice because it is traditionally the "Help" key.

We can use form keydown (set form keypreview property to true) or use button previewkeydown like these:
Private Sub Button1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Button1.PreviewKeyDown
If e.KeyCode = Keys.F1 Then
Button2.Focus()
End If
End Sub
Private Sub Button2_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Button2.PreviewKeyDown
If e.KeyCode = Keys.F1 Then
Button1.Focus()
End If
End Sub

Related

Deselect combobox text on leaving using suggestappend

I have a databound ListItems combobox with AutoComplete.SuggestAppend
and would like to navigate out of the combobox to different controls using the up/down arrow keys rather than scrolling the items.
The issue is that if the text isn't completed the suggested text remains highlighted while the next control has focus.
Link to image example
Here is some code showing a simple example of what I am doing
Public Class Form1
Dim PreventCboBoxChanging As Boolean
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.DataSource = New List(Of String)(New String() {10, 11, 20, 30})
ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
End Sub
Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
If PreventCboBoxChanging = True Then
e.Handled = True
End If
PreventCboBoxChanging = False
End Sub
Private Sub ComboBox1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles ComboBox1.PreviewKeyDown
If e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then
PreventCboBoxChanging = True
TextBox1.Select()
End If
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then
ComboBox1.Select()
End If
End Sub
Private Sub ComboBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ComboBox1.Validating
Dim index As Integer = sender.FindString(sender.Text)
If index > -1 Then
sender.SelectedIndex = index
Else
e.Cancel = True
Me.Text = ""
Beep()
End If
End Sub
End Class
Is there any way to deselect the text?
I found the solution in another thread. Needed to turn off the Combobox AutoComplete Mode, change focus then reenable SuggestAppend mode under the PreviewKeyDown event.
Private Sub ComboBox1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles ComboBox1.PreviewKeyDown
If e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then
ComboBox1.AutoCompleteMode = AutoCompleteMode.None
TextBox1.Select()
ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
End If
End Sub
This should make it:
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1.SelectionLength = 0
End Sub

Event for if text cursor is in textbox

I am sure the answer is out there, but I am unsure how to word it...
So I am looking for an event or a way , if the user has clicked on textbox1 (and put their input in) they can press enter and it will do a task. But if they did that with textbox2, then it would do a different task.
Sorry for the wording, cannot think of a better way to explain
Code currently trying. But all it is doing is making the 'ding' sound on enter.
Private Sub StoreNumberPT_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles StoreNumberPT.KeyDown
If e.KeyCode = Keys.Enter Then
If Trim(StoreNumberPT.Text) <> vbNullString Then
MsgBox("success")
Else
MsgBox("success")
End If
End If
End Sub
Private Sub textbox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles textbox1.KeyDown
If e.KeyCode = Keys.Enter Then
If Trim(textbox1.Text) <> vbNullString Then
' code
Else
'code
End If
End If
End Sub
or you can handle keydown event for each TextBox under one method like below
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles _
TextBox1.KeyDown, TextBox2.KeyDown
Dim txtbx = DirectCast(sender, TextBox)
If e.KeyCode = Keys.Enter And Trim(txtbx.Text) <> vbNullString Then
Select Case txtbx.Name
Case "TextBox1"
'your code when user type in TextBox1, as a sample
MsgBox(txtbx.Text)
Case "TextBox2"
'your code when user type in TextBox2
MsgBox(txtbx.Text)
End Select
End If
End Sub
use
Private Sub textbox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles textbox1.KeyDown,textbox2.KeyDown
If e.KeyCode = Keys.Enter Then
If sender.name="Textbox1"
If Trim(textbox1.Text) <> vbNullString Then
' code
Else
'code
End If
Elseif sender.name="Textbox2"
If Trim(textbox2.Text) <> vbNullString Then
' code
Else
'code
End If
End if
End if
End Sub
Hope this helps.

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

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

How to handle KeyDown in VB.NET

Ok here's my dilemma. here's this code I have:
If e.KeyCode = Keys.A Then
TextBox1.AppendText("C, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
End If
Now when I enter this under Form1_KeyDown, visual basic thinks this:
'KeyCode is not a a member of 'System.EventArgs'
Now I've seen this code work before, but it isn't here. Any help?
Here's the full code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = Keys.A Then
TextBox1.AppendText("A, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
End If
If e.KeyCode = Keys.S Then
TextBox1.AppendText("C,")
PictureBox14.Visible = True
My.Computer.Audio.Play(My.Resources.D, AudioPlayMode.Background)
End If
End Sub
Not sure why you method definition declares e as EventArgs, but the fix is simply to make the parameter of type KeyEventArgs. This is because EventArgs (naturally) does not contain a property called KeyCode, but KeyEventArgs does!
Change your event handler method definition to the following:
Private Sub foo_KeyDown(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.A Then
TextBox1.AppendText("A, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
ElseIf e.KeyCode = Keys.S Then
TextBox1.AppendText("C,")
PictureBox14.Visible = True
My.Computer.Audio.Play(My.Resources.D, AudioPlayMode.Background)
End If
End Sub
It sounds like your method is using the wrong EventArgs. The Control.KeyDown event sends it as a System.Windows.Forms.KeyEventArgs. So your code should read as
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)
// code here
End Sub
I've found sometimes you need to provide the full object type similar to the below:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
MsgBox(e.KeyCode.ToString()) 'Message what the keycode
If (e.KeyCode = Keys.A) Then
MsgBox("e.KeyCode = Keys.A") 'Message that I've found the A
TextBox1.AppendText("C, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
End If
End Sub
Have you also tried testing this in a textbox on the form similar to the below (textbox in example is named TextBoxKeyTest)?
Private Sub TextBoxKeyTest_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBoxKeyTest.KeyDown
MsgBox(e.KeyCode.ToString())'Message what the keycode
If (e.KeyCode = Keys.A) Then
MsgBox("e.KeyCode = Keys.A")'Message that I've found the A
TextBox1.AppendText("C, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
End If
End Sub
This should work in VB 2010
Private Sub cmdWiden_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.Up Then Me.Top = Me.Top - 5
If e.KeyCode = Keys.Down Then Me.Top = Me.Top + 5
If e.KeyCode = Keys.Left Then Me.Left = Me.Left - 5
If e.KeyCode = Keys.Right Then Me.Left = Me.Left + 5
End Sub