选择带有Checked Combobox的所有选项,用于WInForm C# [英] Select All Option with Checked Combobox For WInForm C#

查看:675
本文介绍了选择带有Checked Combobox的所有选项,用于WInForm C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Windows应用程序中,我通过以下代码项目文章选择了带复选框的组合框。



A-ComboBox-with-a-CheckedListBox-as-a-Dropdown



它的工作正常,但是客户需求就像在复选框中应该选择全部选项,如果我在组合框中选择全选选项,则应选中所有复选框项目。



我尝试过ccb_ItemCheck事件但没有工作。



 private void ccb_ItemCheck(object sender,ItemCheckEventArgs e) 
{}





请帮我完成此操作。



提前谢谢

解决方案

我建​​议你不要在生产代码的链接中使用解决方案:架构存在严重缺陷;它使用Form作为Control的内部部分,继承自ComboBox,就像用大炮射击鼠标一样。



如果你必须使用该代码,那么为什么不在ListView中放置另一个项目......在顶部或底部...



假设您开始取消选中某些项目,该项目会读取全部检查,如果您检查,则检查所有项目,文本更改为全部取消选中或清除。



监控用户的选择/检查/取消选中活动:任何项目的时刻......但是此特殊项目...未选中更改取消选中特殊项目并将其文本更改为全部检查,依此类推。



我建议你构建自己的复合UserControl,它将组合一个ComboBox和一个ListView。这并不难。并且,如果您自己创建UserControl,则可以向其添加一个Button来执行check-all或uncheck-all功能,如果这是您真正想要的功能。


以下代码将是绝对正常工作.........





关于itemcheck事件类型代码

cmbMOPTypeADV是checkedcombobox的控件ID



private void cmbMOPTypeADV_ItemCheck(object sender,ItemCheckEventArgs e)

{

string objsender =((System.Windows.Forms.ListBox)(sender))。文本;

int count = cmbMOPTypeADV.CheckedIndices.Count;

if(e.NewValue = = CheckState.Checked)

{

count = count + 1;

}

if(objsender.ToLower ()==all&& e.NewValue == CheckState.Unchecked)

{

cmbMOPTypeADV.ItemCheck - = new System.Windows.Form s.ItemCheckEventHandler(this.cmbMOPTypeADV_ItemCheck);

UncheckedAll(cmbMOPTypeADV);

cmbMOPTypeADV.ItemCheck + = new System.Windows.Forms.ItemCheckEventHandler(this.cmbMOPTypeADV_ItemCheck);

}

else if(objsender.ToLower()==all&& e.NewValue == CheckState.Checked)

{

cmbMOPTypeADV.ItemCheck - = new System.Windows.Forms.ItemCheckEventHandler(this.cmbMOPTypeADV_ItemCheck);

CheckedAll(cmbMOPTypeADV);

cmbMOPTypeADV.ItemCheck + = new System.Windows.Forms.ItemCheckEventHandler(this.cmbMOPTypeADV_ItemCheck);

}

else if((objsender.ToLower()!=&& objsender.ToLower( )!=all)&& e.NewValue == CheckState.Unchecked)

{

this.cmbMOPTypeADV.ItemCheck - = new System.Windows.Forms.ItemCheckEventHandler(这个.cmbMOPTypeADV_ItemCheck);

cmbMOPTypeADV.SetItemChecked(0,false);

this.cmbMOPTypeADV.ItemCheck + = new System.Windows.Forms.ItemCheckEventHandler(this.cmbMOPTypeADV_ItemCheck);

}

else if((objsender.ToLower()!=&& objsender.ToLower()!=all)&& count ==(cmbMOPTypeADV.Items .Count - 1))

{

cmbMOPTypeADV.ItemCheck - = new System.Windows.Forms.ItemCheckEventHandler(this.cmbMOPTypeADV_ItemCheck);

cmbMOPTypeADV.SetItemChecked(0,true);

cmbMOPTypeADV.ItemCheck + = new System.Windows.Forms.ItemCheckEventHandler(this.cmbMOPTypeADV_ItemCheck);

}

}



实现以下两种方法

public void CheckedAll(CheckedComboBox objCombo)

