我有窗口应用程序。现在,在运行时我在该页面内添加一个页面和复选框列表。
我的代码是:
Form inputBox = new Form();
inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
inputBox.ClientSize = size;
inputBox.Text = "Doc Selection";
inputBox.StartPosition = FormStartPosition.CenterScreen;
inputBox.ControlBox = false;
System.Windows.Forms.CheckedListBox DocTypeChkList = new CheckedListBox();
DocTypeChkList.Location = new System.Drawing.Point(15, 10);
DocTypeChkList.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
DocTypeChkList.Items.Add("B");
DocTypeChkList.Items.Add("P");
DocTypeChkList.Items.Add("Other");
DocTypeChkList.SelectionMode = SelectionMode.One;
inputBox.Controls.Add(DocTypeChkList);
现在,在运行时用户可以选中多个复选框...我希望一次只选中一个复选框而不是多个...我已经给出了选择模式“ONE” ..
你能告诉我吗。我缺少什么????
谢谢
最佳答案
CheckedListBox
允许用户选中多个复选框,这就是设计此控件的目的。 SelectionMode
只是表示您可以选择一个或多个项目(如果项目高亮显示未选中,则认为该项目已选中)。因此,要解决此问题,您必须添加一些代码来处理 ItemCheck
事件。机制很简单。
int lastCheckedIndex = -1;
//ItemCheck event handler for your checkedListBox1
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.Index != lastCheckedIndex)
{
if(lastCheckedIndex != -1)
checkedListBox1.SetItemCheckState(lastCheckedIndex, CheckState.Unchecked);
lastCheckedIndex = e.Index;
}
}
//To register event
checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
关于C# 窗口应用程序复选框列表选择模式一不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18415611/