【问题标题】:How to set winform checkboxlist items checked when clicked on checkbox or its text?单击复选框或其文本时如何设置选中的winform checkboxlist项目?
【发布时间】:2019-01-19 08:49:40
【问题描述】:

我在我的窗口窗体应用程序中使用复选框列表控件。当用户点击任何一行时,该行变为蓝色,我们通过以下代码获取其检查状态

private void clbRoles_ItemCheck(object sender, ItemCheckEventArgs e)
{
.
.
.
}

如何仅在单击复选框或其文本时选择复选框?

不是通过单击复选框文本区域外的行。

【问题讨论】:

  • 选择不同于检查。问题是什么?请详细说明问题和预期行为。
  • 在我看来,如果我理解正确,您有一个 row_click 事件会更改框的值?

标签: c# winforms checkboxlist


【解决方案1】:

您的代码中的某些东西(可能是一个事件处理程序,正如@markorial 所建议的那样)导致了这种行为。请注意,使用如下代码,您只能单击复选框本身(或其标签)才能激活它。

class CheckBoxesWindow: Form {
    public Window()
    {
        this.Build();

        var row1 = new ListViewItem( "Do the dishes", 0 );
        row1.SubItems.Add( "High" );
        row1.Checked = true;

        var row2 = new ListViewItem( "Wash sheets", 1 );
        row2.SubItems.Add( "Average" );

        this.lvView.Items.AddRange( new ListViewItem[]{ row1, row2 } );
    }

    void Build()
    {
        var lv = this.BuildListView();

        this.Controls.Add( lv );
        this.Show();
    }

    ListView BuildListView()
    {
        int width = this.ClientSize.Width;
        var toret = new ListView{ Dock = DockStyle.Fill };

        toret.View = View.Details;
        toret.CheckBoxes = true;
        toret.Columns.Add( "Desc", (int) ( width * 0.70 ), HorizontalAlignment.Center );
        toret.Columns.Add( "Priority", (int) ( width * 0.30 ), HorizontalAlignment.Right );

        this.lvView = toret;
        return toret;
    }

    ListView lvView;
}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多