CheckedListBox - 如何从checklistbox中的目录中选择文本文件 [英] CheckedListBox - How to select the text file from directory from checklistbox

查看:321
本文介绍了CheckedListBox - 如何从checklistbox中的目录中选择文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want to use a CheckedListBox in an application where each item in the ListBox is the name of a folder on my hard drive and for the purpose of reading and writing text files to and from each of these folders I want to ensure that multiple item (a folder) can be selected at any time in the CheckedListBox



Thanks for reading :-)







private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            CheckedListBox.CheckedIndexCollection checkedIndices = checkedListBox1.CheckedIndices;

            if (checkedIndices.Count > 0)
            {
                if (checkedIndices[0] != e.Index)
                {
                    checkedListBox1.ItemCheck -= checkedListBox1_ItemCheck;
                    checkedListBox1.SetItemChecked(checkedIndices[0], true);
                    checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
                }
                else
                {
                    // the user is unchecking the currently checked item, so deselect it...
                    checkedListBox1.SetSelected(e.Index, false);
                }
            }
        }





我可以选择多个文件但我的主要问题是如何从目录中选择文本文件..



如何通过C#中的代码实现此目的?



I can select multiple files but my main question is How to select text files from the directory..

How can I achieve this via code in C#?

推荐答案

试试这个: -



选定的活动: -



Try this :-

On Selected event:-

string[] filePaths = Directory.GetFiles(@"FolderPath", "*.txt",
                                         SearchOption.AllDirectories);







遍历此filePaths数组并获取文件名。




Loop through this filePaths array and get file names.


这篇关于CheckedListBox - 如何从checklistbox中的目录中选择文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