Create a new line by pressing control + enter - vb.net

for some reason every time i try to press control + enter I get "send message" why is this? I am pressing control so I should get create line..
If e.KeyCode = Keys.Control And e.KeyCode = Keys.Enter Then
Debug.Print("create a new line")
ElseIf e.KeyCode = Keys.Enter Then
Debug.Print("send message")
End Sub

You can just use this
If e.Control And e.KeyCode = Keys.Enter Then

Related

make program press the enter key on textbox focus

Forgive me if this is not possible, here's the scenario.
I have 2 textboxes, I type something on textbox1 and press enter, the text goes to textbox2 and the program presses the enter key on textbox2(user will not press the enter key).
Here's what I have in textbox1_keydown
if e.keycode = keys.enter then
str = textbox1.text
textbox2.focus()
textbox2.text = str
'Make textbox2 press the enter key here, without user pressing it on keyboard
end if
Found the answer
if e.keycode = keys.enter then
str = textbox1.text
textbox2.focus()
textbox2.text = str
'Make textbox2 press the enter key here, without user pressing it on keyboard
SendKeys.Send("{ENTER}")
end if
If e.keycode = Keys.Enter Then
'/* Check whether textbox1 is empty or not */
If textbox1.text <> "" Then
textbox2.text = ""
textbox2.text = textbox1.text
SendKeys.Send("{ENTER}")
Else
'/* if textbox1 is empty then cursor focus on textbox1 only */
textbox1.focus()
End If
End If
OR
If e.keycode = Keys.Enter Then
textbox2.text = ""
textbox2.text = textbox1.text
SendKeys.Send("{ENTER}")
End If

SendKeys not working in Datagridview keydown event

I have a Datagridview on my form with two columns. I only want the cells in the first column to be selected. So I'm using the datagridview's keydown event to capture the TAB and SHIFT+TAB. So if the first row is selected and the user presses tab, the second row will be selected instead of staying on the first row and selecting the second column's cell.
My if statement for the TAB key is working fine, but for some reason the SHIFT+TAB statement isn't. The cell above the currently selected cell isn't getting selected. Nothing happens.
Private Sub myGrid_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles myGrid.KeyDown
If e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
SendKeys.Send("{DOWN}")
End If
If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
SendKeys.Send("{UP}")
End If
End Sub
The really weird thing though is if I add a messagebox into the statement it works.
If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Tab Then
MsgBox("test")
e.SuppressKeyPress = True
SendKeys.Send("{UP}")
End If
Any ideas?
Sendkeys is a last, last resort...
How about something like this:
Private Sub dgv_KeyDown(sender As Object, e As KeyEventArgs) Handles dgv.KeyDown
If e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
dgv.CurrentCell = dgv(0, dgv.CurrentCell.RowIndex + 1)
End If
End Sub
With the help from #rheitzman I was able to see the main issue. The issue came from having the TAB and SHIFT+TAB in two different if statements. Here's my updated code:
Private Sub myGrid_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles myGrid.KeyDown
Dim row As String = Me.myGrid.CurrentCellAddress.Y
If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
Me.myGrid.CurrentCell = Me.myGrid.Rows(row - 1).Cells(0)
Me.myGrid.Rows(row - 1).Cells(0).Selected = True
ElseIf e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
Me.myGrid.CurrentCell = Me.myGrid.Rows(row + 1).Cells(0)
Me.myGrid.Rows(row + 1).Cells(0).Selected = True
End If
End Sub
Tab and Shift+Tab are already handled by Windows Forms and DataGridView. They move the cell highlighter/selector move left(previous) and right(next). You'd have to create a class that inherits DataGridView and manually override their functionality to achieve what you want, then use that new class for your data grid.
Private Sub DataGridView1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
Dim r As Integer = Me.DataGridView1.CurrentCellAddress.Y
Dim f As Integer = Me.DataGridView1.CurrentCellAddress.X
If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
If 0 = r Then
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(DataGridView1.RowCount - 1).Cells(f - 1)
Me.DataGridView1.Rows(DataGridView1.RowCount - 1).Cells(f - 1).Selected = True
Else
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(r - 1).Cells(f)
Me.DataGridView1.Rows(r - 1).Cells(f).Selected = True
End If
ElseIf e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
If DataGridView1.RowCount = (r + 1) Then
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(0).Cells(f + 1)
Me.DataGridView1.Rows(0).Cells(f + 1).Selected = True
Else
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(r + 1).Cells(f)
Me.DataGridView1.Rows(r + 1).Cells(f).Selected = True
End If
End If
End Sub

