0

我写了一个可以打开文件对话框的函数,但它不会返回文件路径+文件名。

我错过了什么?

private void browseButton_Click(object sender, EventArgs e)
    {
        browseDatabase(accessDatabaseTextbox.Text, "mdb bestanden|*.mdb");
    }

    private void browsebutton2_Click(object sender, EventArgs e)
    {
        browseDatabase(klantenDatabaseTextbox.Text, "accdb bestanden|*.accdb");
    }

private void browseDatabase(string textbox, string filter)
    {
        openFileDialogDB.Filter = filter;

        if (openFileDialogDB.ShowDialog() == DialogResult.OK)
        {
            string DBfile = openFileDialogDB.FileName;
            if (System.IO.File.Exists(DBfile))
            {
                textbox = DBfile;
            }
            else
            {
                MessageBox.Show("Selected file doesn't exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        else
        {
            MessageBox.Show("No file selected.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
4

1 回答 1

2

我猜你需要选择的文件最终出现在你的accessDatabaseTextbox. 在这种情况下,您做错了,字符串是按值传递的。试试这个

private void browsebutton2_Click(object sender, EventArgs e)
{
    klantenDatabaseTextbox.Text=browseDatabase( "accdb bestanden|*.accdb");
}

private string browseDatabase(string filter)
{
    openFileDialogDB.Filter = filter;

    if (openFileDialogDB.ShowDialog() == DialogResult.OK)
    {
        string DBfile = openFileDialogDB.FileName;
        if (System.IO.File.Exists(DBfile))
        {
            return DBfile;
        }
        else
        {
            MessageBox.Show("Selected file doesn't exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    else
    {
        MessageBox.Show("No file selected.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
 return "";
}
于 2012-10-26T16:30:09.553 回答