Checked list box Checked items Verification - c#

I have a Checkedlistbox that have the following information:
*************
*__All Cells*
*__Cell A *
*__Cell B *
*__Cell C *
*__Cell D *
*************
I want to check every field that i want, however i want to if I check the "All Cells" Checkbox, all the fields have to checked automatically, I already can do this. The part that need help if that I want that when i uncheck the "All Cells" checkbox all the cells are supposed to unchecked.
Here is the code that i use. Please help me with this.
private void Cells_CheckedListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (Cells_CheckedListBox.GetItemChecked(Cells_CheckedListBox.Items.IndexOf("All Cells")))
{
for (int i = 0; i < Cells_CheckedListBox.Items.Count; i++)
{
Cells_CheckedListBox.SetItemCheckState(i, CheckState.Checked);
}
}
}

You seem to be doing this in the wrong event altogether. You should handle the ItemCheck event.
private void Cells_CheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
{
int allIndex = Cells_CheckedListBox.Items.IndexOf("All Cells");
if (e.Index == allIndex)
{
for (int i = 0; i < Cells_CheckedListBox.Items.Count; i++)
{
if (i != allIndex)
Cells_CheckedListBox.SetItemCheckState(i, e.NewValue);
}
}
}

Related

How clear two RichTextBox using CheckedListBox and button

can anybody say me, how can I clear content inside RichTextBox using one button where: CheckedListBox - select which of rtb will be cleared?
I have problem with CheckedListBox - It works for one select position, but not for checked/marked.
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
string str = (string)checkedListBox1.Items[i];
if(str == "rtb1")
{
richTextBox1.Clear();
richTextBox1.Focus();
}
if(str == "rtb2")
{
richTextBox2.Clear();
richTextBox2.Focus();
}
}
}

multiple remove checkedlistbox items

Hi on this site I found how to delete multiple checkedbox objects in a checklistbox
How to delete multiple checked items from CheckedListBox?
But it's not working for me.
My previous partner who handled this project before I did saved something in Global.answer class. I have tried to modify that script like this:
for (int i = checkedListBoxAnswers.Items.Count - 1; i >= 0; i--) {
if (checkedListBoxAnswers.GetItemCheckState(i) == CheckState.Checked)
{
Global.answers.RemoveAt(checkedListBoxAnswers.SelectedIndex);
}
}
It can delete one checkbox correctly, but when I check for two or more checkboxes, it goes wrong...
I wonder how to do it correctly.
this is my delete button
private void buttonDelete_Click(object sender, EventArgs e)
{
if (checkedListBoxAnswers.SelectedIndices.Count < 1)
{
MessageBox.Show(this, "Please select answer to be deleted");
}
else
{
for (int i = checkedListBoxAnswers.Items.Count - 1; i >= 0; i--)
{
if (checkedListBoxAnswers.GetItemCheckState(i) == CheckState.Checked)
{
Global.answers.RemoveAt(checkedListBoxAnswers.SelectedIndex);
}
}
updateCheckListBoxAnswers();
}
}
in-fact here is correct code:
CheckedListBox.CheckedItemCollection checkedItemColl = checkedListBoxAnswers.CheckedItems;
for (int i = checkedItemColl.Count; i > 0; i--)
{
int index = checkedItemColl[i - 1];
checkedListBoxAnswers.Items.Remove(index);
}
Because you using this code
Global.answers.RemoveAt(checkedListBoxAnswers.SelectedIndex);
this will delete only the selected first item in the list, you must pass the index of selected Checked Item ,So it will delete that item only,
This is how you get all checked Item from List:
CheckedListBox.CheckedItemCollection checkedItemColl = checkedListBoxAnswers.CheckedItems;
for (int i = checkedItemColl.Count; i > 0; i--)
{
int index = checkedItemColl[i - 1];
checkedListBoxAnswers.Items.Remove(index);
}
remove comments and test the code. hope you will get benifit from it.
you can try this...
Global.answers.RemoveAt(i);
hie bro i have sample ... you can evolve tray this in your sweet home....
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
listBox1.Items.Add(checkedListBox1.Items[checkedListBox1.SelectedIndex]);
if (e.NewValue == CheckState.Unchecked)
listBox1.Items.Remove(checkedListBox1.Items[checkedListBox1.SelectedIndex]);
}
Your are Removing an Object so Parse that Object into Integer
by this way
Convert.ToInt32(Object)
in your case it will be..
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
listBox1.Items.Add(checkedListBox1.Items[checkedListBox1.SelectedIndex]);
if (e.NewValue == CheckState.Unchecked)
listBox1.Items.Remove(Convert.ToInt32(checkedListBox1.Items[checkedListBox1.SelectedIndex]));
}

