How to open file dialog on starting app

Jeffrey Schwartz 101 Reputation points
2021-08-01T00:32:45.673+00:00

(C# novice here...)

I have a WinForms app in Visual C#. When I initially open the app, I want to automatically open an OpenFileDialog so the user can select a file to work with.

Where do I put the code for this?

When I put it in the Form_Load event, it doesn't seem to work. When I put it in the Form_Activate event, the OpenFileDialog opens everytime the app comes to the foreground, not just the first time. When I put it if after InitializeComponent() in the main form, it doesn't work because things are "out of current context".

When I put the code in a Btn_click event, it works fine, but that's not how I want it to work.

More generally, what is the best place to put initialization or startup code in a WinForms app?

Thanks in advance for any advice.

Jeff Schwartz

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,962 questions
{count} votes

Accepted answer
  1. Castorix31 85,541 Reputation points
    2021-08-01T05:31:34.037+00:00

    > When I put it in the Form_Load event, it doesn't seem to work.

    What do you mean by "doesn't seem to work" ?

    It should work, but you just have to set the focus back, like

    OpenFileDialog ofd = new OpenFileDialog();
    ofd.ShowDialog();
    this.Activate();
    

    Otherwise, you can add it just after the main window is displayed for the first time, in Form1_Shown


4 additional answers

Sort by: Most helpful
  1. Bonnie DeWitt 811 Reputation points
    2021-08-01T01:41:51.777+00:00

    Jeff, it should work fine in the Form_Load(). That is usually a good place to put startup code. Can you post the code you have in the Load?

    0 comments No comments

  2. Sam of Simple Samples 5,546 Reputation points
    2021-08-01T03:39:10.587+00:00

    The best place for me might be the worst place for your requirements. As Bonnie says, show us more. Either show us the code you are using or show us what the problem is or at least describe the problem. And as Bonnie says, based on what we know, Form_Load would work.

    We do not know your requiements but perhaps you need to do it before the form is shown. So one possibility is to create a separate form; let us call it Form2. the following is a simplified version of what it might be like.

    public partial class Form2 : Form
    {
        public string Filename { get; set; }
    
        public Form2()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();
            Filename = ofd.FileName;
        }
    }
    

    In the Program class (Program.cs) instead of:

    Application.Run(new Form1());
    

    You can have:

            Form2 f2 = new Form2();
            Application.Run(f2);
            Form1 f1 = new Form1();
            f1.Filename = f2.Filename;
            Application.Run(f1);
    

    Now that is probably not how you want to do it but we do not know the requirements, it might be what you want.

    0 comments No comments

  3. Blithe_Zhu 1 Reputation point
    2021-08-01T04:27:04.2+00:00

    It works well on Form_Load event . Here's my test code. You can refer to it.

    119570-qq%E6%88%AA%E5%9B%BE20210801122318.png

    0 comments No comments

  4. Sam of Simple Samples 5,546 Reputation points
    2021-08-01T21:44:48.387+00:00

    For the benefit of others in the future, just use the Form.Shown Event instead of Form.Load.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.