本文介绍了将值传递给按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!如何将值传递给按钮单击?



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!

推荐答案

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());
        }
}



这篇关于将值传递给按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 16:46
查看更多