event troubles vb.net**

[b][red]This message was edited by Tsikman at 2003-5-10 19:55:35[/red][/b][hr]
[b][red]This message was edited by Tsikman at 2003-5-10 19:53:47[/red][/b][hr]
Basically, I need a toggle switch for 26 labels. When one of them is clicked, that particular label changes color and back if the label is clicked again. making 26 click routines seems like a ridiculous waste of time. As you ccan probably tell I am new to programing

here is the long way

(dim temp as integer = 1)
Private Sub lblA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblA.Click
If temp = 1 Then
lblA.ForeColor = Drawing.Color.Red
temp = 2
ElseIf temp = 2 Then
lblA.ForeColor = Drawing.Color.Black
temp = 1
End If
End Sub

Private Sub lblB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblB.Click
If temp = 1 Then
lblA.ForeColor = Drawing.Color.Red
temp = 2
ElseIf temp = 2 Then
lblA.ForeColor = Drawing.Color.Black
temp = 1
End If
End Sub

Private Sub lblC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblC.Click
If temp = 1 Then
lblA.ForeColor = Drawing.Color.Red
temp = 2
ElseIf temp = 2 Then
lblA.ForeColor = Drawing.Color.Black
temp = 1
End If
End Sub

Private Sub lblD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblD.Click
If temp = 1 Then
lblA.ForeColor = Drawing.Color.Red
temp = 2
ElseIf temp = 2 Then
lblA.ForeColor = Drawing.Color.Black
temp = 1
End If
End Suby




Comments

  • : [b][red]This message was edited by Tsikman at 2003-5-10 19:55:35[/red][/b][hr]
    : [b][red]This message was edited by Tsikman at 2003-5-10 19:53:47[/red][/b][hr]
    : Basically, I need a toggle switch for 26 labels. When one of them is clicked, that particular label changes color and back if the label is clicked again. making 26 click routines seems like a ridiculous waste of time. As you ccan probably tell I am new to programing
    :
    : here is the long way
    :
    : (dim temp as integer = 1)
    : Private Sub lblA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblA.Click
    : If temp = 1 Then
    : lblA.ForeColor = Drawing.Color.Red
    : temp = 2
    : ElseIf temp = 2 Then
    : lblA.ForeColor = Drawing.Color.Black
    : temp = 1
    : End If
    : End Sub
    :
    : Private Sub lblB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblB.Click
    : If temp = 1 Then
    : lblA.ForeColor = Drawing.Color.Red
    : temp = 2
    : ElseIf temp = 2 Then
    : lblA.ForeColor = Drawing.Color.Black
    : temp = 1
    : End If
    : End Sub
    :
    : Private Sub lblC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblC.Click
    : If temp = 1 Then
    : lblA.ForeColor = Drawing.Color.Red
    : temp = 2
    : ElseIf temp = 2 Then
    : lblA.ForeColor = Drawing.Color.Black
    : temp = 1
    : End If
    : End Sub
    :
    : Private Sub lblD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblD.Click
    : If temp = 1 Then
    : lblA.ForeColor = Drawing.Color.Red
    : temp = 2
    : ElseIf temp = 2 Then
    : lblA.ForeColor = Drawing.Color.Black
    : temp = 1
    : End If
    : End Suby
    :
    :
    :
    :
    : Hi Tsikman

    Basically all you need to do is to create a sub routine and pass a label as an argument:


    Dim temp as integer = 1
    Private Sub lblA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblA.Click

    Dim MyLabel as lblA
    Call ChooseLabel(ByVal MyLabel as label)

    End Sub


    Private ChooseLabel(ByVal MyLabel as label)

    : If temp = 1 Then
    : MyLabel.ForeColor = Drawing.Color.Red
    : temp = 2
    : ElseIf temp = 2 Then
    : MyLabel.ForeColor = Drawing.Color.Black
    : temp = 1
    : End If
    : End Sub
    :



    Private Sub lblB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblA.Click

    Dim MyLabel as lblB
    Call ChooseLabel(ByVal MyLabel as label)

    End Sub


  • Doesn't that mean I have to still bring up 26 subs they just contain less ?
    I really don't want to create all the click routines./=

  • You can add one event to multiple controls. If you are doing it by hand, just reuse the sub in your AddHandler code, if you using the "Handles" thing then just list the events for all labels separated by commas. If you are using VS.NET, select all the labels, goto the properties window/events, double click the Click event. VS.NET should take care of it for you (using the Handles and commas stuff)

    Now in your event handler you can write code like this:
    [code]
    Private Sub lblD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblA.Click, lblB.Click, lblC.Click 'etc
    ' sender is the target label
    dim Label lbl as Label = CType(sender, Label)
    If temp = 1 Then
    lbl.ForeColor = Drawing.Color.Red
    temp = 2
    ElseIf temp = 2 Then
    lbl.ForeColor = Drawing.Color.Black
    temp = 1
    End If
    End Sub
    [/code]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories