本文主要是利用ComboBox对上一篇模糊查询的简单化。上一篇是不利用属性实现的模糊查询(完整版)有兴趣的可以去访问。
本文依旧是给一个Button按钮,可以选择本地文件夹,将文件名输出在ComboBox下拉表中。
之所以说是阉割版是因为AutoComplete属性只能实现从左往右的查询,不能有间隔,
代码入下:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> list = new List<string>();
private static void cb_buttonshow(string direpath, List<string> list)
{
DirectoryInfo di = new DirectoryInfo(direpath);
FileSystemInfo[] files = di.GetFileSystemInfos();
foreach (FileSystemInfo file in files)
{
if (file is DirectoryInfo)
{
cb_buttonshow(file.FullName, list);
}
if (file is FileInfo)
{
string name = file.Name;
list.Add(name);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
cb_buttonshow(folderBrowserDialog1.SelectedPath, list);
Form1_Load(sender, e);
}
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (string item in list)
{
comboBox1.Items.Add(item);
}
comboBox1.AutoCompleteCustomSource.AddRange(list.ToArray());
//光标在后
comboBox1.SelectionStart = comboBox1.Text.Length;
//下拉框自动显示
comboBox1.DroppedDown = true;
}
}
03-14
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交