button identification when mousedown event occurs

151 views Asked by At

I have code adding some button dynamically and these button have mousedown event. When i click button, mousedown function called. I want to identify what button i click in this event function. Button.Name property is limited string value.

Exactly i want lower code.

...
Button btnTest = new Button();
btnTest.identificationvalue="111.";
btnTest.MouseDown+=new MouseButtonEventHandler(deletec);
...
private void deletec(object sender, RoutedEventArgs e)  
{
    string idvalue=((Button)sender).identificationvalue;
    if ( idvalue== "111.")
    {

    }
    else if (idvalue == "110.")
    {

    }
    ...
}
...
1

There are 1 answers

0
Sartaglo On BEST ANSWER

Based on my interpretation of your question, you want each button to have a unique identifier which can be used to determine which button is assigned to sender in the deletec method. For this, I would use the Button.Tag property, which, as stated on the MSDN reference page (Control.Tag Property), "Gets or sets the object that contains data about the control." (Edit: this was suggested by Won Hyoung Lee in the comments while I was initially writing this answer.)

Another possibility would be to use the Button.Text property, although this would depend on the context of the application.

In addition, if this approach were to be taken, rather than using if-else statements, it would be cleaner to use a switch statement, i.e.:

switch (id)
{
    case 111:
        // do something
        break;
    case 112:
        // do something else
        break;
    ...
}