1

So what original program I wrote was display pdf file list on comboBox from c:\temp location. But then, I wanted to give user an option to change folder so I created another form called Form2. This Form2 only opens when user press button from Form1 and it closes when user hit save button in Form2. So, I wrote code in Form2. btnSDS opens filepath and displays the path on textBox. How do I make Form1 to get folder location from Form2?

Process

  1. user starts program and form 1 opens and grabs pdf file name from default folder.

  2. user wants to change default folder so he clicks admin button from form 1 and it opens form 2 which is admin form.

  3. user changes default folder setting of folder 1 from folder 2 and closes folder 2.

  4. default folder setting changes in folder 1.

  5. when user opens folder 2 again, default folder setting remains in textBox in folder 2.

    // Form2    
    private void btnSDS_Click(object sender, EventArgs e)
    {
        var folderBrowserDialog1 = new FolderBrowserDialog();
    
        // Show the FolderBrowserDialog.
        DialogResult result = folderBrowserDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            string folderName = folderBrowserDialog1.SelectedPath;
            textBoxSDSLocation.Text = folderName;
        }
    }
    
    // Form1
        private void Form1_Load(object sender, EventArgs e)
        {            
            DirectoryInfo test = new DirectoryInfo(@"c:\temp"); //Assuming Test is your Folder
            FileInfo[] Files = test.GetFiles("*.pdf"); //Getting Text files
    
            comboSDS.DataSource = Files;
            comboSDS.DisplayMember = "Name";
        }
    
    
    private void comboSDS_SelectedIndexChanged(object sender, EventArgs e)
    {
        //axAcroPDF2.LoadFile(@"C:\temp\" + comboSDS.Text);
        //axAcroPDF2.src = @"C:\temp\" + comboSDS.Text;
        axAcroPDF2.LoadFile(@"Form2.textBoxSDSLocation.Text" + comboSDS.Text);
        axAcroPDF2.src = @"Form2.textBoxSDSLocation.Text" + comboSDS.Text;
        axSetting();
    }
    
  • Please refer to this: [Accessing variables in other Windows Form class](https://stackoverflow.com/questions/12042303/accessing-variables-in-other-windows-form-class) – Ali Hosseini Nezhad Dec 05 '18 at 14:44

1 Answers1

0

Actually you need to design a dialog form.

There is a predefined dialog for choosing a Folder in Windows Form project; It's called FolderBrowserDialog this is a sample code for that:

privatevoid BrowseFolderButton_Click(object sender, EventArgs e) {  
    FolderBrowserDialog folderDlg = newFolderBrowserDialog();  
    folderDlg.ShowNewFolderButton = true;  
    // Show the FolderBrowserDialog.  
    DialogResult result = folderDlg.ShowDialog();  
    if (result == DialogResult.OK) {  
        textBox1.Text = folderDlg.SelectedPath;  
        Environment.SpecialFolder root = folderDlg.RootFolder;  
    }  
}  

You can Set Default path using this code:

folderDlg .SelectedPath = //myFolder;

If you didn't want to use this default. you can build your own Dialog. In Form2:

public partial class Form2:Form
{

  public string SelectedPath {get; set;}

  private SelectPath_Click(object sender, EventArgs e)
  {
      // if path is a valid path {

      SelectedPath = txtBoxPath.text;
      this.DialogResult = DialogResult.OK;
      this.Close();

     // } else { CloseForm or Display an Error or ... }  
  } 
}

this is pretty clear that i added a Button to the second form. you may want to choose another way.

you can use your codes like FolderBrowserDialog (SampleCode):

Form2 FolderDialog = new Form2(); 
if (FolderDialog.ShowDialog() == DialogResult.OK)
{
   /// Set New Path 
   Foo.Text = FolderDialog.SelectedPath;
} 
else 
{ 
  // User Didn't selected a Valid path or he closed your form without response.
} 
RezaNoei
  • 1,266
  • 1
  • 8
  • 24
  • how do I call this from form1 that? it seems Form2.txtBox.Text doesn't work. Also, selectedPath doesn't stay on textBox in form 2 when form2 is closed. –  Dec 05 '18 at 16:15
  • Form2 is not Disposed !!! It's just closed ! you can access to the SelectedPath again. as I showed in the sample code, you can use it, because it's Public – RezaNoei Dec 05 '18 at 16:43
  • the program load form1 first. I updated code so you can see how it gets pdf file name that populates combo box. tried your method but i can't get it to populate on form 1 combo box. ideally, i want form 1 to grab file from default folder but also use form 2 as admin form where I can change default folder of form 1 combo box. So process is like this. Program starts -> Form 1 opens, it grabs file name from default folder and populate combo box. user click admin button on form 1 and it opens form 2. From there user change default folder and form 2 closes and form 1 gets new default folder. –  Dec 05 '18 at 18:50
  • Default Access Modifier for form's controls is `Private`. so If you want to access to a textbox, you must change it's `Modifier` property to Public so you can see it. I have Added my own property (SelectedPath) and it was Public. Somehow you must check Form2's closing in Form1. I don't know how you have done that but i would prefer to use ShowDialog method and return a Dialog result to check the return value of Form2. – RezaNoei Dec 06 '18 at 03:50