将变量从一个按钮单击传递到另一个按钮

时间:2013-12-13 13:52:34

标签: c# winforms button buttonclick

这是一个简单的问题,但我似乎无法找到答案。我想在同一个表单中使用从一个按钮单击到另一个按钮的存储值。非常感谢任何援助。我尝试使用Calling code of Button from another one in C#中的示例但无法使其工作。

public struct xmlData
    {
         public string xmlAttribute;   
    }

private void Show_btn_Click(object sender, EventArgs e)
    {
           xmlData myXML = new xmlData();
                //do something.....
           myXML.xmlAttributes = "blah"
    }

private void Submit_btn_Click(object sender, EventArgs e)
    {
 //I want to call myXML.xmlAttributes retrieving the stored value from Show_btn_Click
    }

2 个答案:

答案 0 :(得分:2)

在构造函数中实例化xml数据,以便您可以在类中全面访问它。

public class XYZ
{    
   xmlData myXML; 

   public XYZ()
   {
       myXML = new xmlData();
   }

   private void Show_btn_Click(object sender, EventArgs e)
   {            
       //do something.....
       myXML.xmlAttributes = "blah"
   }

   private void Submit_btn_Click(object sender, EventArgs e)
   {
      // Here you can work myXML.xmlAttributes
   }    
}

答案 1 :(得分:2)

您应该在更高的范围内声明myXML变量。

xmlData myXML = new xmlData();

public struct xmlData
{
     public string xmlAttribute;   
}

private void Show_btn_Click(object sender, EventArgs e)
{
            //do something.....
       myXML.xmlAttributes = "blah"
}

private void Submit_btn_Click(object sender, EventArgs e)
{
 //I want to call myXML.xmlAttributes retrieving the stored value from Show_btn_Click
}