C# Check All from a CheckedListBox without showing the "Check All" entry

I have a small program that has several checkedboxlists in VS2010. I wanted to allow a user to select all in one of the lists and came up with this looping structure...
private void CheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
{
Applications.Add(CheckedListBox1.Items[e.Index].ToString());
}
else if (e.NewValue == CheckState.Unchecked)
{
Applications.Remove(CheckedListBox1.Items[e.Index].ToString());
}
}
private void CheckedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (CheckedListBox1.SelectedIndex == 0)
{
for (int i = 1; i < CheckedListBox1.Items.Count; i++)
{
CheckedListBox1.SetItemChecked(i, CheckedListBox1.GetItemChecked(0));
}
}
else
{
if (!CheckedListBox1.GetItemChecked(CheckedListBox1.SelectedIndex))
{
CheckedListBox1.SetItemChecked(0, false);
}
}
}
The problem is this also puts the "Select All" checkbox into the output. Is there a way I can tweak the loop to not include the first checkbox (which is the "Select All" Check) or should I be going about this a different way?
Pretty unclear what "into the output" might mean. Using the SelectedIndexChanged event isn't very appropriate, the ItemCheck event signals checking an item. Try this instead:
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
if (e.Index == 0) {
for (int item = 1; item < checkedListBox1.Items.Count; item++) {
checkedListBox1.SetItemChecked(item, true);
}
e.NewValue = CheckState.Unchecked; // Prevent "Check All" from getting checked
}
}
If you want to use SelectedIndexChanged anyway then still keep this event handler to prevent the item from getting checked.

how to check only one item in checkedlistbox