{

for(int i = 0;我< objCombo.Items.Count; i ++)

{

objCombo.SetItemChecked(i,true);

}

}



public void UncheckedAll(CheckedComboBox objCombo)

{

for(int i = 0; i< objCombo.Items。数; i ++)

{

objCombo.SetItemChecked(i,false);

}

}

In my windows application, I have combobox with check box option by following Code Project Article.

A-ComboBox-with-a-CheckedListBox-as-a-Dropdown

Its working fine but client needs like there should be Select All option in that Check box, If i select Select All option in combobox all check box items should be checked.

I have tried with ccb_ItemCheck event but not worked.

private void ccb_ItemCheck(object sender, ItemCheckEventArgs e) 
{}



Please help me to complete this operation.

Thanks in advance

解决方案

I'd advise you not to use the solution in the link in production code: the architecture is seriously flawed; it uses a Form as an internal part of a Control that inherits from ComboBox which is like shooting a mouse with a cannon.

If you have to use that code, then why not just put another item in the ListView ... at the top, or the bottom ...

Assuming you start off with some items unchecked, that item reads "Check All," and, if you check it, all items are checked, and the text changes to "Uncheck All" or "Clear."

Monitor the user's selection/check/uncheck activity: the moment any item ... but this special item ... is unchecked change uncheck the special item and change its text to "Check All," and so forth.

I suggest you build your own composite UserControl which will combine a ComboBox and a ListView. It's not that difficult to do. And, if you make your own UserControl, you can add a Button to it to perform a check-all or uncheck-all function, if that's what you really want.


Following code will be definetly work.........


On itemcheck event type following code
cmbMOPTypeADV is the control ID of checkedcombobox

private void cmbMOPTypeADV_ItemCheck(object sender, ItemCheckEventArgs e)
{
string objsender = ((System.Windows.Forms.ListBox)(sender)).Text;
int count = cmbMOPTypeADV.CheckedIndices.Count;
if (e.NewValue == CheckState.Checked)
{
count = count + 1;
}
if (objsender.ToLower() == "all" && e.NewValue == CheckState.Unchecked)
{
cmbMOPTypeADV.ItemCheck -= new System.Windows.Forms.ItemCheckEventHandler(this.cmbMOPTypeADV_ItemCheck);
UncheckedAll(cmbMOPTypeADV);
cmbMOPTypeADV.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.cmbMOPTypeADV_ItemCheck);
}
else if (objsender.ToLower() == "all" && e.NewValue == CheckState.Checked)
{
cmbMOPTypeADV.ItemCheck -= new System.Windows.Forms.ItemCheckEventHandler(this.cmbMOPTypeADV_ItemCheck);
CheckedAll(cmbMOPTypeADV);
cmbMOPTypeADV.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.cmbMOPTypeADV_ItemCheck);
}
else if ((objsender.ToLower() != "" && objsender.ToLower() != "all") && e.NewValue == CheckState.Unchecked)
{
this.cmbMOPTypeADV.ItemCheck -= new System.Windows.Forms.ItemCheckEventHandler(this.cmbMOPTypeADV_ItemCheck);
cmbMOPTypeADV.SetItemChecked(0, false);
this.cmbMOPTypeADV.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.cmbMOPTypeADV_ItemCheck);
}
else if ((objsender.ToLower() != "" && objsender.ToLower() != "all") && count == (cmbMOPTypeADV.Items.Count - 1))
{
cmbMOPTypeADV.ItemCheck -= new System.Windows.Forms.ItemCheckEventHandler(this.cmbMOPTypeADV_ItemCheck);
cmbMOPTypeADV.SetItemChecked(0, true);
cmbMOPTypeADV.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.cmbMOPTypeADV_ItemCheck);
}
}

Implement following two methods
public void CheckedAll(CheckedComboBox objCombo)
{
for (int i = 0; i < objCombo.Items.Count; i++)
{
objCombo.SetItemChecked(i, true);
}
}

public void UncheckedAll(CheckedComboBox objCombo)
{
for (int i = 0; i < objCombo.Items.Count; i++)
{
objCombo.SetItemChecked(i, false);
}
}


这篇关于选择带有Checked Combobox的所有选项,用于WInForm C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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