Results 1 to 18 of 18

Thread: Label click events - Duplication needed ?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Label click events - Duplication needed ?

    Hi,
    I have 35 click events like so
    Code:
    Private Sub Label1_Click(sender As Object, e As System.EventArgs) Handles Label1.Click
            If Label1.Text > "" Then WhiteOut(1)
        End Sub
    
        Private Sub Label2_Click(sender As Object, e As System.EventArgs) Handles Label2.Click
            If Label2.Text > "" Then WhiteOut(2)
        End Sub
    
        Private Sub Label3_Click(sender As Object, e As System.EventArgs) Handles Label3.Click
            If Label3.Text > "" Then WhiteOut(3)
        End Sub
    Do I need all 35 click events, or is there some other way to achieve the same thing?
    Thanks.

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Label click events - Duplication needed ?

    Hi,

    The better way to achieve this sort of thing is to create One Routine which can then handle the Click Event for all your labels. You do this by creating a single routine to handle a single controls event as you normally do but then you expand the Handles Clauses at the end of the Routine to Add Additional Controls to be handled by this routine.

    What you then need to do is to CAST, there’s that word again, the sender variable in the signature of the routine to the Type of Object which called the routine. This then gives you full access the Actual Control that Raised the Event. i.e:-

    vb.net Code:
    1. Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label1.Click, Label2.Click, Label3.Click
    2.   Dim currentLabel As Label = DirectCast(sender, Label)
    3.  
    4.   'Do whatever you want with the Label that was clicked.
    5.   MsgBox(currentLabel.Text)
    6. End Sub

    Hope that helps.

    Cheers,

    Ian

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Re: Label click events - Duplication needed ?

    Hi Ian, Thanks for that, very handy and I have it in place but there's a problem later.

    Code:
    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click, Label2.Click, Label3.Click, Label4.Click, Label5.Click, Label6.Click, Label7.Click, Label8.Click, Label9.Click, Label10.Click, Label11.Click, Label12.Click, Label13.Click, Label14.Click, Label15.Click, Label16.Click, Label17.Click, Label18.Click, Label19.Click, Label20.Click, Label21.Click, Label22.Click, Label23.Click, Label24.Click, Label25.Click, Label26.Click, Label27.Click, Label28.Click, Label29.Click, Label30.Click, Label31.Click, Label32.Click, Label33.Click, Label34.Click, Label35.Click
            Dim currentLabel As Label = DirectCast(sender, Label)
            Dim lc = currentLabel.Text.ToString
            If lc > "" Then WhiteOut(CInt(currentLabel.Text))
    
        End Sub
    Pretty long line And another fight over strings/integers but it is entering Sub Whiteout as passing an integer.
    But then a line there is failing

    Code:
     If Me.GroupBox1.Controls("Label" & x).text > "" Then 'Highlight this day
    x is what is passed in as (CInt(currentLabel.Text)) but the above never evaluates to > "".
    Using the normal label click event it's OK. So something in that Handles method must be preventing the use of the control name.

  4. #4
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Label click events - Duplication needed ?

    Hi,

    I think you are making this harder than it really needs to be. Since currentLabel IS the label that has been clicked then why bother trying to access a control collection only to get the same label again?

    If you want to highlight that label as being clicked then just say:-

    Code:
    currentLabel.BackColor = Color.SomeColor
    Hope that helps.

    Cheers,

    Ian

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Re: Label click events - Duplication needed ?

    Quote Originally Posted by IanRyder View Post
    Since currentLabel IS the label that has been clicked then why bother trying to access a control collection only to get the same label again?
    Because the label may or may not have any text. Only if it does do i want to know. (It's a date picker because the inbuilt one doesn't work so well in XP, and some days in the month are blank.)
    Cheers - ABB

  6. #6
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Label click events - Duplication needed ?

    Hi,

    OK, to answer another point in your coding you are using the "Greater Than" Comparison Operator to check to see if something contains a string but you should really be checking this against one of the String Fields such as String.Empty so you could simply say:-

    vb.net Code:
    1. Dim currentLabel As Label = DirectCast(sender, Label)
    2.  
    3. If Not currentLabel.Text = String.Empty Then
    4.   currentLabel.BackColor = Color.Aqua
    5. End If

    Hope that helps.

    Cheers,

    Ian

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Re: Label click events - Duplication needed ?

    Ok Ian, I've just changed all of them and it's testing fine. It's a new method I've never seen before.
    Thanks

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Re: Label click events - Duplication needed ?

    I've also applied this technique to a group of command buttons used as a colour picker and storing the color for use later.
    When it is used, what is the correct way to remove focus from the button ?

    My previous (VBA) method was to set focus to an invisible control, but perhaps VBNet has something better ?

  9. #9
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Label click events - Duplication needed ?

    Use this code is more concise.

    Code:
            For Each lbl As Label In Me.Controls.OfType(Of Label)()
                AddHandler lbl.Click, Sub() If lbl.Text > "" Then WhiteOut(CInt(lbl.Text))
            Next
    If you find my contributions helpful then rate them.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Re: Label click events - Duplication needed ?

    Hi Toph, spent a bit of time trying that out but I couldn't get response from the click event.

    I presume it should go in Form Load ?

    Part of it was underlined in green saying something about a iteration variable being replaced with a local variable.

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Label click events - Duplication needed ?

    @Toph

    You really need to read Ian Ryder's post #6

    try this (in Form_Load):

    Code:
    For Each lbl As Label In Me.Controls.OfType(Of Label)()
        AddHandler lbl.Click, Sub() If lbl.Text <> "" Then lbl.BackColor = Color.Yellow
    Next

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Label click events - Duplication needed ?

    Quote Originally Posted by AlexanderBB View Post
    I've also applied this technique to a group of command buttons used as a colour picker and storing the color for use later.
    When it is used, what is the correct way to remove focus from the button ?

    My previous (VBA) method was to set focus to an invisible control, but perhaps VBNet has something better ?
    Setting focus to a hidden control is ok

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Re: Label click events - Duplication needed ?

    Ok on Set focus Paul, thank you.

    I added that to form load but same results. Nothing went yellow. If I removed the existing click event, clicks have no effect.

    Is that code intended to replace all 35 click events and work the same ?

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Label click events - Duplication needed ?

    yes. you don't need the click events because the addhandler assigns a lambda sub to each click event

    are the labels on the form or in a container control, such as a groupbox, panel, tabpage?

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Re: Label click events - Duplication needed ?

    Ah, yes I've struck this before - they are in GroupBox1. There is one other label that should not have a click event as well.
    Its called lblDay and the rest are label1, label 2... Label35.

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Label click events - Duplication needed ?

    Container controls have their own Controls collection which contains the controls contained in them():

    Code:
    For Each lbl As Label In Me.GroupBox1.Controls.OfType(Of Label)()
        If lbl.Name = "lblDay" Then Continue For
        AddHandler lbl.Click, Sub() If lbl.Text <> "" Then lbl.BackColor = Color.Yellow
    Next

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Label click events - Duplication needed ?

    unless you meant assign a lambda sub for lblDay also?

  18. #18

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Re: Label click events - Duplication needed ?

    > unless you meant assign a lambda sub for lblDay also?

    lblDay needs to be excluded from any click event

    It didn't seem to be working so I made a change to your last For Each Loop and put :

    Code:
     For Each lbl As Label In Me.GroupBox1.Controls.OfType(Of Label)()
                If lbl.Name = "lblDay" Then Continue For
                AddHandler lbl.Click, Sub() Debug.Print(lbl.Name)
            Next
    That printed "lblDay" every time I clicked.

    btw the word lbl after the brackets in line 3 is underlined in green and says

    Using the iteration variable in a lambda expression may have unexpected results. Instead, create a local variable within the loop and assign it the value of the iteration variable

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width