user2736424
user2736424

Reputation: 163

C# Textbox Failing to Update

I'm attempting to use FolderBrowserDialog to allow a selected folder to be stored in a string, I then want that string to populate a text box on the application interface. I can select the file box all well and good and the directory paths are being stored correctly, however they are not auto populating the text box. If I try to type something into the textbox it will show the string that I want there. Here's the code for the button I'm using to get the directory:

 private void openJPEGButton_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog jpegDialog = new FolderBrowserDialog();
        string selectedFolder = @"C:\";
        jpegDialog.SelectedPath = selectedFolder;

        if (jpegDialog.ShowDialog() == DialogResult.OK)
        {
            originDirectory = jpegDialog.SelectedPath;
            textBox1.Update(); 

        }

And here's the code for the textbox,

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        textBox1.Text = originDirectory;
    }

Thanks!

Upvotes: 0

Views: 94

Answers (3)

dotmido
dotmido

Reputation: 1384

You will just need to assign Text property to this dialog value..

textbox1.Text = jpegDialog.SelectedPath;

Upvotes: 0

user2509901
user2509901

Reputation:

If i get you well, you want to extract the path and show it in a textbox. You can use

private void openJPEGButton_Click(object sender, EventArgs e)
{
    FolderBrowserDialog jpegDialog = new FolderBrowserDialog();
    string selectedFolder = @"C:\";
    jpegDialog.SelectedPath = selectedFolder;

    if (jpegDialog.ShowDialog() == DialogResult.OK)
    {
         textbox1.Text = jpegDialog.SelectedPath;
    }
}

You could also use this

private void textBox1_TextChanged(object sender, EventArgs e)
{
    textBox1.Text = jpegDialog.SelectedPath;
}

Upvotes: 1

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48558

Simply set the SelectedPath to textbox's text.

if (jpegDialog.ShowDialog() == DialogResult.OK)
{
    originDirectory = jpegDialog.SelectedPath;
    textBox1.Text = jpegDialog.SelectedPath;
}

Upvotes: 0

Related Questions