Question

I don't know if this is possible at all, but if someone knows how to achive this it will be geat. I have a check-box-list in winform and I need some check-boxes to be semi-checked (e.g. the check-box is not checked and not not checked). Is it even possible to do this in winforms check-box-list ? If not, is it possible to achive this with a regular check-box and not a check-box-list ?

Was it helpful?

Solution

This can also be achieved on the CheckListBox but you have to set the indeterminate state yourself, from MSDN:

The CheckedListBox object supports three states through the CheckState enumeration: Checked, Indeterminate, and Unchecked. You must set the state of Indeterminate in the code because the user interface for a CheckedListBox does not provide a mechanism to do so.

There is also a code example:

  // Adds the string if the text box has data in it. 
  private void button1_Click(object sender, System.EventArgs e)
  {
     if(textBox1.Text != "")
     {
        if(checkedListBox1.CheckedItems.Contains(textBox1.Text)== false)
           checkedListBox1.Items.Add(textBox1.Text,CheckState.Checked);  // here you can set CheckState.Indeterminate!
        textBox1.Text = "";
     }
  }

for reference: http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox%28v=vs.110%29.aspx

OTHER TIPS

private void button1_Click(object sender, EventArgs e)
{
    int index = checkedListBox1.Items.Add("test");
    checkedListBox1.SetItemCheckState(index, CheckState.Indeterminate);
}

Indeterminate

Setting the indeterminate state when clicking it:

void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    switch (e.CurrentValue)
    {
        case CheckState.Checked:
            e.NewValue = CheckState.Unchecked;
            break;

        case CheckState.Indeterminate:
            e.NewValue = CheckState.Checked;
            break;

        case CheckState.Unchecked:
            e.NewValue = CheckState.Indeterminate;
            break;
    }
}

It's possible with a checkbox. Set the ThreeState property to true:

checkBox1.ThreeState = true;

Gets or sets a value indicating whether the CheckBox will allow three check states rather than two.

CheckState can be:

CheckState.Checked 
CheckState.Indeterminate
CheckState.Unchecked

Example:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();

        CheckState state = checkBox1.CheckState;

        switch (state)
        {
        case CheckState.Checked:
        case CheckState.Indeterminate:
        case CheckState.Unchecked:
            {
            MessageBox.Show(state.ToString());
            break;
            }
        }

        MessageBox.Show(checkBox1.Checked.ToString());
    }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top