Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with C# browse button [closed]

Tags:

c#

winforms

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?

like image 387
Sabrin Alexander Avatar asked Nov 30 '25 23:11

Sabrin Alexander


1 Answers

Sorry for top one this time now i debug to confirmly check the solution: it is working fine and showing browse dialog on debugging put his code:

OpenFileDialog ofd = new OpenFileDialog();
DialogResult dr = ofd.ShowDialog();
like image 123
CodeWarrior Avatar answered Dec 03 '25 13:12

CodeWarrior



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!