问题描述
我有一个CheckedListBox( CheckedListBox1
)和一个CheckBox( CheckBox1
)。
我想在同一个 CheckedListBox1
中显示所有已检查的项目,
假设<$检查c $ c> checkBox1 然后显示在 CheckedListBox1
中检查了哪些项目,如果未选中复选框,则显示所有项目(之前检查过的项目和剩下的了。
我想做的例子:
CheckedListBox1列表项目是:
A未选中
B已检查
C已检查
D未选中
如果选中复选框,则仅显示从CheckedListbox1到CheckedListbox1的已检查项目。
这里的项目是:
B和C
如果未选中复选框,则显示所有项目在CheckdeListBox1中。
这里的项目是:
A未选中
B已检查
C已检查
D未选中
我在VB6中看到它添加组件对话框,其中所有组件都在一个checkedlistbox,并且有一个复选框(文本是仅限所选项目)。如果我们选中该复选框,那么它将仅显示checkedlistbox中的已检查项目。
如何做到这一点??
感谢高级!!!
问候:
Jayanta。
I have one CheckedListBox(CheckedListBox1
) and one CheckBox(CheckBox1
).
I want to show all the checked items in a same CheckedListBox1
,
suppose when checkBox1
is checked then show which items were checked in CheckedListBox1
, and if checkbox is unchecked then show all items(which checked before and rest of all).
Example of what i want to do :
CheckedListBox1 List Items are :
"A" unchecked
"B" Checked
"C" Checked
"D" Unchecked
if Checkbox is checked then shows only checked items from CheckedListbox1 to CheckedListbox1.
Here the items are :
"B" and "C"
if checkbox is unchecked then shows all items which were in CheckdeListBox1.
Here the items are :
"A" unchecked
"B" Checked
"C" Checked
"D" Unchecked
I saw it in VB6 Add component dialog Box, where all the component was in one checkedlistbox, and there was a checkbox(which text is "Selected Items only").if we checked that checkbox then it will shows only checked items in checkedlistbox.
How to do this??
thanks in Advanced!!!
Regards :
Jayanta.
推荐答案
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
bool flag = (sender as CheckBox).Checked;
for (int i = 0; i < checkedListBox1.Items.Count; i++)
checkedListBox1.SetItemChecked(i, flag);
}
// file: MyCheckListBox
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test_Checklist
{
public class MyCheckListBox : System.Windows.Forms.CheckedListBox
{
List<object> all = new List<object>();
List<int> chks = new List<int>();
bool showChecked = false;
public MyCheckListBox()
: base()
{
this.ItemCheck += MyCheckListBox_ItemCheck;
}
void MyCheckListBox_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
{
if (!this.showChecked)
return;
var o = this.Items[e.Index];
int oi = all.IndexOf(o);
if (e.NewValue == System.Windows.Forms.CheckState.Checked)
chks.Add(oi);
else
chks.Remove(oi);
}
public void ShowOnlyChecked(bool val)
{
this.showChecked = val;
if (val)
{
all.AddRange(this.Items.Cast<object>());
chks.AddRange(this.CheckedIndices.Cast<int>());
for (int i = 0; i < all.Count; i++)
if (!chks.Contains(i))
this.Items.Remove(all[i]);
}
else
{
this.Items.Clear();
this.Items.AddRange(this.all.ToArray());
this.chks.ForEach(i1 => this.SetItemChecked(i1, true));
all.Clear();
chks.Clear();
}
}
}
}
和示例表格:
and an example form:
// file: Form1.cs
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;
namespace Test_Checklist
{
public partial class Form1 : Form
{
MyCheckListBox lb = new MyCheckListBox();
public Form1()
{
InitializeComponent();
this.SuspendLayout();
this.Controls.Add(lb);
this.lb.Items.AddRange(new string[] { "item1", "item2", "item3" });
this.lb.Left = this.checkBox1.Left;
this.lb.Top = this.checkBox1.Top + 20;
this.ResumeLayout(false);
this.checkBox1.CheckState = CheckState.Unchecked;
this.checkBox1.CheckedChanged += checkBox1_CheckedChanged;
}
void checkBox1_CheckedChanged(object sender, EventArgs e)
{
switch (this.checkBox1.CheckState)
{
case CheckState.Checked:
this.lb.ShowOnlyChecked(true);
break;
case CheckState.Indeterminate:
case CheckState.Unchecked:
this.lb.ShowOnlyChecked(false);
break;
default:
break;
}
}
}
}
它没有设计师支持,但您可以将其转换为自定义控件。
It doesn't have designer support, but you can convert it into custom control.
这篇关于如何在同一个CheckedListBox中显示所有选中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!