I have form1 that whenever I press F1..
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
If keyData = Keys.F1 Then
Form2.Show()
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
form2 shows..
what I wanted is when form2 is up, and I press F1 again, it closes.
any idea? thanks
Just write the same code you wrote but in Form2 and say form2.close()
If keyData = Keys.F1 Then
Form2.Close()
Return True
End If
Related
Please help me figure out for Shift+F10 in VB.NET, Escape and F1 work but Shift+F10 doesn’t work.
Here’s my code:
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
Select Case keyData
Case Keys.Escape
Me.Dispose()
Case Keys.F1
MsgBox("You have pressed F1")
Case Keys.ShiftKey = Keys.F10
MsgBox("You have pressed Shift+F10")
Case Else
Return MyBase.ProcessCmdKey(msg, keyData)
End Select
Return True
End Function
You can use
Case Keys.F10 + Keys.Shift
MsgBox("You have pressed Shift+F10")
Result:
I'm creating a simple typing test program.
I want the space bar to trigger checking if the typed word is correct, then clear the contents of the textbox.
What happens here is the first typed word will work and be counted when the space bar is pressed and the typed word will be cleared, but the space character still remains and so the next typed word will contain a space char and will be read wrong.
I tried interchanging where the txtInput.Clear() should be placed but results in the same problem.
Private Sub txtInput_TextChanged(sender As Object, e As KeyEventArgs) Handles txtInput.KeyDown
If e.KeyValue = Keys.Space Then
Space()
End If
End Sub
Public Function Space()
If txtInput.Text = txtWord.Text Then
ctr = CInt(txtWord.TextLength)
charTotal = charTotal + ctr
lblScore.Text = charTotal.ToString
End If
txtInput.Clear()
txtWord.Text = rdmWord()
End Function
In KewDown Event the value of TextBox isnot setted yet. So, use KeyUp Event as the code below shows
Private Sub txtInput_KeyUp(sender As Object, e As KeyEventArgs) Handles txtInput.KeyUp
If e.KeyValue = Keys.Space Then
Space()
End If
End Sub
You could also override ProcessCmdKey and trap the spacebar there:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If keyData = Keys.Space Then
If Me.ActiveControl Is txtInput Then
Space()
Return True ' suppress space
End If
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
If you want to allow space or enter, then change to:
If keyData = Keys.Space Or keyData = Keys.Enter Then
I made a MyTextMain custom control
I added the property:
Public Property PressedEscape As Boolean = False
and
Private Sub MyTextMain_KeyUp (ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.Escape Then Me.PressedEscape = True
End Sub
However, when I add a TextBox to any new form, the focus is on that control, I press ESC and the property does not change.
I tried the KeyPreview property of the form with True and False and the same.
Where is the error?
Override the ProcessCmdKey in your custom TextBox control:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If keyData = Keys.Escape Then
Me.PressedEscape = True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Making PressedEscape a property seems like an odd choice. I would re-think that depending on what you are doing with it.
Using a derived class, I capture the key on a DataGridView and process it as a TAB key. (No issue).
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
If keyData = Keys.Return Then
keyData = Keys.Tab
With msg
.WParam = Keys.Tab
End With
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
'
'This routine handles Cell Editing
Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
If keyData = Keys.Return Then
keyData = Keys.Tab
End If
Return MyBase.ProcessDialogKey(keyData)
End Function
But a situation has come up where I want to place a Button cell type into the row. I don't want to process ENTER > TAB for that cell type. I would prefer ENTER to work as ENTER.
I cannot find any example code to show me how to determine the CELLTYPE of the cell where I'm processing the keystroke.
Any help appreciated.
I am working with MDIs, and I have an MDI parent and childs. But when I press Ctrl+F4 it closes the MDI child.
I am currently working on KeyEvents to disable Ctrl + F4:
Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
Select Case (keyData)
Case Keys.Control Or Keys.F4
Return True
Case Keys.Control
Return True
End Select
Return MyBase.ProcessDialogKey(keyData)
End Function
I can't get this working for me... How can I fix it?.
Wrong method, it isn't a dialog. And be sure you put it in the MDI parent form, not the child. This works:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
If keyData = (Keys.Control Or Keys.F4) Then Return True
Return MyBase.ProcessCmdKey(msg, keyData)
End Function