繁体   English   中英

C# 带有单选按钮行为的选中列表框

[英]C# checkedlistbox with radiobutton behavior

最近几天我一直在寻找解决这个问题的方法,但还没有找到合适的解决方案。 我制作了一个带有选中列表框的 WinForm,它必须表现为单选按钮组。 我知道这不是可取的,但我自己没有选择这个。 我正在尝试扩展我对 WinForm 的了解,所以我抓住了一个程序并尝试模仿这种行为,所以请留在我身边。

我想要在我的选中列表框中的单选按钮属性:

  • 您只能检查一项。 (很容易做到,见下面的代码)
  • 始终检查一项。 (这是我问题的根源)
  • 当您单击复选框/项目符号时,它会一键检查,单击文本不会改变任何内容。 (我也想出了一个)

我需要的 Checkedlistbox 属性:

  • 复选框后面的可选文本。 (用于重命名和删除列表中的项目)

我大部分时间都在工作,除非选择了一个项目而未选中另一个项目。 那时可以取消选中列表中的每个项目。

这是 ItemCheck 事件的代码。

private string _checkedName = "";
bool _autorizeCheck {get; set;}

private void OnItemCheck(object sender, ItemCheckEventArgs e)
{
            var list = sender as CheckedListBox;

            #region//Click checkbox
            if (!_authorizeCheck)
            {
                e.NewValue = e.CurrentValue;
            }
            #endregion

            #region//Radiobutton config
            if (e.CurrentValue == CheckState.Unchecked)
            {
                for (int i = 0; i < list.Items.Count; i++)
                {
                    if (i != e.Index)
                    {
                        _checkedName = list.Items[e.Index].ToString();
                        list.SetItemChecked(i, false);
                    }
                }
            }
            else if(e.CurrentValue == CheckState.Checked && _checkedName == list.Items[e.Index].ToString())
            {
                if (list.CheckedItems.Count == 1)
                {
                    e.NewValue = e.CurrentValue;
                }
            }
            #endregion
}

这是单击复选框的代码:

private void OnMouseDown_checkedListBox(object sender, MouseEventArgs e)
        {
            #region//Check if checkbox is clicked
            var checkedListBox = sender as CheckedListBox;
            var location = checkedListBox.PointToClient(Cursor.Position);
            for (int i = 0; i < checkedListBox.Items.Count; i++)
            {
                var rec = checkedListBox.GetItemRectangle(i);
                rec.Width = 16;

                if (rec.Contains(location))
                {
                    _authorizeCheck = true;
                    bool newValue = !checkedListBox.GetItemChecked(i);
                    checkedListBox.SetItemChecked(i, newValue); 
                    _authorizeCheck = false;

                    return;
                }
            }
            #endregion
        }

制作一个新的 class Form2,将其粘贴到上面:

public partial class Form2 : Form
{
    private int index = 0; //helper to track the selected index
    public Form2()
    {
        InitializeComponent();
        checkedListBox1.SetItemChecked(0, true); //check at least one item
    }

    //this fires before an item is checked. Really, Microsoft should perhaps have called it ItemCheckChanging
    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        if (index == -1) //programmatic change, exit early; see below
            return;
        else if (index == e.Index)
            e.NewValue = CheckState.Checked; //undo an attempt to uncheck the checked item
        else
        {
            var oldIndex = index; //what item do we want to uncheck
            index = -1; //prevent event handler firing again when we..
            checkedListBox1.SetItemChecked(oldIndex, false); //..uncheck th old
            index = e.Index; //track the newly checked
        }
    }
}

顺便说一句,如果您使用绑定到具有 2 列、一个布尔值和一个文本的表的数据网格视图,您的生活可能会更简单。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM