I am having some trouble moving labels up and down. I've gotten them to move left and right but cannot seem to get them to move up and down.

Also

I wanted to control a picture box by arrow keys. I'm not totally sure along which code I need to acquire that task.

Thanks,

mkalinic

Recommended Answers

All 6 Replies

Welcome,

Show us your code please.

Hi,

for your moving labels, it is easy when you know how

for moving a label UP, the code would be.

Label1.Location = New Point(Label1.Location.X, Label1.Location.Y - 1)

Down:

Label1.Location = New Point(Label1.Location.X, Label1.Location.Y + 1)

Left:

Label1.Location = New Point(Label1.Location.X - 1, Label1.Location.Y)

Right:

Label1.Location = New Point(Label1.Location.X + 1, Label1.Location.Y)

As for your picturebox, it's quite a similar story, we just need to detect when the arrow keys are being used.

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Up Then
            Label2.Location = New Point(Label2.Location.X, Label2.Location.Y - 1)
        ElseIf e.KeyCode = Keys.Down Then
            Label2.Location = New Point(Label2.Location.X, Label2.Location.Y + 1)
        ElseIf e.KeyCode = Keys.Left Then
            Label2.Location = New Point(Label2.Location.X - 1, Label2.Location.Y)
        ElseIf e.KeyCode = Keys.Right Then
            Label2.Location = New Point(Label2.Location.X + 1, Label2.Location.Y)
        End If
    End Sub

EDIT: If you want to move the labels/pictureboxes in higher increments, use for example Location.X + 2 instead of + 1

Note I used Label2 as an example, this is because I tried making it myself and used a label, not a picturebox, although they should work in the same way.

Warning though.. I found if I used buttons to control Label1's position, I found it hard to deselect the buttons, so be aware of that.

Hope I could help, add rep and mark as solved if I did :)

commented: he's good +1

Thank you very much for your help on the moving of picture boxes, but I believe i worded my other problem wrong. I would like to have the labels move up and down by them selves with a time. I have an idea of how to move them left to right in the form, but I want them to move up and down inside the perimeter of a panel. Heres what I have so far.

Public Class MainForm
    Dim RanNum As New Random            ' Random number generator
    Dim DistMove As Integer = 10   ' The distance the label moves each time

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ' Add distance to label
        moveLabel1.Top += DistMove
        ' Check if label has reach the right side of window
        If moveLabel1.Top + moveLabel1.Width > Me.ClientRectangle.Width Then
            ' Reverse the direction - if positive make negative, if negative make positive
            DistMove = Not DistMove
        ElseIf moveLabel1.Top < 20 Then
            ' If label is less than zero make it zero
            moveLabel1.Left = 20
            ' Reverse the direction
            DistMove = Not DistMove
        End If

Thank you very much for your help on the moving of picture boxes, but I believe i worded my other problem wrong. I would like to have the labels move up and down by them selves with a time. I have an idea of how to move them left to right in the form, but I want them to move up and down inside the perimeter of a panel. Heres what I have so far.

Public Class MainForm
    Dim RanNum As New Random            ' Random number generator
    Dim DistMove As Integer = 10   ' The distance the label moves each time

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ' Add distance to label
        moveLabel1.Top += DistMove
        ' Check if label has reach the right side of window
        If moveLabel1.Top + moveLabel1.Width > Me.ClientRectangle.Width Then
            ' Reverse the direction - if positive make negative, if negative make positive
            DistMove = Not DistMove
        ElseIf moveLabel1.Top < 20 Then
            ' If label is less than zero make it zero
            moveLabel1.Left = 20
            ' Reverse the direction
            DistMove = Not DistMove
        End If

Hi, I'm not really an expert in the bouncing labels category :D But I think this might be a push in the right direction, however, it lags the program a little so you might not want it on a constant loop.

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        For iCount = 50 To 10 Step -1
            Label1.Location = New Point(Label1.Location.X, Label1.Location.Y - 1)
            Me.Refresh()
            Threading.Thread.Sleep(10)
        Next
        For iCount = 50 To 10 Step -1
            Label1.Location = New Point(Label1.Location.X, Label1.Location.Y + 1)
            Me.Refresh()
            Threading.Thread.Sleep(10)
        Next
    End Sub

Thanks, but it lags too much, ima try and find a more efficient way.

Hello all,
I am currently making a VB.Net game for my end of the project, but I have found myself in a deadlock. I was wondering how to make labels move up and down in my panel by themselves at a set speed. Also left and right by themselves. I've tried a couple different methods but none seem to work. They either lag or I can't seem to set where there bounds are. If you could reply I would greatly appreciate the advice\help.


Thanks,

mkalinic

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.