Filtering a texbox with a combobox

2019-07-13 07:31发布

Ok so I have to create a form that takes the contents of a folder and lists it in a textbox (yes a textbox not a list box!)

I then have to filter this textbox using a combobox which contains all the extensions of the folder (e.g. if i choose ".txt" in the combobox the textbox should filter to show all the text files only!)

I've managed to do everything bar getting the combobox to filter the textbox. I can't find any help anywhere online and am new to programming so please help!

By the way i'm using c# and this is all using windows forms

1条回答
孤傲高冷的网名
2楼-- · 2019-07-13 08:22

On the selected index changed event of the combo box, I'd rewrite the information in the text box based off of the filter selected. You can use System.IO.DirectoryInfo to filter your directory given a file extension.

StringBuilder fileNames = new StringBuilder();
DirectoryInfo dInfo = new DirectoryInfo(<string yourDirectory>);                                 
FileInfo[] fileInfo = dInfo.GetFiles("*" + <string yourChosenFileExtension>);
foreach (FileInfo file in fileInfo)
{  
   fileNames.Append(file.Name);
}
yourTextBox.Text = fileNames.ToString();
查看更多
登录 后发表回答