Checkedlistbox

M

MFRASER

How can I turn on and off what is selected? I have a checked listbox I want
Index 0 = "All" and each of the other nodes to be a number. If the user
selects all I want to uncheck all the other items and visa versa.

Here is what I have on the checked event. I can not get this to compile.

Thanks
/// <summary>

/// Event fires when user checks Item

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void lstIteration_ItemCheck(object sender,
System.Windows.Forms.ItemCheckEventArgs e)

{

if (e.Index == 0)

{

this.lstIteration.ItemCheck -= new
System.Windows.Forms.ItemCheckEventHandler(this.lstIteration_ItemCheck);

this.lstIteration.ClearSelected();

this.lstIteration..Items[0].Checked =
System.Windows.Forms.CheckState.Checked; //Line fails on compile

this.lstIteration.ItemCheck += new
System.Windows.Forms.ItemCheckEventHandler(this.lstIteration_ItemCheck);

}

else

{

this.lstIteration.ItemCheck -= new
System.Windows.Forms.ItemCheckEventHandler(this.lstIteration_ItemCheck);

this.lstIteration.Items[0].Checked =
System.Windows.Forms.CheckState.Unchecked; //Line fails on compile

this.lstIteration.ItemCheck += new
System.Windows.Forms.ItemCheckEventHandler(this.lstIteration_ItemCheck);

}

}
 
M

MFRASER

I got it to work like this

if (e.Index == 0)

{

this.lstIteration.ItemCheck -= new
System.Windows.Forms.ItemCheckEventHandler(this.lstIteration_ItemCheck);

this.lstIteration.ClearSelected();

e.NewValue =System.Windows.Forms.CheckState.Checked;

this.lstIteration.ItemCheck += new
System.Windows.Forms.ItemCheckEventHandler(this.lstIteration_ItemCheck);

}

else

{

// First show the index and check state of all selected items.

foreach(int indexChecked in lstIteration.CheckedIndices)

{

if(indexChecked == 0 && lstIteration.GetItemCheckState(indexChecked) ==
System.Windows.Forms.CheckState.Checked)

{

this.lstIteration.ItemCheck -= new
System.Windows.Forms.ItemCheckEventHandler(this.lstIteration_ItemCheck);

lstIteration.SetItemCheckState(indexChecked,
System.Windows.Forms.CheckState.Unchecked);

this.lstIteration.ItemCheck += new
System.Windows.Forms.ItemCheckEventHandler(this.lstIteration_ItemCheck);

break;

}

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top