How to reload item of combobox in c#? - c#

I have 10 items in combobox and when I delete one of those items, the combobox don't show automatically next item it show white space. I want to display next item in range.
I delete them normaly
cmbsomename.Items.Remove(cmbsomename.SelectedItem);
How to do that?

Because you deleted the selected item, you need to set another item as selected:
comboBox.SelectedIndex = 0;
Edit:
The code above sets the selected item as the first item in the list.
For the next item, you need to find the index of the item you are deleting first:
int index = cmbsomename.SelectedIndex;
cmbsomename.Items.Remove(cmbsomename.SelectedItem);
if (index < cmbsomename.Items.Count) // Make sure there IS a next item
cmbsomename.SelectedIndex = index; // Next item will have same index value

var selectedIndex = cmbsomename.SelectedIndex;
cmbsomename.Items.Remove(cmbsomename.SelectedItem);
comboBox.SelectedIndex = selectedIndex < cmbsomename.Items.Count() ? ++selectedIndex : 0;

after deleting an Item set the selectedindex
int index = comboBox.SelectedIndex;
comboBox.Items.Remove(comboBox.SelectedItem);
if (index < comboBox.Items.Count)
comboBox.SelectedIndex = index;

Related

How to copy the other Unselected items from a listBox to another

How to copy selected items from ListBox1 to listBox2, and unselected Items to listBox3.
I already know how to copy selected items as below:
for (int i = 0; i < listBox1.SelectedItems.Count; i++)
{ listBox2.Items.Add(listBox1.SelectedItems[i]);}
But I don't know how to copy the other unselected items from the main ListBox1.
Thanks
You can use the GetSelected(x) method of Listbox to determine if it is selected to not, and then put it into the appropriate ListBox.
for (int i = 0; i < listBox1.Items.Count; i++)
{
if (listBox1.GetSelected(i))
listBox2.Items.Add(listBox1.Items[i]);
else
listBox3.Items.Add(listBox1.Items[i]);
}

How do I get Index from listboxitem by its content?

Is there a way to get a Listbox Item Index by its content? Something like:
id = listbox.Items.Contains("text");
I know that this way I'll get a bool result, but I don't know how to get the item Index. If I could get the Index I could be able to remove items with
listbox.Items.RemoveAt(id);
In WPF
ListBox listBox = new ListBox();
int index = listBox.Items.IndexOf(item);
Look for the item's index, and then you can remove it with the given method:
int index = myListBox.Items.IndexOf(itemToSearch);
/*if there is no coincidence, it returns -1, so you must check for that return, else RemoveAt(-1) would give you a runtime error.*/
if (index >=0)
{
myListBox.Items.RemoveAt(index);
}
Assuming the item is not selected but for some reason you just want to find it in the list:
The listbox.items is a listbox object collection:
So:
id=listbox.items.IndexOf("text");
will do the trick.

How to sync multiple selection indices between two ListBoxes?

I'm trying to move the selection from listbox1 to listbox2 with this code
if (listBox1.SelectedItems.Count > 0)
{
int selectedindex = listBox1.SelectedIndex;
for (int i =0 ; i < listBox1 .SelectedItems.Count; i++)
{
listBox2.SetSelected(selectedindex , true);
}
}
I got a result , but when I select more than one item in listbox1 it will only select one item in listbox2 which is the first one I've selected in listbox1.
You are only storing one selection index of listbox1 in selectedIndex. Later in the loop you are always instructing listbox2 to set its selections to this one index.
Try this instead:
foreach (int index in listBox1.SelectedIndices)
{
listBox2.SetSelected(index, true);
}
You have to set the SelectionMode property to allow multiple selections. You can set in property window or on load event.
listBox2.SelectionMode.MultiExtended = SelectionMode.MultiExtended;

Combobox Selected Index is changing to 0 when data is appended to it

Hi I am using the following code in the form load
Combobox1.DataSource=GetItems();
Then by default first item is selected.
I suppose your ComboBox has the DropDownStyle property set to DropDownList. When it is, setting the Datasource automatically sets the SelectedIndex to 0 (first element in the list). You could write:
Combobox1.DataSource=GetItems();
Combobox1.SelectedIndex = -1;
You're not appending data, you're replacing it entirely. So the SelectedIndex will be reset. You could remember it and then set it back like so
int oldIndex = Combobox1.SelectedIndex;
Combobox1.DataSource= GetItems();
Combobox1.SelectedIndex = oldIndex; //should check to see if the new list is long enough.

Selecting default item from Combobox C#

I have few items on my ComboBox items collection, and i'd like to select one item from this list and set it as default item - when app starts - this item is already on comboBox.
I'm trying something like that:
SelectPrint11.SelectedIndex=2;
but error is:
System.ArgumentOutOfRangeException: InvalidArgument=Value of '2' is not valid for 'SelectedIndex'
Edit:
In mylist are 3 items, Printer1, Printer2, Printer3. All are added in ComboBox Properties -> Items -> Collection
You can set using SelectedIndex
comboBox1.SelectedIndex= 1;
OR
SelectedItem
comboBox1.SelectedItem = "your value"; //
The latter won't throw an exception if the value is not available in the combobox
EDIT
If the value to be selected is not specific then you would be better off with this
comboBox1.SelectedIndex = comboBox1.Items.Count - 1;
Remember that collections in C# are zero-based (in other words, the first item in a collection is at position zero). If you have two items in your list, and you want to select the last item, use SelectedIndex = 1.
private void comboBox_Loaded(object sender, RoutedEventArgs e)
{
Combobox.selectedIndex= your index;
}
OR if you want to display some value after comparing into combobox
foreach (var item in comboBox.Items)
{
if (item.ToString().ToLower().Equals("your item in lower"))
{
comboBox.SelectedValue = item;
}
}
I hope it will help, it works for me.
this is the correct form:
comboBox1.Text = comboBox1.Items[0].ToString();
U r welcome
This means that your selectedindex is out of the range of the array of items in the combobox. The array of items in your combo box is zero-based, so if you have 2 items, it's item 0 and item 1.
first, go to the form load where your comboBox is located,
then try this code
comboBox1.SelectedValue = 0; //shows the 1st item in your collection
ComboBox1.Text = ComboBox1.Items(0).ToString
This code is show you Combobox1 first item in Vb.net

Categories

Resources