daynamic event handlers....
-
i created 100 buttons ( each with different text) with click even handlers daynamically.how can i display the text of the clicked button (how can i know which button is being clicked)?
Depending on how you did it the event handler is probably of the form
void Button_OnClick(object sender, eventargs args)
{
//......
}the variable
sender
is most likely the button (dependant upon your implementation), so you can probably do something likevoid Button_OnClick(object sender, eventargs args)
{
Button buttonClicked = sender as Button;
if(buttonClicked == null)
return;
string buttonText = buttonClicked.Text;
// use buttonText here or wherever....
}If this doesn't answer your question, post some code and please remember the <pre> tags!
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
-
i created 100 buttons ( each with different text) with click even handlers daynamically.how can i display the text of the clicked button (how can i know which button is being clicked)?
The sender item in the event parameters indicates which item was the origin of the event. You can get the text of the button by casting the sender to a Button and then read the Text property. Here's a quick sample
private void ButtonEventHandler(object sender, EventArgs e)
{
Button button = sender as Button;
if (button == null) return;
txtClicked.Text = button.Text;
}"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
The sender item in the event parameters indicates which item was the origin of the event. You can get the text of the button by casting the sender to a Button and then read the Text property. Here's a quick sample
private void ButtonEventHandler(object sender, EventArgs e)
{
Button button = sender as Button;
if (button == null) return;
txtClicked.Text = button.Text;
}"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
Now that is just frightening! :-)
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
-
Now that is just frightening! :-)
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
-
The sender item in the event parameters indicates which item was the origin of the event. You can get the text of the button by casting the sender to a Button and then read the Text property. Here's a quick sample
private void ButtonEventHandler(object sender, EventArgs e)
{
Button button = sender as Button;
if (button == null) return;
txtClicked.Text = button.Text;
}"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
You're welcome.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
No problem!
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.