keypress

seanvoth

Well-known member
Joined
Aug 5, 2006
Messages
58
Programming Experience
1-3
i have a label and when i press the up key i want it to go up

and i cant find the keypress code nor the code to move a

label please help:eek:
 
Hi,

You can use a textbox (and make it look like a label)

To make it look like a label

VB.NET:
TextBox1.BackColor = System.Drawing.SystemColors.Control
TextBox1.BorderStyle = System.Windows.Forms.BorderStyle.None
TextBox1.Cursor = System.Windows.Forms.Cursors.Arrow
TextBox1.ReadOnly = True

Then handle the keydown event to move the control in runtime.

VB.NET:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
     If e.KeyCode = Keys.Up Then
          TextBox1.Location = New System.Drawing.Point(TextBox1.Location.X, TextBox1.Location.Y - 1)
          e.Handled = True
     ElseIf e.KeyCode = Keys.Down Then
          TextBox1.Location = New System.Drawing.Point(TextBox1.Location.X, TextBox1.Location.Y + 1)
          e.Handled = True
     End If
End Sub

Regards,
Karthik Simha
 
VB.NET:
PrivateSub TextBox1_KeyDown(ByVal sender AsObject, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Up Then
TextBox1.Location = New System.Drawing.Point(TextBox1.Location.X, TextBox1.Location.Y - 1)
e.Handled = True
ElseIf e.KeyCode = Keys.Down Then
TextBox1.Location = New System.Drawing.Point(TextBox1.Location.X, TextBox1.Location.Y + 1)
e.Handled = True
ElseIf e.KeyCode = Keys.Left Then
TextBox1.Location = New System.Drawing.Point(TextBox1.Location.X - 1, TextBox1.Location.Y)
e.Handled = True
ElseIf e.KeyCode = Keys.Right Then
TextBox1.Location = New System.Drawing.Point(TextBox1.Location.X + 1, TextBox1.Location.Y)
e.Handled = True
EndIf
EndSub
 
ok thanks but know i have timer with this code

VB.NET:
[SIZE=2][COLOR=#0000ff]
Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Timer1_Tick([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Timer1.Tick
TextBox2.Location = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Drawing.Point(TextBox2.Location.X, TextBox2.Location.Y + 5)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

and when it gets to the location 80, 248 i want it to go to this location 184, 8 please help
 
Back
Top