Trying to use keyboard whilst text box is on screen - vb.net

In Visual Basic, I try to move 2 Picture Boxes in my pong game using my keyboard ( 'W' and 'S' keys and up and down arrows).
They won't move if there are any buttons or text boxes on the screen, however they will when I remove the the button/text box. I think it might have to do with being able to use the arrow keys to navigate between them but I don't know how exactly to stop that from happening.

Using these events to move up, down,
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.Up
moveUp()
e.Handled = True
Case Keys.Down
moveDown()
e.Handled = True
End Select
End Sub
Private Sub Form1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs)
Select Case e.KeyCode
Case Keys.Up
moveUp()
Case Keys.Down
moveDown()
End Select
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
Select Case e.KeyCode
Case Keys.Up, Keys.Down
stopMoving()
e.Handled = True
End Select
End Sub
Private Sub moveUp()
Me.Text = "Moving Up"
End Sub
Private Sub moveDown()
Me.Text = "Moving Down"
End Sub
Private Sub stopMoving()
Me.Text = "Idle"
End Sub
you can also use the same events to handle up, down on the TextBoxes and Buttons. Add the handlers programmatically
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each c As Control In Me.Controls
AddHandler c.KeyDown, AddressOf Form1_KeyDown
AddHandler c.PreviewKeyDown, AddressOf Form1_PreviewKeyDown
AddHandler c.KeyUp, AddressOf Form1_KeyUp
Next
End Sub
Note: it will only add the handlers to controls directly on your form, not controls inside containers. Though, you can use an extension method to get all children, no matter how buried in containers. See https://stackoverflow.com/a/21388034/832052

Related

How to pass Click event handler as a parameter to another sub?

In VB.Net I’m writing a program but I don’t know how to pass Click event handler as a parameter to another sub and how to call it from there. Can I do that? If yes, how?
E.g.: I have the following code. On two forms I have 1 ListView and 1 Button. If I press the button, it calls the click event of the button. In addition, if I press CTRL+P in the ListView, it calls the same event. I do it on both forms.
On form1:
Private Sub MyPrintButton1_Click(sender As Object, e As EventArgs) Handles MyPrintButton1.Click
' Print MyListView1 data...
End Sub
Private Sub MyListView1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyListView1.KeyUp
' CTRL+P = Print
If e.Modifiers = Keys.Control AndAlso e.KeyCode = Keys.P Then
e.Handled = True
MyPrintButton1_Click(sender, e)
End If
End Sub
On form2:
Private Sub MyPrintButton2_Click(sender As Object, e As EventArgs) Handles MyPrintButton2.Click
' Print MyListView2 data...
End Sub
Private Sub MyListView2_KeyUp(sender As Object, e As KeyEventArgs) Handles MyListView2.KeyUp
' CTRL+P = Print
If e.Modifiers = Keys.Control AndAlso e.KeyCode = Keys.P Then
e.Handled = True
MyPrintButton2_Click(sender, e)
End If
End Sub
I want to create only one sub in a different (common) file and want to call that passing the variables sender & e, and the event handler of the two Click events.
In this case the two KeyUp events would be the same, only the name of the passed event would be different:
On form1:
Private Sub MyListView1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyListView1.KeyUp
' Handle
AllListViews_KeyUp(sender, e, AddressOf MyPrintButton1_Click)
End Sub
On form2:
Private Sub MyListView2_KeyUp(sender As Object, e As KeyEventArgs) Handles MyListView2.KeyUp
' Handle
AllListViews_KeyUp(sender, e, AddressOf MyPrintButton2_Click)
End Sub
And the common sub would be similar like this:
Public Sub AllListViews_KeyUp(sender As Object, e As KeyEventArgs, PassedPrintButton_Click As ???? Of(????))
' CTRL+P = Print
If e.Modifiers = Keys.Control AndAlso e.KeyCode = Keys.P Then
e.Handled = True
PassedPrintButton_Click(sender2, e2 ????)
End If
End Sub
So: How do I need to pass the events as parameter and how to call the event? When I call the PassedPrintButton_Click event, how can I pass its own sender and e parameters (sender2, e2)? And how and where can I declare them?)
And, when I pass the address of MyPrintButton1_Click and addressMyPrintButton2_Click, from where will the sub know the click event’s sender and e parameters?
Thanks is advance.

vb.net can't use button.PerformClick after control Validated

in the form, i need check the textbox detail,so i use the Validated method.i also have a button to save form detail.and the button bind F10.
the text box Validated method
Private Sub txtReportCD_Validated(sender As Object, e As EventArgs) Handles txtReportCD.Validated
If txtReportCD.Text <> String.Empty Then
cboReportName.Value = txtReportCD.Text.Trim
Else
CommonMsg.showMsg("the CD not be empty")
End If
End Sub
the form keydown
Private Sub form_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.Control AndAlso e.Shift Then
Exit Sub
End If
Select Case e.KeyCode
'save data
Case Keys.F10
ClickButton(Me.btnSaveData, e)
e.Handled = True
End Select
End Sub
the ClickButton method
Private Sub ClickButton(ByVal btn As ButtonKdcCtrl, e As KeyEventArgs)
e.Handled = True
If e.Alt OrElse e.Control OrElse e.Shift Then
Exit Sub
End If
If btn.Enabled Then
btn.PerformClick()
End If
End Sub
the btnSaveData method
Private Sub btnSaveData_Click(sender As Object, e As EventArgs) Handles btnSaveData.Click
CommonMsg.showMsg("begin save data")
'do save form data
End Sub
when focus in txtReportCD,and i press F10 key. the form first do [btn.PerformClick], then do [txtReportCD_Validated] twice,last do [btnSaveData_Click].
but when focus in txtReportCD,and i click btnSavaData button.
the form first do [txtReportCD_Validated] once and not do [btnSaveData_Click].
how can i do,make when i press F10,the form do [txtReportCD_Validated] once
and the [btnSaveData_Click] not be call
PS: the txtReportCD text always empty.

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

