473,799 Members | 3,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.IndexOut OfRangeExceptio n 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(o bject sender, EventArgs e)
{
if (lstBoxAll.Sele ctedIndex != -1)
{
for (int x = 0; x < lstBoxAll.Items .Count; x++)
{
if (lstBoxAll.GetS elected(x) == true)
{
// when x=2 and lstBoxAll.GetSe lected(x) == true, I
still am not able to retrieve the 3rd item from my list
lstBoxSelected. Items.Add(lstBo xAll.SelectedIt ems[x].ToString());
}
}

//for (int counter = lstBoxAll.Selec tedItems.Count - 1;
counter >= 0; counter--)
//{
//
lstBoxAll.Items .Remove(lstBoxA ll.SelectedItem s[counter]);
//}
}
}

Any ideas?

Thank you
Joao
Dec 27 '06 #1
4 2433
"Joao" <jd*******@hotm ail.com.REMOVE_ THISwrote in message
news:%2******** *******@TK2MSFT NGP04.phx.gbl.. .
Hi all,

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

System.IndexOut OfRangeExceptio n 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(o bject sender, EventArgs e)
{
if (lstBoxAll.Sele ctedIndex != -1)
{
for (int x = 0; x < lstBoxAll.Items .Count; x++)
{
if (lstBoxAll.GetS elected(x) == true)
{
// when x=2 and lstBoxAll.GetSe lected(x) == true, I
still am not able to retrieve the 3rd item from my list

lstBoxSelected. Items.Add(lstBo xAll.SelectedIt ems[x].ToString());
}
}

//for (int counter = lstBoxAll.Selec tedItems.Count - 1;
counter >= 0; counter--)
//{
//
lstBoxAll.Items .Remove(lstBoxA ll.SelectedItem s[counter]);
//}
}
}

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

Therefore, lstBoxAll.Selec tedItems[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.Selec tedItems.Count; x++)
{
lstBoxSelected. Items.Add(lstBo xAll.SelectedIt ems[x]);
}
//remove items
for (int counter = lstBoxAll.Selec tedItems.Count - 1; counter >= 0;
counter--)
{
lstBoxAll.Items .Remove(lstBoxA ll.SelectedItem s[counter]);
}

And it worked great! Thank you so much.

Joao
Dec 27 '06 #3
"Joao" <jd*******@hotm ail.com.REMOVE_ THISwrote in message
news:uy******** ******@TK2MSFTN GP06.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
3338
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 the list box (items they wish inserted into the table), or they can select items in the list box to delete (from the table). Once they have finished making any changes, they are to click a SAVE button to actually commit the changes to the database....
4
1916
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 AutoPostBack="False" BackColor="#ffffff" id="lstCrewChief" runat="server" Rows="1" DataSource="<%# DsCrewChief1 %>" Enabled="True"
0
1121
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
1480
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 from the fact that on MouseDown the ListBox changes the highlighting but not the selection collection (it updates the collection on MouseUp) Anyone have any suggestions on how to Drag/Drop from a ListBox with
4
5043
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 of computations, etc). when all of the calculations are complete, I'm trying to change the listbox selection mode from single to MultiExtended, using: myListBox.SelectionMode=SelectionMode.MultiExtended
4
2562
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 them... I found an example online in the form of a DragNDrop.cs file that I've included in my project (attached below).
1
2502
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 fine). There is then a combo box they can select a field from (eg CompanyID etc) and then the list box below that contains the values (eg Microsoft, Novell etc). These are all multi-select list boxes. Now I can get the code to work if the user...
0
1389
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 other listbox display the same text but still have the same value behind. so when i submit the lbTo data should read something like Smith John 7A with a value of 657576 for each person in the lbTo.
3
5052
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 on a form. The first listbox is populated according to command buttons (Command 14 & Command 15) that are clicked by the user (draws data via SQL statement - see code below). The second listbox is populated by user selection from the first listbox. I...
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10484
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10228
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.