Detect Print Screen keyup and keydown in Keyboard Tester app VB NET

I have a problem when I try to make Keyboard Tester application for my small office.
I cant detect print screen keycode e.keycode = keys.PrintScreen.
I will do something like change picturebox back color when a key down, but it seems doesnt work with print screen, nothing happen.
my code is:
Private Sub keyboardmenu_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
'Esc + Function Keys -----------------------------------------
If e.KeyCode = Keys.Escape Then
EscBox.BackColor = Color.Red
End If
If e.KeyCode = Keys.F1 Then
F1Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F2 Then
F2Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F3 Then
F3Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F4 Then
F4Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F5 Then
F5Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F6 Then
F6Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F7 Then
F7Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F8 Then
F8Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F9 Then
F9Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F10 Then
F10Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F11 Then
F11Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F12 Then
F12Box.BackColor = Color.Red
End If
'End of Esc + Function Keys -----------------------------------------
Private Sub keyboardmenu_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
'Esc + Function Keys ----------------------------------------
If e.KeyCode = Keys.F1 Then
F1Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F2 Then
F2Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F3 Then
F3Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F4 Then
F4Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F5 Then
F5Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F6 Then
F6Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F7 Then
F7Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F8 Then
F8Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F9 Then
F9Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F10 Then
F10Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F11 Then
F11Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F12 Then
F12Box.BackColor = Color.Transparent
End If
'End of Esc + Function Keys -----------------------------------------
End Sub
Please help me. Idk if there are more keys like print screen problem.
Thank You
I'm not sure of the real reason, but I've read about it before and came to the conclusion that Windows is protecting that key's event from being easily handled. Someone else probably knows better, but this works:
Protected Overrides Function ProcessKeyEventArgs(ByRef msg As Message) As Boolean
If msg.WParam = Keys.PrintScreen Then
MessageBox.Show("PrintScreen key press detected!")
End If
Return MyBase.ProcessKeyEventArgs(msg)
End Function
Also, you should put all of those if statements in a Select Case statement:
Private Sub keyboardmenu_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.Escape
EscBox.BackColor = Color.Red
Case Keys.F1
F1Box.BackColor = Color.Red
Case Keys.F2
F2Box.BackColor = Color.Red
'Etc
End Select
End Sub
This comment is on the IF statements and CASE suggestion, otherwise I concur with the answer by Keith.
I use this sort of thing fairly often, but it's crazy to put all those IF statements, OR the Case statement:
Simply name the boxes to match the enumeration, then (assuming Controls is the parent container of all the boxes)
Controls(e.keycode.tostring & "box").backcolor = Color.Red
and
Controls(e.keycode.tostring & "box").backcolor = Color.Transparent
This one line will replace everything (if you rename the escbox to escapebox)
Of course, you might want to do some checking like
If Controls.ContainsKey(e.keycode.tostring & "box") Then ...

Control KeyDown Event Trap CTRL-C etc

