【问题标题】:How do I show a directory path in a textbox when selected by the user?用户选择时如何在文本框中显示目录路径?
【发布时间】:2015-02-16 17:14:50
【问题描述】:

首先,我使用 Visual Studio 2013 和 C# 编码来开发 Windows 窗体应用程序。我添加了“System.IO”命名空间。

当用户选择时,我需要在文本框中显示目录路径。 该代码适用于用户从弹出窗口中选择文件夹的位置 并按 OK 按钮,然后显示其中的文件数 该文件夹 - 但文件夹路径没有按我的意愿显示。

代码如下:

   private void button1_Click(object sender, EventArgs e)
    {
        //
        // This event handler was created by clicking the button in the application GUI.
        //
        DialogResult button1_Click = folderBrowserDialog1.ShowDialog();
        if (button1_Click == DialogResult.OK)
        {
            //
            // The user selected a folder and pressed the OK button.
            // A message pops up and identifies the number of files found within that folder.
            //
            string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
            MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
        }
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        string path;
        path = folderBrowserDialog1.SelectedPath;
        // folderBrowserDialog1.ShowDialog(); // NOT SURE ABOUT USING THIS!
        textBox1.Text = path;
    }

【问题讨论】:

  • 不要在 TextChanged 中尝试 - 该事件将在他们键入每个字符时触发,以确保您需要为部分键入的输入添加错误处理
  • 谢谢。我删除了它。

标签: c# winforms visual-studio-2013


【解决方案1】:

您可以将其添加到 button1_Click 方法的末尾(在 if 块内):

textBox1.Text = folderBrowserDialog1.SelectedPath;

【讨论】:

  • 成功了!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
相关资源
最近更新 更多