hello everyone.
i have 8 labels on a page and i have to select them one ba one.
all the labels are selected ba the user one after the other from top to bottom fashion,
like firstly label1 is selected then label2........labeln
and if someone exploits this fashion i want to show an error message please select label 1 before label2.
thanx in advance......

Dani AI

Generated

Short summary and corrective notes for 's scenario: the simplest, most reliable pattern is to keep a single piece of form-level state that tracks the next label index, attach one Click handler to all labels, and either enable only the current label or check the clicked label's index against that state. This avoids the common mistakes shown earlier.

Notes on the existing replies: 's idea (many boolean flags) will work only when those flags live at form scope; in the posted snippet they are declared inside the click method so they reset on each click. 's answer is on the right track — one form-level counter plus a common handler is the clean solution. Below is an alternative implementation that makes the expected order explicit by disabling later labels until their turn.

Private labelsInOrder As List(Of Label)
Private nextIndex As Integer = 0

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    labelsInOrder = New List(Of Label) From {Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8}
    For Each lbl As Label In labelsInOrder
        AddHandler lbl.Click, AddressOf Label_Click
        lbl.Enabled = False           'prevent out-of-order clicks
        lbl.Cursor = Cursors.Hand     'show clickable cursor
    Next
    labelsInOrder(0).Enabled = True   'only first label active at start
End Sub

Private Sub Label_Click(sender As Object, e As EventArgs)
    Dim clicked As Label = DirectCast(sender, Label)
    Dim idx As Integer = labelsInOrder.IndexOf(clicked)
    If idx <> nextIndex Then
        MessageBox.Show(String.Format("Please select label {0} before label {1}.", nextIndex + 1, idx + 1))
        Return
    End If

    clicked.ForeColor = Color.Red
    clicked.Enabled = False

    nextIndex += 1
    If nextIndex < labelsInOrder.Count Then
        labelsInOrder(nextIndex).Enabled = True
    Else
        MessageBox.Show("All labels are selected now.")
    End If
End Sub

Troubleshooting and UX tips: confirm label names match the designer (Label1..Label8) before building the list; attach handlers after InitializeComponent (Form_Load or New after InitializeComponent); use visual cues (ForeColor, BackColor, Cursor) so users see the order; consider LinkLabel or Buttons if keyboard focus or accessibility is needed.

Recommended Answers

All 6 Replies

and you have tried....?

8 fixed lables? are you putting them in Panel or gropu box?
And what are the names you have given them to identify the sequence?

yes 8 fixed labels..
i am putting them in a form.

What is the name u gave to the label to identify?

See if this helps you... you need to give the propernames what you gave in design.

Private Sub Label3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label3.Click, Label4.Click, Label5.Click, Label6.Click
        Dim blblclick1 As Boolean
        Dim blblclick2 As Boolean
        Dim blblclick3 As Boolean
        Dim blblclick4 As Boolean


        Select Case sender.name.ToString
            Case "Label1"
                blblclick1 = True
            Case "Label2"
                If blblclick1 = False Then
                    MessageBox.Show("Please click label 1 first")
                End If
            Case "Label3"
                If blblclick2 = False Then
                    MessageBox.Show("Please click label 2 first")
                End If
            Case "Label4"
                If blblclick3 = False Then
                    MessageBox.Show("Please click label 3 first")
                End If
            Case "Label5"
                If blblclick4 = False Then
                    MessageBox.Show("Please click label 4 first")
                End If


        End Select

    End Sub

Here is the code you want. It check all the possibilities.
Let me know what you think:

Private labelClicked As Integer
Public Sub New()
	InitializeComponent()
	Dim labels As Label() = {label1, label2, label3, label4, label5, label6, _
		label7, label8}
	For i As Integer = 0 To labels.Length - 1
		labels(i).Tag = i
		labels(i).Click += New EventHandler(AddressOf Labels_Click)
	Next
End Sub

Private Sub Labels_Click(sender As Object, e As EventArgs)
	Dim l As Label = TryCast(sender, Label)
	Dim currentTag As Integer = CInt(l.Tag)
	If labelClicked < 8 Then
		If labelClicked <> currentTag Then
			If labelClicked < currentTag Then
				MessageBox.Show([String].Format("Please select label{0} before label{1}.", (labelClicked + 1), (currentTag + 1)))
			Else
				MessageBox.Show([String].Format("Label{0} has already been selected.", (currentTag + 1)))
			End If
		Else
			labelClicked += 1
			'color selected label for example:
			l.ForeColor = Color.Red
			If labelClicked.Equals(8) Then
				MessageBox.Show("All labels are selected now.")
			End If
		End If
	Else
		MessageBox.Show("You have already selected all the labels.")
	End If
End Sub
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.