I have a listbox control on winform and same is Single Items SelectionMode OR One Items Selection Mode. I am trying to scroll it from form_KeyDown Event as below
if ((Keys)e.KeyCode == Keys.Down)
{
if (listBox2.Items.Count >= listBox2.SelectedIndex)
{
listBox2.SelectedIndex++;
}
}
But it’s throw an error like “ArgumentOutOfRangeException was unhandled”
Invalid argument of value =23 is not valid for selection index.
How to get ridoff?
Try this:
if ((Keys)e.KeyCode == Keys.Down)
{
if ((listBox2.Items.Count-1) > listBox2.SelectedIndex)
{
listBox2.SelectedIndex++;
}
}
Remember that if you have 23 items, SelectedIndex goes from 0 to 22...
ListBox.SelectedIndex is a zero based array IE the first item will be 0 in the index whereas the Items.Count will always return a value starting at 1.
Please see the following for further information:
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindex.aspx
Kind Regards, Wayne
According to MSDN's documentation on ListBox.SelectedIndex:
A zero-based index of the currently selected item. A value of negative
one (-1) is returned if no item is selected.
So, I believe you need to change
if (listBox2.Items.Count >= listBox2.SelectedIndex)
to
if (listBox2.Items.Count-1 > listBox2.SelectedIndex)
Please vote Marco's answer as correct, as he pointed this out to me!
Because if there are 23 items in the listbox, item 23 is actually item 22, item 1 is actually item 0, etc. etc.
Related
How do I restrict the amount of rows/indexes in a list box from a dynamic amount to a constant amount, like 5, programmatically? For example, a user inputs data from a text box to a list box up to the fifth row. If they attempt again, the program would reject the data the user entered which would prevent the list box from increasing the dynamic row size.
I tried using Selected Index and Selected Items properties such as:
if (ListBox1.SelectedItems.Count != 0)
{
while (ListBox1.SelectedIndex == 5)
{
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
}
}
but it seems that the properties require a list box index to be selected.
if(ListBox1.Items.Count < 5){
ListBox1.Items.Add("asd");
}
Instead of removing, while inserting, you can check if the number of items in ListBox1 are less than 5 and only add an item if it holds true.
I have several items in my ComboBox. But how to tell ComboBox to show only item number 1 from list as default? Currently at design time I have empty text.
You have to set SelectedIndex:
comboBox.SelectedIndex = 1;
You can use the SelectedIndex as show value:
comboBox1.SelectedIndex = 0;
NB: you should check the count of values in your Items array.
MS documentation: https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindex(v=vs.110).aspx
When I set comboBoxEdit.selectedindex = some value, it never take this value. its value is always -1. I have set it in Constructor or in Form_Load.
if (oPersclientEntrp.TypPrint == 1) {
comboBoxEdit_Print.SelectedIndex = 0;
} else {
comboBoxEdit_Print.SelectedIndex = 2;
}
I have heard that The SelectedValue, SelectedIndex, SelectedItem properties can't be set until the control is added to the form. After the control is added to the form, the selectedValue, -Index and -Item properties can be set.
but I bind the value on design mode .
Try updating your code to be this:
if (oPersclientEntrp.TypPrint == 1) { comboBoxEdit_Print.SelectedIndex = 0; }
else { comboBoxEdit_Print.SelectedIndex = 1; }
If you only have 2 items, your SelectIndex should be 1, not 2.
You have 2 Items and the index of SelectedIndex starts with 0 (Because it access' an internal array, which of course starts with 0). So you have to edit your code to use index 0 instead of 1 and index 1 inseatd of 2.
Btw, this is a common behavior of the most SelectedIndex properties, i.e. of TabControl.
I have an if statement saying that if a webpage has a certain text to remove the selected item on a listBox and iterate down to the next one.
I made some code but when I try it I keep getting:
ArgumentOutOfRangeException was unhandled by usercode
This is the error in more detail:
{"InvalidArgument=Value of '1' is not valid for
'SelectedIndex'.\r\nParameter name: SelectedIndex"}
This is my code:
listBox1.Items.Remove(listBox1.SelectedItem);
listBox1.SelectedIndex = + 1;
EDIT:
Thanks for all the help guys! I removed the issue by not removing the items and just making it iterate down.
You have to test if the item which you are trying to select acutally exists.
int index = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(index);
If (index < listBox1.Items.Count) {
listBox1.SelectedIndex = index;
}
EDIT: If you want to delete items in a loop, it is a good idea to start at the end, since removing an item changes the position of the following items. Looping upwards would make you skip an item each time you remove an item.
for (int i = listBox1.Items.Count - 1; i >= 0; i--) {
if (listBox1.Items[i].ToString() == "whatever") {
listBox1.Items.RemoveAt(i);
}
}
I'd have to see the full code sample (with the if statement) to know for sure. But it is pretty obvious you are setting the selected item to an index that isn't in the list box.
Be careful, the selectedIndex is zero based, not one based.
MSDN says of your error:
ArgumentOutOfRangeException: The assigned value is less than -1 or
greater than or equal to the item count.
Since the error indicates it is happening when you set the SelectedIndex to 1, I am assuming that you only have a single item in the listbox (index=0) when this code is called.
Since indices are 0-based, setting SelectedIndex to 1 is selecting the second value in the list. I'm guessing it's failing when you have removed all but one of the values and are trying to set SelectedIndex to the second one.
Do you want to select the first item in the listBox? If so the code would be:
if(listBox1.Items.Count > 0) listBox1.SelectedIndex = 0;
I'm hoping someone can help with an exception I've inherited. Basically I'm writing the rows in a datagrid to a text file. Which works fine apart from when the row is hidden, when the row is hidden the exception "Index was outside the bounds of the array" is thrown at the line highlighted below. Thanks for any help.
DataRow dr;
for (int i = 0; i < bindingManagerBase.Count; i++)
{bindingManagerBase.Position = i;
dr = ((DataRowView)bindingManagerBase.Current).Row;
bindingManagerBase.SuspendBinding();
try
{
int rowIndex = dr.Table.Rows.IndexOf(dr);
if (!rowsDisplayStatus[rowIndex]) //<---------Exception here "Index was outside the bounds of the array" //Picture below
{
m_Dgv.Rows[rowIndex].Visible = false;
continue;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
bindingManagerBase.ResumeBinding();
}
writeData(tw, dr);
iIndex++;
}
You're getting the row index and then trying to use it with rowsDisplayStatus. You can't use the database row index as an index into your collections.
I would change:
if (!rowsDisplayStatus[rowIndex])
to:
if (!rowsDisplayStatus[i])
How is rowsDisplayStatus populated? If it only contains one element and something is expected to be at index 9, you should take a look at the code that populates it.
As the picture shows, rowsDisplayStatus has 1 item in it... you're trying to pull the 10th item (or the item at index 9)... that index is out of range.
How is "rowsDisplayStatus" populated? Maybe there is a problem in that routine.
I'll explain what mark said, above. (Don't know how to comment, so putting this in an answer)
He's correct you should change the
if (!rowsDisplayStatus[rowIndex])
Into
if (!rowsDisplayStatus[i])
The reason for this is as follows:
The rowIndex grows even when rows where previously deleted from the Rows object. So there could be only one or two rows in the dr.Table.Rows, but they could have indexes (indice) for example of 8 and 9 (because previously rows 1 to 7 where erased or for other reasons).
So you get the current rowIndex by checking the bindingManager.Current.RowIndex property.
But your rowsDisplayStatus is a simple array (or ArrayList) with the correct amount of rows according to i. So for row index:8 (the first row) you should look at rowsDisplayStatus[0] (which is the value of i), and for row index:9 (the second row) you should look at rowsDisplayStatus[1]... etc.
HTH, Moshe
Basically, whenever I've received this error the index (or value that I have set) was non-existant at the time it was being referenced.
For example if I have two Items in a ListBox and I tryi to reference a third Item, I will receive an Index Out Of Range exception (many times in the past)...