문제

This is a simple question but I can't seem to find an answer. I want to use the stored value from one button click to another within the same form. Any assistance would greatly be appreciated. I tried using an example from Calling code of Button from another one in C# but could not get it to work.

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
    }
도움이 되었습니까?

해결책

You should declare myXML variable at higher level of scope.

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
}

다른 팁

Instanciate the xmlData in the Constructor so you can access it overall in the class.

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
   }    
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top