I'm having a problem with processing CTRL-C, CTRL-V, CTRL-X on my form.
Private Function HandleKeyDown(sender As Object,
e As KeyEventArgs,
ByVal vShow As String) As Boolean
HandleKeyDown = False
If e.KeyCode = Keys.F1 Then
Help.ShowPopup(Me, vShow, Cursor.Position)
End If
If e.KeyCode = Keys.C AndAlso e.Modifiers = Keys.Control Then
sender.Copy()
ElseIf e.KeyCode = Keys.V AndAlso e.Modifiers = Keys.Control Then
sender.Paste()
ElseIf e.KeyCode = Keys.X AndAlso e.Modifiers = Keys.Control Then
sender.Cut()
Else
Console.WriteLine(String.Format("Modifiers:{0} KeyCode:{1} KeyData:{2} KeyValue:{3} ", e.Modifiers.ToString, e.KeyCode.ToString, e.KeyData.ToString, e.KeyValue.ToString))
End If
HandleKeyDown = True
End Function
The KeyDown event never picks up the second key. The only value the KeyCode ever seems to hold is Keys.Control. This is what the Console.WriteLine outputs for CTRL-C
Modifiers:Control KeyCode:ControlKey KeyData:ControlKey, Control KeyValue:17
Where am I going wrong?
Are you calling this from the actual event handler? I had to add "Handles MyBase.KeyDown" and remove your extra parameter.
Private Sub HandleKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
'HandleKeyDown = False
If e.KeyCode = Keys.C AndAlso e.Modifiers = Keys.Control Then
MessageBox.Show(String.Format("Modifiers:{0} --- KeyCode:{1} --- KeyData:{2} --- KeyValue:{3} ",
e.Modifiers.ToString, e.KeyCode.ToString, e.KeyData.ToString, e.KeyValue.ToString))
'sender.Copy()
ElseIf e.KeyCode = Keys.V AndAlso e.Modifiers = Keys.Control Then
MessageBox.Show(String.Format("Modifiers:{0} --- KeyCode:{1} --- KeyData:{2} --- KeyValue:{3} ",
e.Modifiers.ToString, e.KeyCode.ToString, e.KeyData.ToString, e.KeyValue.ToString))
'sender.Paste()
ElseIf e.KeyCode = Keys.X AndAlso e.Modifiers = Keys.Control Then
MessageBox.Show(String.Format("Modifiers:{0} --- KeyCode:{1} --- KeyData:{2} --- KeyValue:{3} ",
e.Modifiers.ToString, e.KeyCode.ToString, e.KeyData.ToString, e.KeyValue.ToString))
'sender.Cut()
End If
'HandleKeyDown = True
End Sub
It's working for me. I get a value of 88, 67, and 86 respectively.

VB.NET Moving Objects Diagonally using Keys

How can I Move objects diagonally when two directional keys are pressed? How to do that?
I tried adding elseif statement for Handling Up and Right together but It still move up or right when i press them together
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Right Then
Label1.Location = New Point(Label1.Location.X + 5, Label1.Location.Y)
ElseIf e.KeyCode = Keys.Left Then
Label1.Location = New Point(Label1.Location.X - 5, Label1.Location.Y)
ElseIf e.KeyCode = Keys.Down Then
Label1.Location = New Point(Label1.Location.X, Label1.Location.Y + 5)
ElseIf e.KeyCode = Keys.Up Then
Label1.Location = New Point(Label1.Location.X, Label1.Location.Y - 5)
ElseIf e.KeyCode = Keys.Up And e.KeyCode = Keys.Right Then
Label1.Location = New Point(Label1.Location.X + 5, Label1.Location.Y + 5)
End If
CheckIntersections()
If Label1.Location.Y < 0 Then
Label1.Location = New Point(Label1.Location.X, Me.Height)
ElseIf Label1.Location.Y > Me.Height Then
Label1.Location = New Point(Label1.Location.X, Me.Bottom = 0)
ElseIf Label1.Location.X < 0 Then
Label1.Location = New Point(Me.Width, Label1.Location.Y)
ElseIf Label1.Location.X > Me.Width Then
Label1.Location = New Point(0, Label1.Location.Y)
End If
End Sub
The KeyDown event occurs each time a single key is pressed. Therefore you'll need to remember when a cursor key is pressed on once occurrence of KeyDown to check in another occurrence for another cursor key being pressed.
Remember to subscribe to KeyUp events to clear the state because the use could press one cursor key, release it and then press another.
Your code will never work:
ElseIf e.KeyCode = Keys.Up And e.KeyCode = Keys.Right Then
because this cannot be true. KeyCode is a single key code, it cannot be both one key and another.