Question Several objects in same if-condition.

asgerbj

New member
Joined
Apr 6, 2011
Messages
2
Programming Experience
1-3
Hello, I'm currently trying to make a beat generator, which allows you to add several labels (boxes) onto a panel, and then sound will be played when the "play slider" reaches the same x-value. However, i can't make the slider find the boxes.

OK, so this is what I've got so far! Clicking my button, a label named sound0, sound1, sound2 is added to my panel_back (which is the background of the player)
Code:
    Private Sub btn_addkick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_addkick.Click
        Dim tempsound As New Label
        Me.panel_back.Controls.Add(tempsound)
        tempsound.Size = New Size(50, 25)
        tempsound.Location = New Point(100, 100)
        tempsound.BackColor = Color.Aqua
        tempsound.Text = "sound" & number_sound.ToString
        AddHandler tempsound.MouseDown, AddressOf sound_MouseDown
        AddHandler tempsound.MouseMove, AddressOf sound_MouseMove
        AddHandler tempsound.MouseUp, AddressOf sound_MouseUp
        tempsound.Name = "sound" & number_sound.ToString
        number_sound += 1
    End Sub
This works, and i see my labels being named sound0, sound1, sound3 in the form. Now, I've added different handlers to make them moveable around the form. This works aswell, and this is my code.

Code:
    Private Sub sound_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dragging = True
        StartX = e.X
        StartY = e.Y
    End Sub

    Private Sub sound_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If Dragging = True Then
            sender.Left = (sender.Left + e.X) - StartX
            sender.Top = (sender.Top + e.Y) - StartY
        End If
    End Sub

    Private Sub sound_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dragging = False
    End Sub
However, i cannot figure out how to make my labels being played, when i have a panel named panel_play to go across the form (with a timer). I've tried something like this, but it doesn't work.

Code:
       Private Sub timer_play_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer_play.Tick
        panel_play.Location = New Point(panel_play.Location.X + 1, panel_play.Location.Y)

        Do Until check_sound >= number_sound
            If panel_play.Location.X = sound(check_sound).Location.X Then
                My.Computer.Audio.Play(My.Resources.kick, AudioPlayMode.Background)
            End If
            check_sound += 1
        Loop

        If panel_play.Location.X = 400 Then
            panel_play.Location = New Point(0, panel_play.Location.Y)
        End If
    End Sub
It gives an error at the If-statement. Am i doing something wrong with just this if-statement, or should do something different when adding labels to the panel?
 

Ehsanit

Well-known member
Joined
Feb 23, 2011
Messages
80
Programming Experience
1-3
sound(check_sound)
won't be seen as sound1, sound2, sound3, etc. It points to an array sound() [this should be sound(1), sound(2) etc], and that array does not exist.
You either need to create such an array (probably preferred), or use a for each loop to select all controls on a form (and additional code to only consider the labels).
What I would do is create an array of integers soundposition(). Create labels at the positions of those integers if you must, but that is just to show the user what's going on. Take your numbers from an array of numbers rather than the properties of the labels.
 

asgerbj

New member
Joined
Apr 6, 2011
Messages
2
Programming Experience
1-3
Me.panel_back.Controls("sound" & number.ToString)
Thanks, that worked for me! Changed my code to this, and the If-condition including the sound.Location.X worked now!

Code:
    Private Sub timer_play_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer_play.Tick
        panel_play.Location = New Point(panel_play.Location.X + 1, panel_play.Location.Y)
        check_sound = 0

        Do Until check_sound = number_sound
            If panel_play.Location.X = Me.panel_back.Controls("sound" & check_sound.ToString).Location.X Then
                My.Computer.Audio.Play(My.Resources.kick, AudioPlayMode.Background)
            End If
            check_sound = check_sound + 1
        Loop

        If panel_play.Location.X = 400 Then
            panel_play.Location = New Point(0, panel_play.Location.Y)
        End If
    End Sub
 
Top Bottom