Hello,
I'm working on a project where I have in a form 5 to 10 buttons, everything manually added (from code) and I need to see which button was clicked on.

Button Remove = new Button();
Remove.Name = i.ToString();
Remove.Text = "Remove";
Remove.Click += new EventHandler(Remove_Clicked());
i++;

 void Remove_Clicked(object sender, EventArgs e)
        {
            
        }

I named the buttons in numbers (i) so i can get them easier.

So is there a way to check which button was pressed?

Thanks

Recommended Answers

All 5 Replies

Cast the sender object to a button and check the ID.

Could u please give me code or something? because I'm not that good at events since they didnt teach us at school about them and all I know is the few things that I read on the internet

Here's an example for first web apps, then Windows apps.

string str = new string(((Button)sender).ID);
string str = new string(((Button)sender).Name);

Hope that helps.

commented: That fixed my problem, thanks +0

Thanks Sodabread that did help :)

Thanks Sodabread that did help :)

Glad to be of service =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.