I am attempting to create a simple platforming game in Visual Basic as a project for school. Although I have figured out how to move the picture up, down, left, and right using the WASD keys, I am unable to make the PictureBox move diagonally by holding two keys. For example, the PictureBox should move diagonally to the top right when the user holds down the W and D keys. In short, how can I move the PictureBox diagonally when the user holds two keys?
Below is the code so far.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If DPressed = True Then
PictureBox1.Left = PictureBox1.Left + 9
ElseIf DPressed = True AndAlso WPressed = True Then
PictureBox1.Left = PictureBox1.Left + 9
PictureBox1.Top = PictureBox1.Top - 9
ElseIf APressed = True Then
PictureBox1.Left = PictureBox1.Left - 9
ElseIf SPressed = True Then
PictureBox1.Top = PictureBox1.Top + 9
ElseIf WPressed = True Then
PictureBox1.Top = PictureBox1.Top - 9
End If
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.D Then
DPressed = True
ElseIf e.KeyCode = Keys.W Then
WPressed = True
ElseIf e.KeyCode = Keys.A Then
APressed = True
ElseIf e.KeyCode = Keys.S Then
SPressed = True
ElseIf (e.KeyCode And Not Keys.Modifiers) = Keys.D AndAlso e.Modifiers = Keys.W Then
DPressed = True
WPressed = True
End If
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
If e.KeyCode = Keys.D Then
DPressed = False
WPressed = False
ElseIf e.KeyCode = Keys.W Then
WPressed = False
ElseIf e.KeyCode = Keys.A Then
APressed = False
ElseIf e.KeyCode = Keys.S Then
SPressed = False
End If
End Sub
E.g.
Private wDown As Boolean = False
Private aDown As Boolean = False
Private sDown As Boolean = False
Private dDown As Boolean = False
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.W
wDown = True
Case Keys.A
aDown = True
Case Keys.S
sDown = True
Case Keys.D
dDown = True
End Select
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyCode
Case Keys.W
wDown = False
Case Keys.A
aDown = False
Case Keys.S
sDown = False
Case Keys.D
dDown = False
End Select
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim xIncrement = 0
Dim yIncrement = 0
If wDown Then
yIncrement -= 9
End If
If aDown Then
xIncrement -= 9
End If
If sDown Then
yIncrement += 9
End If
If dDown Then
xIncrement += 9
End If
PictureBox1.Location += New Size(xIncrement, yIncrement)
End Sub
Maybe try marking the DPressed, WPressed, APressed variables as Shared.
Related
Here the code all was working peacefully and without any error but when i added a checkbox to my form keys are stopped working
Dim Lefty, Righty As Boolean
Private Sub Player_ctrl_Tick(sender As Object, e As EventArgs) Handles Player_ctrl.Tick
Dim Speed As Integer = 23
If Lefty And Player1.Left > Me.Left Then
testlabel.Text += 1
Player1.Left -= Speed
ElseIf Righty And Player1.Left + Player1.Width < Me.Width Then
Player1.Left += Speed
End If
End Sub
Private Sub MainGame_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.Left
Lefty = True
Case Keys.Right
Righty = True
End Select
End Sub
Private Sub MainGame_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Lefty = False
Righty = False
End Sub
Any fixes please?
so im making a 2d game in visual studios, is it possible so if I hold down d then press space/w/upkey then it jumps while still moving in the current direction and could stop moving in that direction mid air?
Also my jumping code is really bad, any advice on how to make it better?
here is my code
Private Sub Map_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Up Or e.KeyCode = Keys.W Or e.KeyCode = Keys.Space Then
If jumping = False Then
Timer2.Interval = 1000
Timer2.Enabled = True
jumping = True
With Timer3
.Interval = 10
.Enabled = True
End With
user.Location = New Point(user.Location.X, user.Location.Y - 5)
End If
ElseIf e.KeyCode = Keys.Down Or e.KeyCode = Keys.S Then
If user.Location.Y >= ground.Location.Y - 94 Then
Else
user.Location = New Point(user.Location.X, user.Location.Y + 5)
End If
ElseIf e.KeyCode = Keys.Left Or e.KeyCode = Keys.A Then
user.Location = New Point(user.Location.X - 5, user.Location.Y)
ElseIf e.KeyCode = Keys.Right Or e.KeyCode = Keys.D Then
user.Location = New Point(user.Location.X + 5, user.Location.Y)
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If user.Bounds.IntersectsWith(ground.Bounds) Then
user.Location = New Point(user.Location.X, user.Location.Y - 6)
End If
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
jumping = False
Timer3.Interval = 10
Timer3.Enabled = False
Do Until user.Location.Y >= ground.Location.Y - 94
user.Location = New Point(user.Location.X, user.Location.Y + 1)
Loop
End Sub
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
Do Until user.Location.Y = ground.Location.Y - 180
user.Location = New Point(user.Location.X, user.Location.Y - 1)
Loop
End Sub
I was wondering if there is a way to allow only some sections of a textbox to be editable in a frozen sentence.
e.g:
He share of ________ to worse. Weddings and any opinions suitable smallest nay.
My he houses or months settle remove ladies appear. Engrossed suffering
supposing he recommend do eagerness.
where the underscore can be edited but the sentences cannot be changed at all.
Thanks
A simple solution for your example
Public Class Form1
Private SelectionStart As Integer = 10
Private SelectionEnd As Integer = 20
Private SelectionCurrent As Integer = 10
Private Sub CursorControl(Optional ByVal e As Object = Nothing)
If SelectionCurrent > SelectionEnd Then
SelectionCurrent = SelectionEnd
ElseIf SelectionCurrent < SelectionStart Then
SelectionCurrent = SelectionStart
End If
If TextBox1.SelectionStart >= SelectionEnd Then
If Not e Is Nothing Then
e.Handled = True
e = Nothing
End If
TextBox1.SelectionStart = SelectionEnd
TextBox1.SelectionLength = 0
Me.SelectionCurrent = TextBox1.SelectionStart
ElseIf TextBox1.SelectionStart <= SelectionStart Then
If Not e Is Nothing Then
e.Handled = True
e = Nothing
End If
TextBox1.SelectionStart = SelectionStart
TextBox1.SelectionLength = 0
Me.SelectionCurrent = TextBox1.SelectionStart
Else
Me.SelectionCurrent = TextBox1.SelectionStart
End If
End Sub
Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click
CursorControl()
End Sub
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
CursorControl()
End Sub
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
TextBox1.SelectionStart = Me.SelectionCurrent
TextBox1.SelectionLength = 0
If e.KeyCode = Keys.Right Or e.KeyCode = Keys.Left Or e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then
e.Handled = True
e = Nothing
End If
CursorControl(e)
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
TextBox1.SelectionStart = Me.SelectionCurrent
TextBox1.SelectionLength = 0
If e.KeyChar = "_" Then
e.Handled = True
End If
If TextBox1.SelectionStart >= SelectionEnd Then
e.Handled = True
e = Nothing
ElseIf TextBox1.SelectionStart <= SelectionStart Then
e.Handled = True
e = Nothing
Else
TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.SelectionStart) & e.KeyChar & TextBox1.Text.Substring(TextBox1.SelectionStart + 1, TextBox1.Text.Length - TextBox1.SelectionStart - 1)
TextBox1.SelectionStart = Me.SelectionCurrent
e.Handled = True
e = Nothing
End If
CursorControl()
End Sub
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
TextBox1.SelectionStart = Me.SelectionCurrent
TextBox1.SelectionLength = 0
If e.KeyCode = 189 Then '_
e.Handled = True
End If
Me.SelectionCurrent = Me.SelectionCurrent + 1
Me.TextBox1.SelectionStart = Me.SelectionCurrent
CursorControl(e)
End Sub
End Class
Put three TextBoxes next to each other and allow only the middle one to be edited. Here's some XAML:
<StackPanel
Orientation="Horizontal">
<TextBox
Text="He share of"
IsReadOnly=True />
<TextBox
Text="<Enter your phrase here>" />
<TextBox
Text="to worse. ......."
IsReadOnly=True />
</StackPanel>
The end user will see the whole line as one unit.
The Winform control RichTextBox via the SelectionProtected Property allows you to mark text ranges a readonly (protected).
Marking the entire text as protected and then selectively unprotecting the desired editable range will produce the desired effect.
Public Class Form1
Private underlinedFont As Font
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RichTextBox1.Text = "This is protected. This is unprotected."
' Protect all text
RichTextBox1.SelectAll()
RichTextBox1.SelectionProtected = True
' Unprotect the word "unprotected"
Dim startUnProtected As Int32 = RichTextBox1.Find("unprotected", RichTextBoxFinds.WholeWord)
If startUnProtected > -1 Then
RichTextBox1.SelectionProtected = False
If underlinedFont Is Nothing Then underlinedFont = New Font(RichTextBox1.Font.FontFamily, RichTextBox1.Font.Size, FontStyle.Underline, RichTextBox1.Font.Unit)
RichTextBox1.SelectionFont = underlinedFont
End If
End Sub
End Class
I want to capture the keydown event in a rich text box in VB .Net.
According to the documentation and examples I have seen I just need to use e.Handled = True but this isn't working, I still get the key value placed in the rich text box.
My code is
Private Sub Tagging_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles rtxReference.KeyDown
If ((e.KeyCode = Keys.Z) AndAlso e.Control) Then
'MsgBox("Undo")
rtxReference.Text = sPrevious
Else
If cbxHotKeyTags.Checked Then
If e.KeyCode = Keys.T Then
sTagName = "article-title"
ElseIf e.KeyCode = Keys.V Then
sTagName = "volume"
ElseIf e.KeyCode = Keys.F Then
sTagName = "fpage"
ElseIf e.KeyCode = Keys.L Then
sTagName = "lpage"
ElseIf e.KeyCode = Keys.I Then
sTagName = "issuee"
End If
e.Handled = True
End If
End If
End Sub
Ok here's my dilemma. here's this code I have:
If e.KeyCode = Keys.A Then
TextBox1.AppendText("C, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
End If
Now when I enter this under Form1_KeyDown, visual basic thinks this:
'KeyCode is not a a member of 'System.EventArgs'
Now I've seen this code work before, but it isn't here. Any help?
Here's the full code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = Keys.A Then
TextBox1.AppendText("A, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
End If
If e.KeyCode = Keys.S Then
TextBox1.AppendText("C,")
PictureBox14.Visible = True
My.Computer.Audio.Play(My.Resources.D, AudioPlayMode.Background)
End If
End Sub
Not sure why you method definition declares e as EventArgs, but the fix is simply to make the parameter of type KeyEventArgs. This is because EventArgs (naturally) does not contain a property called KeyCode, but KeyEventArgs does!
Change your event handler method definition to the following:
Private Sub foo_KeyDown(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.A Then
TextBox1.AppendText("A, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
ElseIf e.KeyCode = Keys.S Then
TextBox1.AppendText("C,")
PictureBox14.Visible = True
My.Computer.Audio.Play(My.Resources.D, AudioPlayMode.Background)
End If
End Sub
It sounds like your method is using the wrong EventArgs. The Control.KeyDown event sends it as a System.Windows.Forms.KeyEventArgs. So your code should read as
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)
// code here
End Sub
I've found sometimes you need to provide the full object type similar to the below:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
MsgBox(e.KeyCode.ToString()) 'Message what the keycode
If (e.KeyCode = Keys.A) Then
MsgBox("e.KeyCode = Keys.A") 'Message that I've found the A
TextBox1.AppendText("C, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
End If
End Sub
Have you also tried testing this in a textbox on the form similar to the below (textbox in example is named TextBoxKeyTest)?
Private Sub TextBoxKeyTest_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBoxKeyTest.KeyDown
MsgBox(e.KeyCode.ToString())'Message what the keycode
If (e.KeyCode = Keys.A) Then
MsgBox("e.KeyCode = Keys.A")'Message that I've found the A
TextBox1.AppendText("C, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
End If
End Sub
This should work in VB 2010
Private Sub cmdWiden_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.Up Then Me.Top = Me.Top - 5
If e.KeyCode = Keys.Down Then Me.Top = Me.Top + 5
If e.KeyCode = Keys.Left Then Me.Left = Me.Left - 5
If e.KeyCode = Keys.Right Then Me.Left = Me.Left + 5
End Sub