Return id or know name of the dynamic object that was clicked

Asked

Viewed 544 times

2

Hello! I am learning now to create dynamic objects in C#, my question is how to return the Text from the button I clicked for example. Let’s say I created 10 numbered dynamic objects (in this case their text) from 0 to 9 and I clicked on 3, how do I know I clicked on 3? What I’ve done so far:

I tried to do by taking a look at the "e" if it had any value stored in it, but apparently it’s not quite as I thought...

2 answers

2


You can share the event between them +/- like this:

private void MeusBotoes(System.Object sender, System.EventArgs e){
      Button botao = default(Button);
      botao = (Button)sender;

      switch (botao.Name){

        case "Button1":
           Interaction.MsgBox("Voce pressionou o botão 1");
        break;

        case "Button2":
           Interaction.MsgBox("Voce pressionou o botão 2");
        break;
           //etc
      }
 }
  • I had already found something on the net very similar to yours, but thank you, that’s right! http://stackoverflow.com/questions/20220544/pass-dynamic-button-id-to-click-event

  • 1

    i already used this once but for VBA, great tutorials from Macoratti

1

Take the desired property from object sender. In this case is the button (remember to convert to Button).

In the case of buttons, there should be one for creating them all.

  • But... What property is this? I’ve made a button that replicates the others understood? What I need now is to click on others, know which one I’m clicking on, identify them by the Text property, you understand?

  • I don’t know if you noticed, but she’s in the parameters of the method boton_click(object sender, EventArgs e).

  • I’m very beginner in C# I didn’t understand anything

Browser other questions tagged

You are not signed in. Login or sign up in order to post.