mck
mck

Reputation: 988

How to set winform checkboxlist items checked when clicked on checkbox or its text?

I am using checkbox list control in my window form application. When user click on any row then that row becomes blue and we get its checked status by following code

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

How to make selection of checkbox only on clicking on checkbox or its text ?

Not by clicking on row outside the checkbox text area.

Upvotes: 1

Views: 532

Answers (1)

Baltasarq
Baltasarq

Reputation: 12212

Something in your code (maybe an event handler, as @markorial suggests), is causing that behaviour. Take into account that with a code like the one below, you can only click in the checkbox itself (or its label) in order to activate it.

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;
}

Hope this helps.

Upvotes: 1

Related Questions