【问题标题】:C# checkedlistbox with radiobutton behavior具有单选按钮行为的 C# 选中列表框
【发布时间】:2021-06-12 11:40:07
【问题描述】:

最近几天我一直在寻找解决这个问题的方法,但还没有找到合适的解决方案。我制作了一个带有选中列表框的 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
        }

【问题讨论】:

  • 您正在寻找RadioButtonList
  • 谢谢 Reza,我用 CheckedListBox 修复了它,因为我想要 CheckedListBox 的美感

标签: c# winforms


【解决方案1】:

创建一个新类 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 列、一个布尔值和一个文本的表的数据网格视图,您的生活可能会更简单..

【讨论】:

    猜你喜欢
    • 2022-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 2021-05-11
    • 2012-05-15
    相关资源
    最近更新 更多