I'm seraching for a way to delete a specific item in a ComboBox by index.
I only found a way to delete the item by value with this code:
cbRooms.Items.Remove((ComboBoxItem)item))
Update
I want to use a code like that
ComboBox.Items.Remove(ComboBox.SelectedIndex);
You can remove item by index, using RemoveAt method:
comboBox.Items.RemoveAt(index);
It is as simple as :
cbRooms.Items.RemoveAt(0); //0 = Index
If you want the get the index of the selected value and remove it, you can do something like :
int SelectedIndex = cbRooms.SelectedIndex;
// OR LIKE : int SelectedIndex = cbRooms.FindString(textBox2.Text);
cbRooms.Items.RemoveAt(SelectedIndex); //0 = Index
Hope that what you are looking for and my answer helped you.
Related
For example if I click on the first item it will be at index 0.
If I click on item 15 then the index should be 16.
I tried
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
int index = listView1
}
But I'm not sure if this is the right event or I should use the listView1_Click event ?
And the listView1 does not have any property SelectedIndex.
And last thing is I want to get the item text according to the index of the item I clicked on.
Assuming you want the index of the currently selected item you can do it like this :
int index = ListView1.FocusedItem.Index
Use ListView.SelectedIndices property:
List<int> selectedIndices = listView1.SelectedIndices.Cast<int>().ToList();
It returns collection of selected indices (because by default you can select several items in listview if you click on items with Ctrl or Shift key pressed). Also note that when you deselect all items, this collection will be empty and things like listView1.SelectedIndices[0] will throw IndexOutOfRange exception.
But if you will set MultiSelect property to false. Then this collection will always contain zero or one item. You can use Count property of SelectedIndicesCollection to check if item was selected:
if (listView1.SelectedIndices.Count > 0)
{
int selectedIndex = listView1.SelectedIndices[0];
}
You need to use the selected indices list you can also do this be item too.
listView1.SelectedIndices[0]
First you can get listview item object like below
ListViewItem lst=(ListViewItem)listView.SelectedItems[0];
from that object(lst) you can get the text like below
string text=lst.Content.ToString();
According to MSDN, there is still SelectedIndex. In my opinion your event is wrong, but you can still see it by .SelectedIndex. As it was mentioned before.
UPDATE: according to the comment, the link is fixed for the correct case.
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.
I have a problem filling my ComboxBox. Here´s my code:
string[] test = { "Ausgaben", "Eingaben" };
foreach (string r in test)
{
cbEinAus.Items.Add(r);
}
The values from the string array are in the ComboBox, but the selected item is a empty string.
I want one of this two string from the array to be my selected item.
I already tried with the SelectedItem property, but that doesn´t work.
Maybe its a simple solution... Can anybody help me please?
Use:
cbEinAus.SelectedIndex = 0;
You can replace the 0 with the zero based index of whichever item you want to select.
Try
cbEinAus.Items[vbEinAus.SelectedIndex]
instead of SelectedItem. usually works for me.
Try setting the SelectedItem as cbEinAus.Items[0]
You can set the Text property.
cbEinAus.Text = test[0];
More examples can be found at How do I set the selected item in a comboBox to match my string?
cbEinAus.SelectedIndex = 0;
replace item to index
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
C#: How do you tell which item index is selected in ListView?
ListView mylistv = new ListView();
var index = mylistv.SelectedIndices();
That should do it.
Have you tried SelectedIndices?
myListView.SelectedIndices
Use the SelectedIndex property of the class.