简体   繁体   English

使用C#Windows应用程序填充checkedListBox

[英]Populating checkedListBox using c# windows application

I want to populate the checkedListBox control with file names from a directory which i can do now using the following code. 我想用目录中的文件名填充checkedListBox控件,现在我可以使用以下代码进行操作。

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(txtPath.Text.ToString());
System.IO.FileSystemInfo[] files = di.GetFileSystemInfos();
checkedListBox1.Items.AddRange(files);

But when the checkbox is selected, i want to get the path of file name. 但是,当选中该复选框时,我想获取文件名的路径。 How can i do this ? 我怎样才能做到这一点 ?

Use the SelectedIndexChanged event of the checkbox item to see which item was checked, and pull the path down from the event args passed into the event handler. 使用复选框项目的SelectedIndexChanged事件来查看选中了哪个项目,然后从传递到事件处理程序的事件args中拉下路径。

You can then use ((System.IO.FileSystemInfo)checkedListBox1.SelectedItem).FullName; 然后可以使用((System.IO.FileSystemInfo)checkedListBox1.SelectedItem).FullName; to get the full file path and name. 获取完整的文件路径和名称。 And if needed, you can use regex to extract the path from the filename. 并且,如果需要,您可以使用正则表达式从文件名中提取路径。

You can find multiple paths using the SelectedIndices property of the checkedListBox control. 您可以使用checkedListBox控件的SelectedIndices属性找到多个路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM