Results 1 to 8 of 8

Thread: [RESOLVED] Get control from its event

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2012
    Posts
    304

    Resolved [RESOLVED] Get control from its event

    Hi there,

    I have 3 labels that I am setting a MouveEnter and MouseLeave Event to change the BackColor.

    Rather than writing Label1.BackColor = Color.Red in Label1_MouseEnter, Label2.BackColor = Color.Red in Label2_MouseEnter etc. and similar for the MouseLeave Event, is there a way of determining the name of the control from the Event. I was hoping for something like sender.BackColor = Color.Red but this wasn't right.

    Thank you.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,040

    Re: Get control from its event

    The sender argument IS the control. You just have to cast it. So, you can do this:
    Code:
    dim myLabel = DirectCast(sender, Label)
    And now you'll have your control to work with.

    Alternatively, you can do something like this:
    Code:
    DirectCast(sender,Label).BackColor = Color.Red
    Which is close to what you were doing, except that you were leaving sender as type Object, which won't have a BackColor property. You have to do the cast to convert it to the right type.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2012
    Posts
    304

    Re: Get control from its event

    Thanks a lot Shaggy!

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2012
    Posts
    304

    Re: [RESOLVED] Get control from its event

    Is there a way to determine if it was the MouseEnter or MouseLeave Event?

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,040

    Re: [RESOLVED] Get control from its event

    No, there's no easy way to do that other than to have one method for MouseEnter and a different method for MouseLeave. An event is really just a list of methods maintained by the object that raises the event. The object maintains a list for each event it raises. When the action that triggers the event occurs, the object goes through the list of methods and calls each one in turn. So, when you create a method to handle an event, all you are really doing is passing the address of the event to the object that will raise the event, and the object (a control, in this case) simply adds the method to a list.

    When the event is raised, the object provides itself as the first argument. That lets you know WHO raised the event, but not WHY the event was raised. It is assumed that you will determine that by organization alone. If you don't care what the event was, pass the same method to all the events. If you DO care which event it was, you'll need a different method for each event.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2012
    Posts
    304

    Re: [RESOLVED] Get control from its event

    Shaggy,

    Thank you very much for that, but all it was is:

    I have my events:

    vb Code:
    1. Private Sub lblDocumentsTab_MouseEnter(sender As Object, e As EventArgs) Handles lblDocumentsTab.MouseEnter
    2.         lblHover(DirectCast(sender, Label), 1)
    3.     End Sub
    4.  
    5.     Private Sub lblDocumentsTab_MouseLeave(sender As Object, e As EventArgs) Handles lblDocumentsTab.MouseLeave
    6.         lblHover(DirectCast(sender, Label), 0)
    7.     End Sub
    8.  
    9.  
    10.     Private Sub lblHover(lbl As Label, switch As Integer)
    11.         If switch = 1 Then
    12.             If lbl.BackColor = Color.WhiteSmoke Then
    13.                 lbl.BackColor = Color.FromArgb(216, 234, 249)
    14.             End If
    15.         Else
    16.             If lbl.BackColor = Color.FromArgb(216, 234, 249) Then
    17.                 lbl.BackColor = Color.WhiteSmoke
    18.             End If
    19.         End If
    20.     End Sub

    Rather than using "switch" as Integer, I wondered if that could perhaps be a String that said "Enter" or "Leave", that way the code within the MouseEnter and MouseLeave could be exactly the same, something like:

    vb Code:
    1. Private Sub lblDocumentsTab_MouseEnter(sender As Object, e As EventArgs) Handles lblDocumentsTab.MouseEnter
    2.         lblHover(DirectCast(sender, Label), EventName.ToString)
    3.     End Sub
    4.  
    5.     Private Sub lblDocumentsTab_MouseLeave(sender As Object, e As EventArgs) Handles lblDocumentsTab.MouseLeave
    6.         lblHover(DirectCast(sender, Label), EventName.ToString)
    7.     End Sub

    Thank you.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,040

    Re: [RESOLVED] Get control from its event

    I like the integer approach better. What do you gain by using a string? You'd still need to have a switch or If statement in lblHover.

    However, if what you showed is all that lblHover has to do, then you don't need the switch at all, nor do you need the event name. After all, the logic is:

    If the color is WhiteSmoke Then
    change it to the other color.
    Else
    change it to WhiteSmoke
    End If

    Then it doesn't seem to matter what the event is.
    My usual boring signature: Nothing

  8. #8
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: [RESOLVED] Get control from its event

    If there were multiple I would suggest an Enum, but in the case with two states I would suggest a bool eg:

    Private Sub lblHover(lbl As Label, Entering As Boolean)

    Kris

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