I have two lists called standards and credits. The standards list is added to a ComboBox. When I click an item in the ComboBox I want to be able to show an item in the credits list. For example I click first index in the ComboBox, I want to show item in the first item of the credits list. I have this code but it gives me an error I can't fix it. This is the error im getting:
System.ArgumentOutOfRangeException
It's from this line of code
lblCredits.Text = credits.ElementAt(standard.IndexOf(cboStandard1.Text))
-
private void cboStandard1_SelectedIndexChanged(object sender, EventArgs e)
{
if (cboStandard1.SelectedIndex + 1 > 0)
{
lblCredits.Text = credits.ElementAt(standard.IndexOf(cboStandard1.Text));
}
}
This is showing that my lists are the same length
private void standardlist()
{
standard.Add("91632");
standard.Add("91633");
standard.Add("91634");
standard.Add("91635");
cboStandard1.DataSource = standard;
((ComboBox)cboStandard1).SelectedIndex = -1;
credits.Add("4");
credits.Add("6");
credits.Add("4");
credits.Add("4");
}
Like I said, binding the ComboBox, i.e. setting its DataSource property, will select the first item by default, which will raise the SelectedIndexChanged event, which will execute your code. That all happens before you've populated the credits list, which is why it contains no items. There are two things you can do, and you may choose to do both:
Populate the credits list before setting the DataSource of the ComboBox.
Handle the SelectionChangeCommitted event instead of SelectedIndexChanged.
The SelectionChangeCommitted event is raised only when the user selects an item via the UI, so it will not be raised when you bind the data and then reset the SelectedIndex, while SelectedIndexChanged will be raised twice. Even if you do implement option 1, you'll still have an issue on the second SelectedIndexChanged event because you'll be passing -1 to ElementAt, so you'll get an ArgumentOutOfRangeException thrown.
Related
Im having a hard time trying to figure this out, till i discovered this is the best approach to my problem, the thing is I have no idea how to do it.
Basically lets say I have a ComboBox, with 5 items inside (the number of items is not constant, just an example).
My goal is, after someone selects one of those 5 items, to discover which one it was by the number. I mean for example, i have 5 items in the ComboBox and I picked the third item (counting from the top), I want my program to know that the user picked the third item.
Any suggestions on how I should do it or has anyone done it and has the code?
the combobox has a property called SelectedIndex. It will start with 0 which stands for the first element.
i picked the third item counting from the top
This item will have the index of 2.
Take this index an add a 1 and your programm will know which element it has.
Unless you want really the index then leave the addition away.
Here is the documentation
There is a cool event called SelectionChanged which you can use to catch the selection:
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int i = comboBox.SelectedIndex;
}
Lets say that you have comboBox and you have label and you want to update label to show index of selected item every time that you click to change selected item.
Just remember that index starts with 0. This is just example of how syntax should look, SelectedIndex method returns INT value in range from 0 to the number of elements-1 based on what item is currently selected.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Text = comboBox1.SelectedIndex.ToString();
}
I have an arraylist which contain objects of my own class. I want to fetch the object from the array list which has the index = selectedindex of listview.
I tried this :
TrackInformation t=(TrackInformation) SongList[listView1.SelectedIndices[0]];
TrackInformation is my class and SongList is an ArrayList of type TrackInformation.
listview1 does not allow multiple indices selection so I want the first element of the SelectedIndices collection.
I am getting ArgumentOutOfRangeException and it says value of '0' is not valid for 'index'.
Put this line before your code -
if(listView1.SelectedIndices.Count > 0)
{
TrackInformation t=(TrackInformation) SongList[listView1.SelectedIndices[0]];
}
The ListView.SelectedIndexChanged event has a quirk that bombs your code. When you start your program, no item is selected. Click an item and SelectedIndexChanged fires, no problem. Now click another item and the event fires twice. First to let you know, unhelpfully, that the first item is unselected. Then again to tell you that the new item is selected. That first event is going to make you index an empty array, kaboom. RV1987's snippet prevents this.
The error is because listView1.SelectedIndices is empty, do you have a row selected?
you probable want to wrap in a test
ListView.SelectedIndexCollection selected=listView1.SelectedIndicies;
if (selected.Count==0) {
// code for no items selected
} else {
TrackInformation t=(TrackInformation) SongList[selected[0]];
// rest of code to deal with t
}
So I added a list view, and I am displaying 3 columns of strings in each. I also have the full row select on. I want to be able to double click on one of the rows, and have it return the string in the 3rd column. I've tried to look everywhere for a solution to this, but so far nothing comes up.
My code so far is:
private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
{
MessageBox.Show(songList.SelectedItems[2].ToString());
}
Yet it returns an error saying "InvalidArgument=Value of '2' is not valid for 'index'.
Parameter name: index"
You could try:
if (songList.SelectedItems.Count > 0)
{
ListViewItem item = songList.SelectedItems[0];
string s_you_want = item.SubItems[1].Text;
}
Taken a ListViewItem, you can take columns values using SubItems[] property.
I think that you hould bound not your listView to doubleClick event, but listViewItem. sender will have DataContext, from which you can get your third column.
How do you fill the ListView? Wihtout knowing this, it's difficult to help. May be you could try this:
Set a breakpoint in this line:
MessageBox.Show(songList.SelectedItems[2].ToString());
As soon as the debugger hits the breakpoint you could select songList and hit Shift-F9.
Now you can explore the songList and check yourself how to get the string of the third column.
SelectedItems returns a SelectedListViewItemCollection and with the indexer you are accessing elements in this collection, i.e. ListViewItems, not the columns. this means that if you have a single row selected the collection contains only one item, and trying to access the third one gives you the error.
Try (edit according to Marco's comment):
songList.SelectedItems[0].SubItems[1].Text
I m trying to use 2 comboboxes to display coursecodes for my studenthandler program.
My Question is why the both the comboboxes are updated to my selected item when I select something in the list.
I m loading my comboboxes in the constructor and both the list are beeing loaded just fine, but when i run the program and choose something in the List the other combobox is also selected with the item that I choose in the first one.
Both the comboboxes uses diffrent list but the same method to load them is called, they don t have the same name or anything like that.
What could cause this?
The debugger runs trough both of these:
private void AllTheCourses_SelectedIndexChanged(object sender,EventArgs e) { }
private void AllCourseCodes_SelectedIndexChanged(object sender, EventArgs e) { }
Probably you're assigning the same event to both comboboxes.
Try to debug the code.
You should bind each combobox to a different event handler. In each event handler, you can call a different fill method or the same one if that's what you want.
I have two Comboboxes where the second one is dependent upon the first one.
When the SelectedIndexChanged event of the first Combobox fires then the second Combobox will be enabled.
After the event, the second Combobox is enabled but I cannot select the ComboBox item.
EDIT
I use Dev express Tools
First Combo I load in Page_Load Event
I use Server Side code:
protected void ASPxComboModule_SelectedIndexChanged(object sender, EventArgs e)
{
LoadSecondCombo();
LoadSecondCombo.Focus();
}
There is no problem in loading, but I'm unable to select 2nd combo item.
What do this do: LoadSecondCombo(); I assume it returns an instance of the control. Where does it set combo.Enabled at?
LoadSecondCombo(); LoadSecondCombo.Focus();
look like method that reference your comboBox and Load it.
When you want to focus LoadSecondCombo should ne an instance of Combo Box. I think it is not and so your combo is not selected.
Can you try going back a bit and look at the Id of combo Box (if it in a composit control like DataGrid / View you will have to do a FindControl) and then focus. I mean typing
comboBtn1.Focus();
Now if that works then you know that your control can be found. In that case mofidify the LoadSescondCombo() to return an instance of combo button if it is not doing so already.
then create a reference to that combobutton by
ComboButton cbtn = LoadSecondCombo(); and
cbtn.Focus();