开发者_StackOverflow中文版
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this questionI want to pass the text value of a button to a procedure in C# ,Please help me
Your question is vague to say the least so I'm going to assume a couple of things that most certainly may be wrong.
Assuming this is Winforms and that by procedure you mean method or function and you have managed to place a button named button1 onto your form you would first attach an event handler to the Click event on the button. You can either do this via the designer or through code.
// The button1_Click is a method which we'll define in a moment
button1.Click += new EventHandler(button1_Click);
Then you would write a method to receive that event:
protected void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(button1.Text);
}
If you have multiple buttons and only want to react based on the text property of them you can create one event handler for all of the buttons.
protected void SomeButton1_Click(object sender, EventArgs e)
{
// By casting the sender to a button we can get a hold
// of a reference to the button that caused the event.
// Just be careful not to hook up anything that isn't a
// button to the event.
Button b = (Button)sender;
MessageBox.Show(b.Text);
}
There are tons of resources when you google for c# event handlers. C# Help has an article that shows up in the top results which at first glance seems to cover the basics.
精彩评论