質問

I'm new to programming and am using C# and Visual Studio Express 2012. I am creating a windows form and have inserted a button which runs open file dialog when clicked. I have a text box on the form that I'd like to have show the file path of the file that the user selected. I have found some code examples on this site but struggle to understand where they should be placed in the code structure as the examples are often standalone snippets. I hope its not too dumb a question!

Thanks in advance

Lee

The answer in case it's of use to anyone was.......

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        using (FileDialog fileDialog = new OpenFileDialog())
        {
            if (DialogResult.OK == fileDialog.ShowDialog())
            {
                string filename = fileDialog.FileName;

                textBox1.Text = fileDialog.FileName;
            }
        }
    }
}
役に立ちましたか?

解決

Your OpenFileDialog has property FileName that contains the path of the selected file, assign that to your TextBox.Text

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
    yourTextBox.Text = openFileDialog.FileName;            
}

他のヒント

OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
if(openFileDialog1.ShowDialog() == DialogResult.OK)
    textbox.text = openFileDialog1.FileName;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top