0

I'm working on a simple IM program as a personal project, and I've hit a bit of a snag. It's really more of a cosmetic thing, but I'm having some trouble with it. I've got a sidebar that lists all of a user's contents in the main window, and I'd like to set it up so that when a user clicks on a contact name, a tab opens in the chat area of the main window with a chat session opened with that contact. The really important part of this is for me to be able to get the UIElement, in this case a Label, that kicked off the MouseDoubleClick event. Once I can access this, I can access the information that I need to make the connection. Unfortunately, I'm a bit rusty with mouse events, and can't figure out how to get back to the Label once the event has been fired. My source code for programmatically creating the label is as follows:

foreach (ContactInfo contact in ContactList)
{
    Label currentContact = new Label();
    currentContact.Content = contact.ContactName.ToString() + " (" + contact.MachineName.ToString() + ")";
    currentContact.MouseDoubleClick += new MouseButtonEventHandler(ContactDoubleClickHandler);
    StckPnl_Contacts.Children.Add(currentContact);
}

And the (currently empty) handler is this:

public void ContactDoubleClickHandler(object sender, MouseButtonEventArgs e)
{

}

Am I going about this the wrong way? Any help would be appreciated.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Sean Cogan
  • 2,516
  • 2
  • 27
  • 42
  • I am not 100% if Labels handle click events properly. If they do, try inspecting the "sender" parameter. – wgraham May 23 '13 at 19:56
  • Sender only gives me Equals, GetHashCode, GetType, and ToString. So I'm guessing they don't properly handle click events? – Sean Cogan May 23 '13 at 19:57
  • 1
    Well through intellisense, yeah -- because it's defined as an object. Why not throw some dummy code in your handler and set a breakpoint -- find out what sender *really* is. – wgraham May 23 '13 at 19:59
  • Wow, I feel dumb now, haha. Yeah, sender is the Label. So, all I have to do is drill down into sender.base.Content to get the text of the Label that triggered the event. Thanks! If you post your comment as an answer, I'd be happy to use it as my selected answer. – Sean Cogan May 23 '13 at 20:01

3 Answers3

2

You can inspect the sender (first casting it to the type) to get the element that triggered the event:

Label targetLabel = sender as Label;
if (targetLabel != null)
{
    // Do something. I recommend not doing a direct cast in case someone in the future hooks another control type to the event handler.
}
wgraham
  • 1,383
  • 10
  • 16
1

You can use either of following to access the sender details

public void ContactDoubleClickHandler(object sender, MouseButtonEventArgs e)
    {
       var uiElement = (UIElement) sender; // cast it to UIElement
    }


public void ContactDoubleClickHandler(object sender, MouseButtonEventArgs e)
    {
       var dp = (DependencyObject) sender; // cast it to dependency object.
    }
S.N
  • 4,910
  • 5
  • 31
  • 51
1

Actually, the sender is your Label, you just have to transform it using:

Label contact = sender as Label;

Be sure to check if contact is null though, before performing any further operations.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
C8Zero
  • 36
  • 1