将值传递给按钮 [英] Pass Values to Button
本文介绍了将值传递给按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好!如何将值传递给按钮单击?
Hi Everyone! How can i pass values to button click?
public Testing(string passvalue) //passvalue is "Hello"
{
MessageBox.Show(passvalue); // Show "Hello"
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
Messagebox.Show(passvalue); // the name 'passvalue' does not exist in the current context
}
请给我一些想法或解决方案。
谢谢!
Please give me idea or solutions.
Thanks!
推荐答案
您必须在课堂上创建一个字段,如下所示:
You would have to make it a field in your class, like this:
public class TestForm : Form
{
private string passvalue = "Hello World!";
public Testing()
{
MessageBox.Show(passvalue); // Show "Hello"
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
Messagebox.Show(passvalue);
}
}
或者如果你想获得一点点发烧友......
Or if you want to get a little fancier...
public class TestForm : Form
{
public TestForm()
{
InitializeComponents();
btnSubmit.Tag = "Hello World"; //You can set this anywhere in code.
}
public void Testing(string passvalue)
{
MessageBox.Show(passvalue);
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
Messagebox.Show(((Button)sender).Tag.ToString());
}
}
你可以先声明一个变量说:string temp;
然后在Testing方法中,将passvalue的值赋给temp:temp = passvalue;
最后,在你的按钮点击事件:MessageBox.Show(temp);
You can first declare a variable say: string temp;
then in the Testing method, assign the value of passvalue to temp: temp = passvalue;
lastly, in your button click event: MessageBox.Show(temp);
这篇关于将值传递给按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文