嗨,各位新手,我正在尝试设置一个图形用户界面,所有我想要的图形用户界面做的是有一个简单的文件资源管理器与一个CheckedListBox来表示选定的文件。我可以让CheckedListBox显示并点击文件,但我不确定如何从这里继续,大多数教程都停留在这里,或者在树状视图和其他对我想要做的事情来说似乎不必要的事情上走得太远。下面是我的代码:
任何帮助都是非常感谢的,如果你们能给我指出正确的方向,那就太好了。
编辑:
重新表述我的问题:
我希望用户通过CheckedListBox选择文件(用户输入在此停止),并将这些选择的文件放入我的代码可以操作的列表中。
在我的第一次foreach循环(将所选目录中的所有文件添加到CheckedListBox中以供用户选择)之后,我不确定如何完成此操作。
第二个foreach循环就是一种尝试,它操作文件,使它们在被选择后输出其文件名。然而,没有Messagebox出现,我认为这是用户选择文件和代码试图操作所述文件之间的脱节。
第二次编辑:
我想我弄明白了,我做了第二个按钮,从这里看起来我可以随心所欲地操作所选的文件。
目前,代码的工作方式与我期望的一样。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace SelectFiles
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
checkedListBox1.CheckOnClick = true;
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
MessageBox.Show(fbd.SelectedPath);
checkedListBox1.Items.Clear();
string[] files = Directory.GetFiles(fbd.SelectedPath);
foreach (string file in files)
{
checkedListBox1.Items.Add(file);
}
}
private void button2_Click_1(object sender, EventArgs e)
{
List<string> list_all_excelfiles = new List<string>();
foreach (string item in checkedListBox1.CheckedItems)
{
list_all_excelfiles.Add(item);
MessageBox.Show(Path.GetFileName(item));
}
}
}}
发布于 2018-06-13 07:05:59
首先,我建议您为每个项目分配Value member和Display member。
中使用
为此,首先创建简单的自定义类
public class Int_String
{
public int _int { get; set; }
public string _string { get; set; }
}Be careful because get; set; part is important to be there since if not code will not work
现在,您需要做的是创建具有自定义类的项目列表,如下所示
class YourForm : Form
{
List<Int_String> myList = new List<Int_String>(); //Create list of our custom class
public YourForm()
{
PopulateMyList();
}
private void PopulateMyList()
{
//Here read from database or get data somehow and populate our list like this
//I will populate it manually but you do it in foreach loop
myList.Add(new Int_String { _int = 0, _string = "First Item" });
myList.Add(new Int_String { _int = 1, _string = "Second Item" });
myList.Add(new Int_String { _int = 2, _string = "Third Item" });
}
}之后,您需要将此列表分配给您的checkedListBox,如下所示:
public YourForm()
{
PopulateMyList();
checkedListBox1.DataSource = myList;
checkedListBox1.DisplayMember = "_string";
checkedListBox1.ValueMember = "_int";
}现在,当您可以像这样处理选中的项目时:
for(int i = 0; i < checkedListBox1.Items.Count; i++)
{
if(checkedListBox1.Items[i].CheckedState == CheckState.Checked)
{
int itemValueMember = (checkedListBox1.Items[i] as Int_String)._int;
int itemDisplayMember = (checkedListBox1.Items[i] as Int_String)._string;
//Use these two vars for whatever you need
}
}两个重要的提示:
checkedBox组件有DisplayMember或ValueMember,但它也不会显示错误。原因是他们故意隐藏了什么原因,但它会工作。Display和Value成员分配给winforms中的许多组件,但由于某些原因,checkedListBox是特定的。它是特定的,因为您必须首先将DataSource赋值给它,然后告诉它checkedListBox.DisplayMember = "_string" .....。对于新来的人,你会问为什么这很重要。简单答案是创建用于测试的自定义列表,并在其中添加10k项,然后首先声明datasource,然后声明Display和Value成员。测试表单需要加载多长时间(脱离冻结状态)。之后,执行相同的操作,但首先声明Display和Value成员,然后分配datasource并再次测试。我是在没有测试的情况下从head那里说出这一点的,但在之前,当我需要大约5k行的第一个解决方案时,我花了大约30秒,而第二个<1秒。如果你想了解更多信息,可以在谷歌上搜索一下,但就目前而言,这对你来说是非常有用的信息。https://stackoverflow.com/questions/50818836
复制相似问题