none
How to interchange the postion of label controls during run time?

    Question

  • I have 6 label controls in my form. On the occurrence of each Button_click event I want these labels to randomly interchange their positions.

    Can anyone please give me an sample code to do this task?

    Thank you in advance.

    Monday, March 20, 2017 4:48 AM

Answers

  • I have 6 label controls in my form. On the occurrence of each Button_click event I want these labels to randomly interchange their positions.

    Do you mean that you want each label to move to a random position?

    Public Class Form1
        Dim positions As New List(Of Point)
        Dim labels As New List(Of Label)
        Dim RND As New Random
    
        Private Sub Form1Load(sender As Object, e As EventArgs) Handles MyBase.Load
            positions.Add(New Point(20, 20))
            positions.Add(New Point(20, 50))
            positions.Add(New Point(20, 80))
            positions.Add(New Point(20, 110))
            positions.Add(New Point(20, 140))
            positions.Add(New Point(20, 170))
            labels.Add(Label1)
            labels.Add(Label2)
            labels.Add(Label3)
            labels.Add(Label4)
            labels.Add(Label5)
            labels.Add(Label6)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim Selected As New List(Of Integer)
            Dim R As Integer = 0
            For I As Integer = 0 To 5
                Do
                    R = RND.Next(6)
                Loop Until Not Selected.Contains(R)
                Selected.Add(R)
                labels(I).Location = positions(R)
            Next I
        End Sub
    End Class
    If you mean that you wnt two randomly selected labels to swap positions, the code will be slightly different.
    • Marked as answer by Tabzee Monday, March 20, 2017 3:22 PM
    Monday, March 20, 2017 5:23 AM

All replies

  • I have 6 label controls in my form. On the occurrence of each Button_click event I want these labels to randomly interchange their positions.

    Do you mean that you want each label to move to a random position?

    Public Class Form1
        Dim positions As New List(Of Point)
        Dim labels As New List(Of Label)
        Dim RND As New Random
    
        Private Sub Form1Load(sender As Object, e As EventArgs) Handles MyBase.Load
            positions.Add(New Point(20, 20))
            positions.Add(New Point(20, 50))
            positions.Add(New Point(20, 80))
            positions.Add(New Point(20, 110))
            positions.Add(New Point(20, 140))
            positions.Add(New Point(20, 170))
            labels.Add(Label1)
            labels.Add(Label2)
            labels.Add(Label3)
            labels.Add(Label4)
            labels.Add(Label5)
            labels.Add(Label6)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim Selected As New List(Of Integer)
            Dim R As Integer = 0
            For I As Integer = 0 To 5
                Do
                    R = RND.Next(6)
                Loop Until Not Selected.Contains(R)
                Selected.Add(R)
                labels(I).Location = positions(R)
            Next I
        End Sub
    End Class
    If you mean that you wnt two randomly selected labels to swap positions, the code will be slightly different.
    • Marked as answer by Tabzee Monday, March 20, 2017 3:22 PM
    Monday, March 20, 2017 5:23 AM
  • This is exactly what I was looking for.

    Thanks alot


    • Edited by Tabzee Monday, March 20, 2017 3:21 PM
    Monday, March 20, 2017 3:21 PM