Listbox multiple selection always selected all - c#

I have one problem with listbox.
I use a listbox and the count is added when selecting the listbox items. But I have two items in the listbox that are always selected and the count is always 2.
What do I do to select a single or mutiple selection?

Two items always selected because of below two reasons:
1) They are selected in the inline code - selected = "true"
2) They are getting selected in the PageLoad method of codebehind and you are calling without Not IspostBack.
Please check.

One fact is you've set Mutliple selection mode.
another fact, is when you insert an item it is selected, if add another both are selected.
So after you add an item set
ListBox1.SelectedIndex = -1;
Otherwise, check suggestions given by Saurabh

Set ListSelectionMode to Multiple and then iterate through the listbox items
foreach (ListItem item in ListBox1.Items)
{
if(item.Selected)
{
count++;
}
}

Related

how to identify if multiple indexes are selected from the listbox control in c# winforms?

I am developing an application and I have a requirement to place the fields
on front end just like take checkbox. If the user selected particular fields on the checkbox, then based on the selection I will generate crystal report from sql database.
So up to 10 fields checkbox is good enough. But the fields are increased up to 30 and the checkbox count is also increased on the form.
So I decided to take listbox. But in listbox how to identify if
multiple items are selected from the user?
In the listbox, I have set SelectionMode property to MultiSimple.
But if I select two or more items, listbox takes only the index of the first item.
Code :
if(listbox1.SelectedIndex==0)
{
//my code for first field.
}
if(listbox1.SelectedIndex==1)
{
//my code for second field.
}
Note : I wrote a method for getting a dynamic sql query based on the user
selected items. So in my method createSQLquery(), I want to identify the
indexes.
I want to identify which items the user has selected from the frontend and based up on that i will proceed to write my code.
Thanks
There are three ways in which you can find
1)
foreach (object item in listbox.SelectedItems)
{
// do domething
}
2)
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
// do domething
}
}
3)
var selected = ListBox1.GetSelectedIndices().ToList();
var selectedValues = (from c in selected
select ListBox1.Items[c].Value).ToList();
You can use the ListBox.SelectedIndices property to get the indexes of multiple selected items.
Gets a collection that contains the zero-based indexes of all
currently selected items in the ListBox.

Keep ListBox sorted, but with a particular entry always pinned to the top

I have a ListBox filled with Countries (which I take from the Active Directory). I want the list to be sorted, but also I want one entry "All" to be at the very top.
How can I do this?
If you are binding the data in code behind you can insert a Listitem at index 0.
ListItem myItem=new ListItem("ALL","value");
myListbox.Items.Insert(0, myItem);
I would sort the list items first before binding to you ListBox. There are several options for doing this depending on what your data source is i.e. DataTable, List, Dictionary etc. To insert an item use code below.
lstCountries.Items.Insert(0, new ListItem("All", "0"));
After you load the data (i.e Countries), add the ListItem as follows:
myListbox.Items.Add(new ListItem() { Text = "All", Value = "" });
myListbox.SelectedIndex = 0; //This line will selected the first item on your ListBox.
Here, you might consider which action take if the ListBox with text "All" is selected.

Problems with selected indices in listview

I have an arraylist which contain objects of my own class. I want to fetch the object from the array list which has the index = selectedindex of listview.
I tried this :
TrackInformation t=(TrackInformation) SongList[listView1.SelectedIndices[0]];
TrackInformation is my class and SongList is an ArrayList of type TrackInformation.
listview1 does not allow multiple indices selection so I want the first element of the SelectedIndices collection.
I am getting ArgumentOutOfRangeException and it says value of '0' is not valid for 'index'.
Put this line before your code -
if(listView1.SelectedIndices.Count > 0)
{
TrackInformation t=(TrackInformation) SongList[listView1.SelectedIndices[0]];
}
The ListView.SelectedIndexChanged event has a quirk that bombs your code. When you start your program, no item is selected. Click an item and SelectedIndexChanged fires, no problem. Now click another item and the event fires twice. First to let you know, unhelpfully, that the first item is unselected. Then again to tell you that the new item is selected. That first event is going to make you index an empty array, kaboom. RV1987's snippet prevents this.
The error is because listView1.SelectedIndices is empty, do you have a row selected?
you probable want to wrap in a test
ListView.SelectedIndexCollection selected=listView1.SelectedIndicies;
if (selected.Count==0) {
// code for no items selected
} else {
TrackInformation t=(TrackInformation) SongList[selected[0]];
// rest of code to deal with t
}

Removing item from BindingList does not refresh DataGridViewComboBoxCell

I have a DataGridView where there is a cell which is a DataGridViewComboCell. Each DataGridViewComboCell is bound to a unique copy of a BindingList. When I remove an item from the binding list the comboboxes remove the entry I had removed from the bindinglist.
However, if that value is selected it stays as the selected item in the cell.
I tried doing a datagridview.refresh(), but it still didn't help. It is getting called from a tool strip menu item
// _contractLists is List<BindingList<String>> which is the datasource for a datagridviewcombobox
List<String> removedList = new List<string>();
_contractSelForm.ShowDialog();
_contractSelForm.GetandClearRemovedContracts(ref removedList);
foreach (BindingList<String> contractList in _contractLists)
{
// remove deleted favorites
foreach (string contract_name in removedList)
{
contractList.Remove(contract_name);
}
}
dataGridView1.Refresh();
dataGridView1.EndEdit();
Couple of things to note/look at:
1) You shouldn't need to call EndEdit after Refresh. If it needs to be called, you should call it before Refresh.
2) If your comboboxes have a DropDownStyle of DropDown, then I this is expected behavior.
From the MSDN documentation:
If you set the DropDownStyle property to DropDown, you can type any value in the editable area of the ComboBox.
To change this, either change the DropDownStyle to DropDownList or manually clear the value in code after removing the items.

How to get the index of the recently selected item in a list box

I have a listbox in which i have some items. I select some items in the listbox. The condition is that i want the selection to be continuous. If I select any other item in listbox which is not continuous with the selection,that item should be deselected immediately. I need to have the index of recently selected item which i tried to get with the help of SelectionChangedEvent but it gives me the index of first selected item. How to do that?
You're looking for the last item in the SelectedIndices collection.
Keep the indicies of those continously selected items somewhere.
When any item is selected or unselected, catch this in SelectionChanged event. Check the SelectedIndicies collection, like SLaks suggested, to see if something except than your collection is selected, or if some of it's items were unselected. If you need, restore the presentation of listBox.

Categories

Resources