【发布时间】: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