C#浏览按钮问题

时间:2016-03-10 06:19:36

标签: c# winforms

我正在构建一个需要浏览excel文件并加载它的应用程序。

到目前为止,我面临的问题是:

调试应用程序时,会自动打开浏览文件对话框,而无需单击“浏览”按钮。

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)
        {

        }
    }
}

我做错了什么?

2 个答案:

答案 0 :(得分:1)

对不起这次排名第一,我现在调试确认检查解决方案: 它运行正常,并在调试时显示浏览对话框,输入他的代码:

OpenFileDialog ofd = new OpenFileDialog();
DialogResult dr = ofd.ShowDialog();

答案 1 :(得分:0)

以下是我要找的内容:

private void browsebtn_Click(object sender, EventArgs e)
{
    OpenFileDialog of = new OpenFileDialog();
    of.Filter = "Excel Files|*.xls;*.xlsx";
    if (of.ShowDialog() == DialogResult.OK)
    {
        textBoxBrowse.Text = of.FileName;
    }
}   

private void textBoxBrowse_TextChanged(object sender, EventArgs e)
{

}

这是我添加的代码并且有效。谢谢html.inn的回答!好东西!