C# 窗口应用程序复选框列表选择模式一不起作用

标签 c# desktop-application checkboxlist

我有窗口应用程序。现在,在运行时我在该页面内添加一个页面和复选框列表。

我的代码是:

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/

相关文章:

c# - 方法 'System.Linq.Queryable.SelectMany System.Linq.IQueryable 的类型参数

haskell - Yesod中的复选框数组

c# - CheckboxList 显示所有选中的框

sql-server-2005 - 为一个 SQL 参数传递多个值

c# - .NET FileSystemWatcher 在移动文件时进入无限循环

c# - 需要 n 层架构反馈

Java JNLP 桌面快捷方式和图标

Python - 将按钮动态添加到 PyQt 中的布局

c# - DateTime.ToString 格式表达式是否受当前区域性影响?

wpf - 在 WPF 应用程序中做广告?