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
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.
Basically I have a menustrip with 3 items. And another combobox with those exact same three items.
How do I set it so upon clicking an item on one list it sets the other list to the same value.
I hope I explained this clearly. Thanks.
If you want to set the ComboBox SelectedItem based on the MenuItem selection You can follow the below steps:
Step 1: You need to cast the sender object into ToolStripMenuItem in your ToolStripMenuItemClick Event handler.
Step 2: then assign the above casted one into ComboBox.FindString() method as an argument so that it returns the Matching Item index in the Combobox.
Step 3: now assign the returned Index value by the FindString() method to the ComboBox1.SelectedIndex property so that the exact item selected in MenuStrip willbe Selected in Combobox aswell.
Try This:
item1ToolStripMenuItem.Click += new System.EventHandler(ToolStripMenuItem_Click);
item2ToolStripMenuItem.Click += new System.EventHandler(ToolStripMenuItem_Click);
item3ToolStripMenuItem.Click += new System.EventHandler(ToolStripMenuItem_Click);
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
comboBox1.SelectedIndex = comboBox1.FindString(((ToolStripMenuItem)sender).Text);
}
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.
I have a ListView in Virtual Mode. I wanna to access SelectedItems property.
But when I use ListView1.SelectedItems , I receive the following Exception :
Cannot access the selected items collection when the ListView is in virtual mode
How can I access to ListView1.SelectedItems in VirtualMode.
It is quite old post but maybe someone else will benefit.
Simply use ListView.SelectedIndexCollection col = listView.SelectedIndices;
Then you can access an item:
forearch(var item in col)
{
string txt = listView.Items[item].Text;
}
..but you won't be able to iterate through ListView.Items using foreach because there is no iterator available in this mode. Using indexer is just flying fine :-)
When trying to use foreach you get an exception:
When the ListView is in virtual mode, you cannot enumerate through the
ListView items collection using an enumerator or call GetEnumerator.
Use the ListView items indexer instead and access an item by index
value.
From the docs
In virtual mode, the Items collection is disabled. Attempting to access it results in an InvalidOperationException. The same is true of the CheckedItems collection and the SelectedItems collection. If you want to retrieve the selected or checked items, use the SelectedIndices and CheckedIndices collections instead.
I you store all items in list and use this list to give item in RetrieveVirtualItem
you can find selected items like following
Dim lstData As List(Of ListViewItem) = New List(Of ListViewItem)
Dim lstSelectedItems As List(Of ListViewItem) = lstData.FindAll(Function(lstItem As ListViewItem) lstItem.Selected = True)
Me.Text = lstItems.Count.ToString()
I've done it by the following code, but it has an exception when more than one item are selected:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
List<ListViewItem> ListViewItems = new List<ListViewItem>();
foreach (int index in listView1.SelectedIndices)
{
ListViewItem SelectedListViewItem = listView1.Items[index]; // exception
ListViewItems.RemoveAt(index);
}
…
void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
e.Item = ListViewItems[e.ItemIndex];
}
Whenever you remove item(s) from a collection, always iterate from the largest index to the smallest index, like this:
for (int index = listView1.SelectedIndices.Count - 1; i >= 0; i--)
{
…
}
This is because every time you remove an item in a collection, the index will change if you do not iterate from the largest to the smallest index.
I have a CheckBoxList with a SelectedIndexChanged event, where I add the Value of the selected ListItem to a variable. I want to substract the Value when the item is unchecked.
I tried SelectedIndex but returns -1 and SelectedItem returns null. And the EventArgs argument doesn't have any data to help...
You should use the Items property. In other words, your variable should start with its value set to whatever it should be before applying the selected items. Then, iterate over the items, applying your logic against the variable one item at a time for each item that is selected. Then, no matter what the user selects or de-selects, you'll always arrive at the proper value for your variable.
int myValue = 0;
foreach(ListItem item in cbl.Items)
{
if(item.Selected) myValue += int.Parse(item.Value);
}