how to check only one item in checkedlistbox - c#

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

Related

Checked list box Checked items Verification

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

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

Move selected items from one listbox to another in C# winform

I'm trying to move selected items in list box1 to list box2, and vice versa. I have two buttons, >> and <<. When I select items in listbox1 and then click on >> the items should move from listbox1 to listbox2.
private void MoveListBoxItems(ListBox source, ListBox destination)
{
ListBox.SelectedObjectCollection sourceItems = source.SelectedItems;
foreach (var item in sourceItems)
{
destination.Items.Add(item);
}
while (source.SelectedItems.Count > 0)
{
source.Items.Remove(source.SelectedItems[0]);
}
}
private void button2_Click_1(object sender, EventArgs e)
{
MoveListBoxItems(listbox , lstActivity);
}
your code works fine. i tested it.
your question is "I try to move selected item in list box1 to list box2."
i think your button2 has problem.delete button2 and the code below
private void button2_Click_1(object sender, EventArgs e)
{
MoveListBoxItems(listbox , lstActivity);
}
then create other button and create click event.
full source:
private void MoveListBoxItems(ListBox source, ListBox destination)
{
ListBox.SelectedObjectCollection sourceItems = source.SelectedItems;
foreach (var item in sourceItems)
{
destination.Items.Add(item);
}
while (source.SelectedItems.Count > 0)
{
source.Items.Remove(source.SelectedItems[0]);
}
}
private void first2second_Click(object sender, EventArgs e)
{
MoveListBoxItems(FirstListbox, LastListbox);
}
private void second2first_Click(object sender, EventArgs e)
{
MoveListBoxItems(LastListbox, FirstListbox);
}
this code is work. if you want select more than one item change property
SelectionMode = MultiSimple;
private void buttonMoveToListBox1_Click(object sender, EventArgs e)
{
if(listBox1.SelectedIndex != -1)
{
listBox2.Items.Add(listBox1.SelectedValue);
listBox1.Items.Remove(listBox1.SelectedValue);
}
}
private void buttonMoveToListBox2_Click(object sender, EventArgs e)
{
if(listBox2.SelectedIndex != -1)
{
listBox1.Items.Add(listBox2.SelectedValue);
listBox2.Items.Remove(listBox2.SelectedValue);
}
}
There will be conflicts for every deleted row, so go with the below code:
>>
for (int intCount = ListBox1.SelectedItems.Count - 1; intCount >= 0; intCount--)
{
ListBox2.Items.Add(ListBox1.SelectedItems[intCount]);
ListBox1.Items.Remove(ListBox1.SelectedItems[intCount]);
}
<<
for (int intCount = ListBox2.SelectedItems.Count - 1; intCount >= 0; intCount--)
{
ListBox1.Items.Add(ListBox2.SelectedItems[intCount]);
ListBox2.Items.Remove(ListBox2.SelectedItems[intCount]);
}
If the above one doesn't work then try this:
while (ListBox1.SelectedItems.Count > 0)
{
ListBox2.Items.Add(ListBox1.SelectedItems[0].Text);
ListBox1.SelectedItems[0].Remove();
}
For more type of answers you can go with this link
Here Two ListBoxes. When i select the name from Left listbox and click ">>" Button the data move to Right ListBox
Code..
currentItemText = LeftListBox.SelectedValue.ToString();
currentItemIndex = LeftListBox.SelectedIndex;
RightListBox.Items.Add(currentItemText);
if (myDataList != null)
{
myDataList.RemoveAt(currentItemIndex);
}
ApplyDataBinding();
I solved using this code:
private void MoveListBoxItems(ListBox poSource, ListBox poDestination)
{
while (poSource.SelectedItems.Count > 0)
{
poDestination.Items.Add(poSource.SelectedItems[0]);
poSource.Items.Remove(poSource.SelectedItems[0]);
}
}
<script type="text/javascript">
$(document).ready(function() {
// > arrow
$('#SingleRightMove').click(function() {
$('#fromBox option:selected').remove().appendTo('#toBox');
//$("#tobox option").attr("selected", false);
$('#toBox').find("option").attr("selected", false);
});
// < arrow
$('#SingleLeftMove').click(function() {
$('#toBox option:selected').remove().appendTo('#fromBox');
$("#fromBox option").attr("selected", false);
});
// >> arrow
$('#AllRightMove').click(function() {
$('#fromBox option').remove().appendTo('#toBox');
$("#toBox option").attr("selected", false);
});
// << arrow
$('#AllLeftMove').click(function() {
$('#toBox option').remove().appendTo('#fromBox');
$("#fromBox option").attr("selected", false);
});
});
</script>
Get the >> and << buttons created with 2 list boxes like listbox2 and 3 below...get ur items in list box 2 first by something like get-content or get-adcomputer etc..
$buttonMOVERight_Click={
foreach ($Srv in $listbox2.selectedItems)
{$selectedServers=$Srv}
$listbox3.BeginUpdate()
foreach($TSrv in $Srv)
{$listbox3.Items.Add($TSrv);
$listbox2.Items.Remove($TSrv);}
$listbox3.EndUpdate()
}
$buttonMoveLeft_Click={
#TODO: Place custom script here
foreach ($Srvs in $listbox3.selectedItems)
{$ServersinRt=$Srvs}
$listbox2.BeginUpdate()
foreach($rSRv in $Srvs)
{$listbox2.Items.Add($rSRv);
$listbox3.Items.Remove($Srvs);}
$listbox2.EndUpdate()
}

SelectedIndices Changed Listbox

Is there some event I can use to tell when the SelectedIndices property changes for a listbox? I want to deselect items in a listbox based on a certain property value of the item. I've hooked up an event that works for when a SelectedIndex is changed, but not sure how to do it for when the SelectedIndices property changes for multiselection.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Curve curItem = (Curve)listBox1.SelectedItem;
int index = listBox1.Items.IndexOf(curItem);
if (curItem.newName == null)
{
listBox1.SetSelected(index, false);
}
}
You could use ListBox.SelectedItems and LINQ to find all Curves with newName==null to deselect them:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var nullNameCurves = listBox1.SelectedItems
.Cast<Curve>()
.Where(c => c.newName == null)
.ToList();
listBox1.SelectedIndexChanged -= listBox1_SelectedIndexChanged;
foreach (Curve curve in nullNameCurves)
listBox1.SetSelected(listBox1.Items.IndexOf(curve), false);
listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;
}
According to MSDN, this event will be fired every time the selection changes:
If the SelectionMode property is set to SelectionMode.MultiSimple or SelectionMode.MultiExtended, any change to the SelectedIndices collection, including removing an item from the selection, will raise this event.
So basically, you can use it the same way as using it with single selection.
Sample:
For example if you want to deselect all items with null as newName:
foreach (var item in listBox1.SelectedItems)
{
if ((item as Curve).newName == null)
{
int index = listBox1.SelectedItems.IndexOf(item);
listBox1.SetSelected(index, false);
}
}
(I'm not sure if you can deselect items inside a foreach loop since it changes the SelectedItems object itself. If it does not work, you can still make a temporary list of those items and deselect them after the loop.)
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Curve curItem = null;
for (int i = 0; i < listBox1.SelectedItems.Count; i++)
{
curItem = (Curve)listBox1.SelectedItems[i];
if (curItem != null)
{
int index = listBox1.Items.IndexOf(curItem);
if (curItem.newName == null)
{
listBox1.SetSelected(index, false);
}
}
}
}

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.

Categories

Resources