OpenFileDialog problem

OpenFileDialog problem

Anonymous
Not applicable
1,804 Views
2 Replies
Message 1 of 3

OpenFileDialog problem

Anonymous
Not applicable

Hi everyone,

 

I am creating an add in to batch insert Revit links to models. 

The first task is to add selected models path to listbox1 as following image: 

Capture.PNG

and following is my Code: 

  private void bt_AddModel_MouseClick(object sender, MouseEventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            if (openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                string[] files = openFileDialog1.FileNames;
                foreach(string s in files)
                {
                    listBox1.Items.Add(s);
                }

            }
        }

But the problem is, after selecting all the files and click OK, the Winform add in close as well instead of add the files list into listbox. 

If anyone have experience in this, please help me.

Thank you so much for your help :).

 

Best Regards,

 

Cherry Truong

0 Likes
Accepted solutions (1)
1,805 Views
2 Replies
Replies (2)
Message 2 of 3

jeremytammik
Autodesk
Autodesk
Accepted solution

Inside your Windows form, store the selected files in a list that is accessible via a public member function, e.g., SelectedFiles.

 

Then, access those files and process them after the form has been confirmed and closed:

  

  class Command : IExternalCommand
  {
    Result Execute(...)
    {
      FileSelectionForm form = new FileSelectionForm();
      if (form.ShowDialog()==DialogResult.OK)
      {
        string[] files = form.SelectedFiles;
        foreach(string s in files)
        {
          process file
        }
      }
    }
  }

  

Lots of samples by The Building Coder demonstrate how to display a Windows form prompting a user for input in an external command and process the results afterwards:

 

https://www.google.com/search?q=ShowDialog&as_sitesearch=thebuildingcoder.typepad.com

 

Cheers,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 3

Anonymous
Not applicable

Dear Jeremy,

 

Thank you so much for your help. I can fix the problem now :). and finished my add in to batch upgrade families to current version of Revit. 

 

Best Regards,

Ninh Truong

0 Likes