Pergunta

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
    }
Foi útil?

Solução

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
}

Outras dicas

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
   }    
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top