473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

I have 2 listboxes with SelectionMode = MultiExtended and I just cannot figure out why I get the error below:

Hi all,

I have 2 listboxes with SelectionMode = MultiExtended and I just cannot
figure out why I get the error below:

System.IndexOutOfRangeException was unhandled
Message="Index was outside the bounds of the array."
Source="System.Windows.Forms"

Listbox Data:
Index String
0 A - Selected
1 B - Not Selected
2 C - Selected

private void button3_Click(object sender, EventArgs e)
{
if (lstBoxAll.SelectedIndex != -1)
{
for (int x = 0; x < lstBoxAll.Items.Count; x++)
{
if (lstBoxAll.GetSelected(x) == true)
{
// when x=2 and lstBoxAll.GetSelected(x) == true, I
still am not able to retrieve the 3rd item from my list
lstBoxSelected.Items.Add(lstBoxAll.SelectedItems[x].ToString());
}
}

//for (int counter = lstBoxAll.SelectedItems.Count - 1;
counter >= 0; counter--)
//{
//
lstBoxAll.Items.Remove(lstBoxAll.SelectedItems[counter]);
//}
}
}

Any ideas?

Thank you
Joao
Dec 27 '06 #1
4 2418
"Joao" <jd*******@hotmail.com.REMOVE_THISwrote in message
news:%2***************@TK2MSFTNGP04.phx.gbl...
Hi all,

I have 2 listboxes with SelectionMode = MultiExtended and I just cannot
figure out why I get the error below:

System.IndexOutOfRangeException was unhandled
Message="Index was outside the bounds of the array."
Source="System.Windows.Forms"

Listbox Data:
Index String
0 A - Selected
1 B - Not Selected
2 C - Selected

private void button3_Click(object sender, EventArgs e)
{
if (lstBoxAll.SelectedIndex != -1)
{
for (int x = 0; x < lstBoxAll.Items.Count; x++)
{
if (lstBoxAll.GetSelected(x) == true)
{
// when x=2 and lstBoxAll.GetSelected(x) == true, I
still am not able to retrieve the 3rd item from my list

lstBoxSelected.Items.Add(lstBoxAll.SelectedItems[x].ToString());
}
}

//for (int counter = lstBoxAll.SelectedItems.Count - 1;
counter >= 0; counter--)
//{
//
lstBoxAll.Items.Remove(lstBoxAll.SelectedItems[counter]);
//}
}
}

Any ideas?
Yup - you have *three* items in lstBoxAll, but only *two* of them are
selected. Therefore, lstBoxAll.Items.Count = 3, but
lstBoxAll.SelectedItems.Count = 2.

Therefore, lstBoxAll.SelectedItems[2] is outside the bounds of the
SelectedItems collection... :-)
Dec 27 '06 #2
Hello David and Mark,

I knew what was wrong by looking at the debugger but I could not find the
right object to use.
Now (with your tips) I changed the code to the following:

//add items
for (int x = 0; x < lstBoxAll.SelectedItems.Count; x++)
{
lstBoxSelected.Items.Add(lstBoxAll.SelectedItems[x]);
}
//remove items
for (int counter = lstBoxAll.SelectedItems.Count - 1; counter >= 0;
counter--)
{
lstBoxAll.Items.Remove(lstBoxAll.SelectedItems[counter]);
}

And it worked great! Thank you so much.

Joao
Dec 27 '06 #3
"Joao" <jd*******@hotmail.com.REMOVE_THISwrote in message
news:uy**************@TK2MSFTNGP06.phx.gbl...
And it worked great! Thank you so much.
No problem.
Dec 27 '06 #4
No worries. Glad you've got it working.

D
Dec 28 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ryan | last post by:
Hello, I have a quick question (I hope). I have a form with a combo box and a multi-selection list box. The list box is based on a query. Users can select values from the cmbobox to add to...
4
by: bill yeager | last post by:
I have several template columns inside of a datagrid. Inside of these template columns are databound listboxes: <asp:TemplateColumn HeaderText="Crew Chiefs"> <ItemTemplate> <asp:listbox...
0
by: **Developer** | last post by:
I have a ListBox with: SelectionMode = SelectionMode.MultiExtended If I use the Shift key to select multiple items In the MouseMove event I see a
0
by: **Developer** | last post by:
Has anyone found out how to do DragDrop from a ListBox with SelectionMode.MultiExtended I've been searching Google and see that it is an old problem. I know from past experience that it stems...
4
by: lgbjr | last post by:
Hi All, I've got a listbox on a VB.NET form. when the form opens, the ListBox SelectionMode is set to Single. while running various routines on the form, items get added to the list box (results...
4
by: Full NameEntry | last post by:
Hi all, I'm new to csharp... learning it by muddling my way though examples. In my current example, I'm trying to make a form with a bunch of listboxes that have drag and drop enabled amongst...
1
by: Intrepid_Yellow | last post by:
Hi, I have the following code that runs my report generator. The user selects a table from a combo box, then whatever fields they want from a list box. (This part all works and the report runs...
0
by: piercy | last post by:
i have two listboxes, lbData and lbTo. i need to drag and drop between the two. lbData.datasource = tblfilter; i need to be able to drag and drop the data from one to the other then on the...
3
JodiPhillips
by: JodiPhillips | last post by:
Hello everyone, there are many questions and answers relating to moving items between two listboxes here and on the net in general, however, none answer my specific problem. I have two listboxes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.