PandaNL
PandaNL

Reputation: 848

C# filename + filepath to textbox

I wrote a function that will open a file dialog, but it won't return me the filepath + filename.

What am i missing?

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);
        }
    }

Upvotes: 0

Views: 1068

Answers (1)

Anri
Anri

Reputation: 6265

I guess you need the selected file end up in your accessDatabaseTextbox. In this case you are doing it wrong, string is being passed by value. Try this

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 "";
}

Upvotes: 2

Related Questions