C# How to prevent ListBox to select first item when you unselect the last one

C# How to prevent ListBox in MultiSimple selection mode to select first item automatically, when you unselect the last one selected item in the box - it happens only if listbox represented by my own class objects, and everything is ok when it represented by string objects. Thnx!

Upvotes: 1

Views: 441

Answers (2)

Thank you very much, but i already use some like this. I don't understand why first item of listBox selected everytime i assign preloaded list as DataSource. I resolve this problem, by making another one temporal List of my object class, which items readed from binary file, and then push them one by one to my original List in foreach cycle by listOfObjects.Add(object) method. I know that every time after code ListOfTags.DataSource = null; ListOfTags.DataSource = tags; ListOfTags.DisplayMember = "Name"; if my tags (it is a List) are preloaded even in code (for example if i write code List<Tag> tags = new List<Tag> {new Tag("1"),new Tag("2"), new Tag("3")}; , this takes situation when first item of listbox selected, and starts selects everytime after that when i try do deselect last selected item in listBox.

Upvotes: 0

jdwee
jdwee

Reputation: 613

It seems like keeping track of the order of the list is the most important part. I would suggest maybe making an array of a struct. You can make this struct contain whatever you want for example:

struct itemList
{
    public string itemName;
    public int itemIndex;
    //include whatever other variables that you need included in here.

}

Also, make sure you place your struct before the namespace. Then, making the array of the struct would look like this:

itemList[] items1 = new itemList[listBoxName.SelectedItems.Count];

All you would have to do then is to add the items to the array before you reorder the listBox

for (int i = 0; i < listBoxName.SelectedItems.Count; i++)
   {
        items1[i].itemName = listBoxName.SelectedItems[i].ToString();
        items1[i].itemIndex = listBoxName.SelectedIndices[i];
   }

Upvotes: 1

Related Questions