Issue with C# browse button

Viewed 500

I am building an application that needs to browse for an excel file and load it.

The issue I'm facing so far is this:

When debugging the application automatically opens the browse for file dialog without having me to click the browse button.

namespace MassyDataMigration
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();            
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            LoadNewFile();
        }

        private void LoadNewFile()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Open required excel file";
            ofd.Filter = "EXCEL 97-03 Files|*.xls|EXCEL Files|*.xlsx";
            ofd.InitialDirectory = @"C:\";            
            System.Windows.Forms.DialogResult dr = ofd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                UserSelectedFilePath = ofd.FileName;
            }
        }

        public string UserSelectedFilePath
        {
            get
            {
                return tbFilePath.Text;
            }
            set
            {
                tbFilePath.Text = value;
            }
        }

        private void tbFilePath_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

What am I doing wrong?

3 Answers
Related