views:

67

answers:

2

How can I use a value from the reading function in the button1_Click function?

public void reading(object sender, EventArgs e)
{
   DialogResult reading_from_folder = new DialogResult();
   reading_from_folder = folderBrowserDialog1.ShowDialog();

   if (reading_from_folder == DialogResult.OK)
   {
      string[] files_in_folder = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
      ...
   }
}

private void button1_Click(object sender, EventArgs e)
{ 
   foreach (string file in files_in_folder) // How do I access files_in_folder?
   {
      ListViewItem li = new ListViewItem(file);
   }
}                
+1  A: 

You need to store it somehow, for example as a private member:

string some_value = null;

public void reading(object sender, EventArgs e)
{
    some_value = "Foobar";
}

private void button1_Click(object sender, EventArgs e)
{
    if (some_value != null)
    {
        // ...
    }
}
Mark Byers
Thanks for help.
luc
+1  A: 
// Make it a member variable
private string[] mFilesInFolder = null;

public void reading(object sender, EventArgs e) 
{ 
    DialogResult reading_from_folder = new DialogResult(); 
    reading_from_folder = folderBrowserDialog1.ShowDialog(); 

    if (reading_from_folder == DialogResult.OK) 
    { 
       mFilesInFolder = Directory.GetFiles(folderBrowserDialog1.SelectedPath); 
    }
}  

private void button1_Click(object sender, EventArgs e) 
{   
   DoFileInFolderOperation();
}

private void DoFilesInFolderOperation()
{
   if(mFilesInFolder != null)
   {
      foreach (string file in mFilesInFolder) 
      { 
         ListViewItem li = new ListViewItem(file); 
      }
   } 
}
SwDevMan81
Thanks for help.
luc