Can't get KeyDown event on Tabpage

I have a TabControl with several TabPages, each containing a series of PictureBox. I want to be able to navigate with the arrow keys inside those TabPages. I tried to give .Focus() to the TabPage when the user selects it, and then trap the PreviewKeyDown and KeyDown events, but it does not work. I've put a breakpoint at the PreviewKeyDown event handler, and it is not event reached.
Here is the activation of the TabPage :
Sub TilesetsTab_Selected(ByVal sender As Object, ByVal e As TabControlEventArgs) Handles TilesetsTab.Selected
Dim myTab As TabControl = CType(sender, TabControl)
If myTab.TabCount = 0 Then Exit Sub
Active.Tileset = Project.Tileset(CInt(myTab.SelectedTab.Tag))
Active.Tileset.Tab.Focus()
End Sub
And here is the Key event handlers
Sub Tab_KeyPreviewDown(ByVal sender As Object, ByVal e As PreviewKeyDownEventArgs) Handles Tab.PreviewKeyDown
If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Or e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down Then e.IsInputKey = True
End Sub
Sub Tab_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Tab.KeyDown 'BUG
Dim TilesWidth As Integer = Me.Tab.Width \ Me.ScreenSize
Select Case e.KeyCode
Case Keys.Left
If Active.Tile.Index = 0 Then Exit Sub
Me.Tile(Active.Tile.Index - 1).Selected()
Case Keys.Right
If Active.Tile.Index = Project.Tileset.Count - 1 Then Exit Sub
Me.Tile(Active.Tile.Index + 1).Selected()
Case Keys.Up
If Active.Tile.Index < TilesWidth Then Exit Sub
Me.Tile(Active.Tile.Index - TilesWidth).Selected()
Case Keys.Down
If Active.Tile.Index + TilesWidth > Active.Tileset.Count - 1 Then Exit Sub
Me.Tile(Active.Tile.Index + TilesWidth).Selected()
End Select
End Sub
Why can't I fire a KeyDown event on the TabPage ?
I don't know why the answer posted yesterday by someone (and the comments I made) were deleted, but it was said that TabPages could not have focus, and that it is restricted to TabControl. Following that advice, I shifted the PreviewKeyDown and KeyDown event handlers to TabControl events and it worked.
I'm losing some of the arrow navigation between TabPages, but it suits me.

How to change the Location (x,y) of the object by using coding in VB.net?

I Want to say sorry if there was a same question like i had. I have tried to Search but i couldn't find it, so.. i hope there is no other question like this..
To the point, i need your help to tell me how to change the Location of an object in a form
What i want to do is making the Button1 move to the left when i press the left key on the keyboard. But i have a problem with how to set the location (x,y) of the object
Private Sub Button1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Button1.KeyPress
If Asc(e.KeyChar) = Keys.Left Then
End If
End Sub
Thanks ...
By default, the arrow keys are not captured by the control's KeyPress, KeyDown or KeyUp events. You can make them be captured by KeyDown and KeyUp by setting e.IsInputKey to True in the PreviewKeyDown event. Then you can move the button sideways by changing its Left property. The following assumes the button has focus.
Private Sub Button1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) _
Handles Button1.PreviewKeyDown
If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Then e.IsInputKey = True
End Sub
Private Sub Button1_KeyDown(sender As Object, e As KeyEventArgs) _
Handles Button1.KeyDown
Dim myButton As Button = CType(sender, Button)
If e.KeyCode = Keys.Left Then myButton.Left -= 1
If e.KeyCode = Keys.Right Then myButton.Left += 1
End Sub
Updated Solution
Here's a way to accomplish it so that it moves while holding down the key. Create a project with a Form and Button.
Right-click on the Project in the Solution Explorer and add two .NET References:
PresentationCore
WindowsBase
Here's the code. A timer will capture key events and move the button:
Imports System.Windows.Input
Public Class Form1
Private timex As New Timer
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
AddHandler timex.Tick, AddressOf myTickEvent
timex.Enabled = True
timex.Interval = 100
End Sub
Private Sub myTickEvent(sender As Object, e As EventArgs)
If isKeyPressed(Key.Left) Then
Me.Button1.Left -= 10
End If
If isKeyPressed(Key.Right) Then
Me.Button1.Left += 10
End If
If isKeyPressed(Key.Up) Then
Me.Button1.Top -= 10
End If
If isKeyPressed(Key.Down) Then
Me.Button1.Top += 10
End If
End Sub
Private Function isKeyPressed(ByRef keys As System.Windows.Input.Key)
If (Windows.Input.Keyboard.GetKeyStates(keys) And Windows.Input.KeyStates.Down) > 0 Then
Return True
Else
Return False
End If
End Function
End Class