I've got a combobox, which the user can type into to filter out results.
To implement this, I've set the dropDownStyle to DropdownList and set the KeyUp event as follows
Private Sub cbRPP_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Handles cbRPP.KeyUp
If boredUserProtection Then Return
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
e.Handled = True
Return 'Boss doesn't want enter to do anything
End If
ComboKeyPressed()
End Sub
Private Sub cbRPP_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles cbRPP.SelectionChangeCommitted
If boredUserProtection Then Return 'boredUserProtection = a flag to prevent the user from typing into the box once they've entered some text
Try
boredUserProtection = True
menuButton.Select()
TooltipNotify("Loading")
cbRPP.Text = String.Empty
LoadRegion()
menuButton.Select()
Catch ex As Exception
ErrorReporter.Program.ReportError(ex, "RPPCalibator", "Handled Gracefully")
Finally
boredUserProtection = False
End Try
End Sub
The problem is that my boss has now asked me to prevent any action from occuring when he presses the enter key.
I've attempted to achieve this by intercepting the keypress and suppressing if enter, but it seems that this doesn't work when dropdownStyle=dropdownList
Private Sub cbRPP_KeyPress(sender As Object, e As KeyPressEventArgs) Handles cbRPP.KeyPress
If e.KeyChar = vbCrLf Then
e.Handled = True
Return 'Boss doesn't want enter to do anything
End If
End Sub
Private Sub cbRPP_KeyDown(sender As Object, e As KeyEventArgs) Handles cbRPP.KeyDown
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
e.Handled = True
Return 'Boss doesn't want enter to do anything
End If
End Sub
I'm just wondering if anyone can shed any light on how I can prevent any events happening on enter keyprss, as the one piece of advice I can readily find on Google (intercepting KeyDown or KeyUp) don't seem to work.
(I'm a pragmatist, so whatever form of solution you have would be much appreciated)
Related
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:
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
I am working on a VB.Net project and am trying to set up a DataGridView so that users can edit data. I'd like to set it up so that when a user finishes editing a cell, either through navigating to a new cell, or by pressing the Enter key, something happens.
I started by creating a method to store the old data that was in the cell:
Private oldCellVal As String = ""
Private oldCellRow As Integer
Private oldCellCol As Integer
Private Sub dg_CellBeginEdit(ByVal sender As Object, ByVal e As DataGridViewCellCancelEventArgs) Handles dg.CellEndEdit
oldCellVal = dg.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
oldCellRow = e.RowIndex
oldCellCol = e.ColumnIndex
End Sub
Now my next logical step was to create a KeyDown event for the DataGridView
Private Sub dg_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles dg.KeyDown
If e.KeyCode <> Keys.Enter Then
If dg.IsCurrentCellInEditMode() Then
If MessageBox.Show("Would you like to cancel editing?", "Confirmation", MessageBoxButtons.YesNoCancel) = DialogResult.Yes Then dg.CurrentCell.Value = oldCellVal
End If
ElseIf e.KeyCode = Keys.Enter Then
If MessageBox.Show("Would you like to save these changes?", "Confirmation", MessageBoxButtons.YesNoCancel) = DialogResult.Yes Then MessageBox.Show("Saved")
End If
End Sub
Well here we have our first problem. While the DataGridView is in edit mode for a cell, it does not register KeyDown events. No matter what I did, I could not trigger the KeyDown event handler until after moving to another cell. Essentially I need to hit Enter twice in order for the MessageBox to appear.
So from here I figured, okay, lets put together an event handler for EditingControlsShowing that should work!
Private Sub dg_EditingControlsSHow(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles dg.EditingControlShowing
MessageBox.Show("Please work")
End Sub
And here the MessageBox only appears once the editting has begun, and I can do nothing once I am finished.
So I am utterly confused as to what to do here. To restate what I want to do I am trying to set up a DataGridView that has the following functionality for the Cells:
While editing the value within a Cell, if you press the Enter key, you get a prompt asking if you want to save your changes.
If instead of Enter, you press any other key that would turn off Cell editing (like Tab or the Arrow Keys) you get a prompt asking if you are sure you wish to cancel editing.
I really don't know what to do here, so any and all help is greatly appreciated.
First you need to check if you are in edit mode:
Private isEditing As Boolean = False
Private Sub DataGridView1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
isEditing = True
End Sub
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
isEditing = False
End Sub
Then you can override the ProcessCmdKey function:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If isEditing = True Then
Select Case keyData
Case Keys.Left
Case Keys.Right
Case Keys.Enter
Case Else
End Select
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Return True from the function if you want to suppress the key.
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
As I'm putting the finishing touches on my program I'm having some troubles.
Theres several user inputs and a submit button, once the inputs has been filled I wish to enable the submit button, else the button should be disabled. This is what I have:
Private Sub ButtonControl(sender As System.Object, e As System.EventArgs) Handles Input1.Validated
If Input1.Text = "" Then
ButtonSubmit.Enabled = False
ElseIf Input1.Text <> "" Then
ButtonSubmit.Enabled = True
End If
End Sub
The thing is it disables nomatter what and then it doesnt enable when my input is filed
Your code will work if you have another control that can receive the focus. Control Validation occurs on the loss of focus. If you need to have just one focusable item active you will need to use either KeyPress, KeyDown or Textchanged events to enable your button, also make sure that the CausesValidation property of your TextBox is true.
I would also make the method more generic so you could call it from multiple textbox's by using the sender object to access the textbox that raised the event. Also if you have a True/False condition you only need to do the comparison in the first if statement and then you just use an else not an elseif.
for example:
Private Sub ButtonControl(sender As System.Object, e As System.EventArgs) Handles Input1.Validated
If DirectCast(sender, TextBox).Text = "" Then
ButtonSubmit.Enabled = False
Else
ButtonSubmit.Enabled = True
End If
End Sub
You can also use the String.IsNullOrWhiteSpace Method to check if just spaces have been entered if you are using the 4.0 framework or above. Like this TextChanged EventHandler.
Private Sub ButtonControl(sender As Object, e As EventArgs) Handles Input1.TextChanged
If String.IsNullOrWhiteSpace(DirectCast(sender, TextBox).Text) Then
ButtonSubmit.Enabled = False
Else
ButtonSubmit.Enabled = True
End If
End Sub
You are going to need to use the TextBox "TextChanged" event, and be sure to set each Textbox AutoPostback="True". You can use an UpdatePanel to make the postbacks that occur on each Textbox you wish to validate less obnoxious to your end-user.
So, your textbox (if you have many, make sure they all have the OnTextChanged="ValidateForm":
<asp:TextBox ID="Input1" runat="server" OnTextChanged="Validate_TextChanged" />
Inside your textchanged ("ValidateForm") event (which each Textbox is attached to), one quick to implement route to do would just be
' Validation inside this event
Protected Sub Validate_TextChanged(sender As Object, e As EventArgs)
if Input1.text <> "" AndAlso Input2.text <> "" AndAlso ' ...etc.
End Sub
If you go the route of the UpdatePanel, you may find this useful.
This is the kind of thing I would do:
Private Sub TextBoxes_TextChanged( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles _
TextBox1.TextChanged, _
TextBox2.TextChanged, _
TextBox3.TextChanged
Dim textBoxes = { TextBox1, TextBox2, TextBox3 }
Button1.Enabled = textBoxes.All(Function (tb) tb.Text <> "")
End Sub
You can then add as many text boxes in to the textBoxes array as you need to check. Just make sure that the text boxes to the Handles list.
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Length > 0 Then
Me.Button1.Enabled = True
Else
Me.Button1.Enabled = False
End If
End Sub
Do that code in the Input1_TextChanged event
This is what I did and worked:
Dim CheckInput1 As Boolean
Private Sub Input1_TextChanged(sender As Object, e As EventArgs) Handles Input1.TextChanged, Input1.Enter
CheckInput1 = True
If Input1.Text = "" Then
CheckInput1 = False
End If
If CheckInput1 = False Then
ButtonSubmit.Enabled = False
ElseIf CheckInput1 = True Then
ButtonSubmit.Enabled = True
End If
End Sub
There must be a more efficient code that this but I think this solves your problem.