Is there anyway to change ComboBox key inputs in VB.NET? - vb.net

I have a combobox in my Userform. Whilst this is in focus, I am wanting to use the keyboard for WASD controls. However, I find that when I press WASD the Combobox is bringing up values beginning with these letters. Also, the arrow keys are cycling through the options as well.
Is there anyway to restrict these commands?
I have tried
Tool.AutoCompleteMode = AutoCompleteMode.None
This does not stop is doing this. Does anybody have any clue how I could access more specific controls to stop this kind of autofill happening?

If you have only the Combobox control in your form and no other input controls, such as textbox, you can try the following code:
Please set e.Handled = True in the KeyPress event of the Combobox.
Private Sub ComboBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ComboBox1.KeyPress
e.Handled = True
End Sub
If you have some input controls, you want to retain the editing function, and you want to not interfere with the KeyPress event of the form.
You could refer to the following steps.
(1) Please set Me.KeyPreview = False in the KeyDown event of each input control.
(2) Please set e.Handled = True in the KeyPress event of ComboBox1.
(3) When double-clicking the form to enter WASD, you can set ComboBox1.Focus().
Sample code:
Public Class Form1
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyBase.KeyPress
'edit your code
Dim offset As Integer = 10
If e.KeyChar = "a" Then
PictureBox1.Location = New Point(PictureBox1.Location.X - offset, PictureBox1.Location.Y)
ElseIf e.KeyChar = "w" Then
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - offset)
ElseIf e.KeyChar = "s" Then
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + offset)
ElseIf e.KeyChar = "d" Then
PictureBox1.Location = New Point(PictureBox1.Location.X + offset, PictureBox1.Location.Y)
End If
e.Handled = True
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_DoubleClick(sender As Object, e As EventArgs) Handles MyBase.DoubleClick
ComboBox1.Focus()
Me.KeyPreview = True
End Sub
Private Sub ComboBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ComboBox1.KeyPress
e.Handled = True
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
Me.KeyPreview = False
End Sub
End Class
Result:

Related

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

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

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

Detect keypress in datagridview

I am using following code to detect keypresses in a datagridview:
Private Sub DataGridView1_mt_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1_mt.EditingControlShowing
AddHandler e.Control.KeyDown, AddressOf cell_Keydown
End Sub
Private Sub cell_Keydown(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.Space And CheckBox3.Checked = True Then
e.Handled = True
InputSimulator.SimulateTextEntry("_")
End If
End Sub
basically I need to replace every space with an underscore. The code is working so far except for 2 problems:
1) e.handled does not seem to affect the output. There is Always a space In front of the underscore. how can I prevent it from typing it?
2) Each time i change cell a new handler is added, and if I am editing for example the fifth cell the result will be a space followed by 5 underscores. How can I avoid this?
Thanks
Solved this way:
Dim eventhandleradded As Boolean = False
Private Sub DataGridView1_mt_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1_mt.EditingControlShowing
If eventhandleradded = False Then
AddHandler e.Control.KeyDown, AddressOf cell_Keydown
eventhandleradded = True
End If
End Sub
Private Sub cell_Keydown(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.Space And CheckBox3.Checked = True Then
e.Handled = True
e.SuppressKeyPress = True
InputSimulator.SimulateTextEntry("_")
End If
End Sub

Enable and disable TextBox with a checkbox in visual basic

I've 6 TextBox and 6 CheckBox. Now I want to disable the TextBox1 with a CheckBox1 and reactivate it with the Same CheckBox.
How a can do it?
Edit1 15.55 14/02/2013
I have done so to solve my problem!
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
TextBox1.Enabled = False
ElseIf CheckBox1.Checked = False Then
TextBox1.Enabled = True
End If
End Sub `
This will work, just add more for the other check boxes
Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
TextBox1.Enabled = True
Else
TextBox1.Enabled = False
End If
End Sub
What this does: if checkbox1 is check, the checked_changed event fires and the code inside is ran. The if statement looks to see if the checkbox is checked or not. If it is checked, then it sets the textbox1 to enabled, if not it sets it to disabled. Be sure to set the enabled property to either enabled or disabled when you create your program. If you want it to be enabled from the start, that is the default....otherwise set it to disabled in its properties view.
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
TextBox1.Enabled = CheckBox1.Checked
End Sub
Take a look at this tutorial below. After, look at the checkbox control's events and choose the most fitting one. The property you will be changing on the textbox is Enabled.
http://www.youtube.com/watch?v=4PbUryXqZ50
This works if you have a layer built in that you can send objects behind (therefore hide things). I use this as a way to make text boxes and other items appear and disappear depending on other selections.
Private Sub checkbox_Click()
If (checkbox = True) Then
ActiveSheet.Shapes("textbox").ZOrder msoSendToFront
ActiveSheet.Shapes("textbox").ZOrder msoSendToFront
Else
ActiveSheet.Shapes("textbox").ZOrder msoSendToBack
ActiveSheet.Shapes("textbox").ZOrder msoSendToBack
End If
End Sub
This worked for me:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Enabled = False
If Not TextBox1.Enabled Then
TextBox1.BackColor = Color.White
End If
End Sub
Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
TextBox1.Enabled = True
Else
TextBox1.Enabled = False
End If
End Sub
End Class