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);
}
}
}
}
Related
I am adding items to the listbox using backgroundworker. It is showing all the items present in the list one by one after some interval of time. I want to show only current item in the list and not all the items.
This is what I have tried.
private void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
{
List<string> result = new List<string>();
var found = obj.getFiles();//List<strings>
if (found.Count != 0)
{
for (int i = 0; i < found.Count; i++)
{
int progress = (int)(((float)(i + 1) / found.Count) * 100);
if (found[i].Contains("SFTP"))
{
result.Add(found[i]);
(sender as BackgroundWorker).ReportProgress(progress, found[i]);
}
System.Threading.Thread.Sleep(500);
}
e.Result = result;
}
}
private void backgroundWorker3_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (e.UserState != null)
listBox3.Items.Add(e.UserState);
}
I want to show only current item in the list and not all the items.
Although this is a bit of a hack, I think it should work for what you describe:
// just call clear first
listBox3.Items.Clear();
listBox3.Items.Add(e.UserState);
Truth be told, are you sure you want a ListBox in this circumstance? After all, it's not really a list, it's only an item.
I am really confused on thinking which control to use for my purpose.
I am having list of items say item1 to item10. User can select 4 or 5 items in any order.
Now user selected items has to be separated in same order.
For example, if the user selected the items in following order, item4, item8, item3 and item2.
I want it in the same order. item4,item8,item3,item2.
How do I achieve this in winforms control?
It is not a very nice solution but I wrote it as you asked.
Set the SelectionMode of your ListBox to MultiExtended or MultiSimple as you need.
Then write this code in SelectedIndexChanged event of your ListBox:
List<string> orderedSelection = new List<string>();
bool flag = true;
private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
{
if (flag)
{
flag = false;
var list1 = listBox3.SelectedItems.Cast<string>().ToList();
if (listBox3.SelectedItems.Count > orderedSelection.Count)
{
orderedSelection.Add(list1.Except(orderedSelection).First());
}
else if (listBox3.SelectedItems.Count < orderedSelection.Count)
{
orderedSelection.Remove(orderedSelection.Except(list1).First());
}
var list2 = listBox3.Items.Cast<string>().Except(list1).ToList();
listBox3.Items.Clear();
for (int i = 0; i < list1.Count; i++)
{
listBox3.Items.Add(list1[i]);
listBox3.SelectedIndex = i;
}
foreach (string s in list2)
{
listBox3.Items.Add(s);
}
flag = true;
}
}
When user selects an item, It comes to first of the list and the rest of the items comes next.
Also, there is an alternative way. You can use a CheckedListBox with two extra button for moving selected items up and down. So user can change the order of selected items.
This solution uses CheckListBox's ItemCheck event along with a private List to keep track of the click items order.
protected List<string> clickOrderList = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
// Populate the checked ListBox
this.checkedListBox1.Items.Add("Row1");
this.checkedListBox1.Items.Add("Row2");
this.checkedListBox1.Items.Add("Row3");
this.checkedListBox1.Items.Add("Row4");
}
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (sender != null && e != null)
{
// Get the checkListBox selected time and it's CheckState
CheckedListBox checkListBox = (CheckedListBox)sender;
string selectedItem = checkListBox.SelectedItem.ToString();
// If curent value was checked, then remove from list
if (e.CurrentValue == CheckState.Checked &&
clickOrderList.Contains(selectedItem))
{
clickOrderList.Remove(selectedItem);
}
// else if new value is checked, then add to list
else if (e.NewValue == CheckState.Checked &&
!clickOrderList.Contains(selectedItem))
{
clickOrderList.Insert(0, selectedItem);
}
}
}
private void ShowClickOrderButton_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (string s in clickOrderList)
{
sb.AppendLine(s);
}
MessageBox.Show(sb.ToString());
}
so far i have accomplished transfer a single select item from one lb1 to lb2 and vice versa. Now the problem is transfering the whole list of data from lb1 to lb2 and vice versa. if anyone could help please.
using for loop will be much better.
i am using the following code:
private void add_Click(object sender, EventArgs e)
{
if (lb1.SelectedItem != null)
{
lb2.Items.Add(lb1.SelectedItem);
lb1.Items.Remove(lb1.SelectedItem);
}
else
{
MessageBox.Show("No item selected");
}
}
private void remove_Click(object sender, EventArgs e)
{
if (lb2.SelectedItem != null)
{
lb1.Items.Add(lb2.SelectedItem);
lb2.Items.Remove(lb2.SelectedItem);
}
else
{
MessageBox.Show("No item selected");
}
}
private void addall_Click(object sender, EventArgs e) //problem is here. adding all the items from lb1 to lb2
{
for (int i = 0; i < lb1.SelectedItems.Count; i++)
{
lB2.Items.Add(lb1.SelectedItems[i].ToString());
}
}
private void removeall_Click(object sender, EventArgs e) //problem is here. adding all the items from lb2 to lb1
{
}
Because you want to transfer all of the items from one list box to another not only the selected items you must loop for every item in the list box and not only the selected items.
So change your usage of ListBox.SelectedItems to ListBox.Items and your code should work as expected assuming you remember to remove the items as well.
for (int i = 0; i < lb1.Items.Count; i++)
{
lB2.Items.Add(lb1.Items[i].ToString());
}
lb1.Items.Clear();
You could do something like this:
private void addall_Click(object sender, EventArgs e)
{
foreach (var item in lb1.Items)
{
lB2.Items.Add(item);
}
}
This would loop through your list and add them all. You would do the reverse for lb2->lb1.
Simply iterate over all Items of the list and add each element to the next list. At the end simply remove all Items.
foreach (var item in listBox1.Items)
{
listBox2.Items.Add(item);
}
listBox1.Items.Clear();
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.
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);
}