开发者

send additional strings to Event handler function

i have a standard mouseEventHandler :

a.MouseClick += new Mous开发者_开发百科eEventHandler(labelClick);

where a is a label. the function called on click is like so :

private void labelClick(object sender,MouseEventArgs mea)
        {
            MessageBox.Show("click on the label");
        }   

How can i send more information to the called function? (i.e. i have a lot of labels ; for each label i would like to send 2 strings for my location and address )

Regards, Alexandru Badescu


Maybe you can use the Tag property of the label and cast the sender parameter as a label and read the Tag property.

Set the Tag property to the

string.Format("{0};{1}", Location, Address)

Then in the event handler

Label lbl = sender as Label;
String[] LocAdd = ((String)lbl.Tag).Split(';');

Now you have the Location in the first item in the array and the Address in the second one.


a.MouseClick += (sender, e) => HandleLabelMouseClick(sender, e, "whatever1", "whatever2");

private void HandleLabelMouseClick(object sender, MouseEventArgs e, string whatever1, string whatever2)
{
    MessageBox.Show(whatever1 + "\n" + whatever2);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