我编写了一个打开文件对话框的函数,但它不会返回文件路径+文件名。
我缺少什么?
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);
}
}
答案 0 :(得分: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 "";
}