开发者

How to pass the value of button to procedure in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
开发者_StackOverflow中文版

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 8 years ago.

Improve this question

I 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