I have a check list box control and I want to select only one item at a time and I am currently using this code to do the same.
private void CLSTVariable_ItemCheck(object sender, ItemCheckEventArgs e)
{
// Local variable
int ListIndex;
CLSTVariable.ItemCheck -= CLSTVariable_ItemCheck;
for (ListIndex = 0;
ListIndex < CLSTVariable.Items.Count;
ListIndex++)
{
// Unchecked all items that is not currently selected
if (CLSTVariable.SelectedIndex != ListIndex)
{
// set item as unchecked
CLSTVariable.SetItemChecked(ListIndex, false);
} // if
else
{
// set selected item as checked
CLSTVariable.SetItemChecked(ListIndex, true);
}
} // for
CLSTVariable.ItemCheck += CLSTVariable_ItemCheck;
}
this code is working fine.
but problem is that when I click again and again on selected item then that selected item should not be unchecked, means at least one item should be checked always...
I agree with commentators above - you should consider using radiobuttons. But if you really need CheckedListBox, then use this ItemChecked event handler instead:
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (checkedListBox1.CheckedItems.Count == 1)
{
Boolean isCheckedItemBeingUnchecked = (e.CurrentValue == CheckState.Checked);
if (isCheckedItemBeingUnchecked)
{
e.NewValue = CheckState.Checked;
}
else
{
Int32 checkedItemIndex = checkedListBox1.CheckedIndices[0];
checkedListBox1.ItemCheck -= checkedListBox1_ItemCheck;
checkedListBox1.SetItemChecked(checkedItemIndex, false);
checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
}
return;
}
}
Well, it was an answer to me! I couldn't get the above code to work in the checkedListBox1_ItemCheck. I had to modify a portion of it ans include it in the checkedListBox1_SelectedIndexChanged event. But I couldn't remove the original code all together. Here is what I've added...
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (checkedListBox1.CheckedItems.Count > 1)
{
Int32 checkedItemIndex = checkedListBox1.CheckedIndices[0];
checkedListBox1.ItemCheck -= checkedListBox1_ItemCheck;
checkedListBox1.SetItemChecked(checkedItemIndex, false);
checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
}
}
Which is basically, if you have more than 1 box checked, switch the last one for the new one. I'm curious why the original code didn't work. And why it has to be there for my new code to work? Thank you.
I found this code it work so well
private void chkboxmov_ItemCheck(object sender, ItemCheckEventArgs e)
{
for (int ix = 0; ix < chkboxmov.Items.Count; ++ix)
if (ix != e.Index)
chkboxmov.SetItemChecked(ix, false);
}
"at least one item should be checked always"
The current solution (the last one) allows items to be checked off. If your purpose is to select exactly one item at all times, use this as a MouseUp event,
private void ChklbBatchType_MouseUp(object sender, MouseEventArgs e)
{
int index = ((CheckedListBox)sender).SelectedIndex;
for (int ix = 0; ix < ((CheckedListBox)sender).Items.Count; ++ix)
if (index != ix) { ((CheckedListBox)sender).SetItemChecked(ix, false); }
else ((CheckedListBox)sender).SetItemChecked(ix, true);
}

How to disable checking checkbox column in DataGridView - Windows forms?

I have a checkbox column in a DataGridView and I want to validate whether the user can check a checkbox by counting the number of checkboxes they click and deciding
then to disable checking.
Can someone guide me how to do this effectively??
Quick code sample:
public partial class DGVCheckBoxTesting : Form
{
private const int MAX_CHECKS = 5;
public DGVCheckBoxTesting()
{
InitializeComponent();
this.dataGridView1.Columns.Add("IntColumn", "IntColumn");
this.dataGridView1.Columns.Add(new DataGridViewCheckBoxColumn { Name = "BoolColumn" });
for (int i = 0; i <= 10; i++)
{
this.dataGridView1.Rows.Add(i, false);
}
this.dataGridView1.CellContentClick += new DataGridViewCellEventHandler(dataGridView1_CellContentClick);
this.dataGridView1.CellContentDoubleClick += new DataGridViewCellEventHandler(dataGridView1_CellContentDoubleClick);
}
private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
this.ValidateCheckBoxState(e);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
this.ValidateCheckBoxState(e);
}
private void ValidateCheckBoxState(DataGridViewCellEventArgs e)
{
if (e.ColumnIndex != 1) //bool column
{ return; }
this.dataGridView1.EndEdit();
bool value = (bool)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
int counter = 0;
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
if (row.Cells[1].Value != null && (bool)row.Cells[1].Value)
{
counter++;
}
}
if (counter > MAX_CHECKS)
{
this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = false;
this.dataGridView1.EndEdit();
}
}
}
Basically, what this code does is it adds an Integer column and a Bool column to the DataGridView. Then, on the CellContentClick event, if the checkbox column was clicked, first we commit the edit (if you don't do this, you'll have all kinds of trouble figuring out if the checkbox is checked or not). Then, we loop through the rows and count all the checked rows. Then, if the amount is greater than what we want to allow, we just set it back to false, and again re commit the edit. Test it out, it works. May not be the most elegant solution, but the DGV can be tricky with the checkboxes, so this is how I would do it.
EDIT: Just a small edit, I hooked into the ContentDoubleClick event as well, as I noticed you were able to beat the validation if you quickly clicked on the cell. Should work better now.

Categories

Resources