Windows Develop Bookmark and Share   
 index > Windows Forms General > Adding selected ListBox Items to an ArrayList?
 

Adding selected ListBox Items to an ArrayList?

Hi there

I am using C# 2005 (VS2005) and wish to add selected ListBox items to an ArrayList. The items in the ArrayList will then later be used in a SQL query to extract data from the SQL database.

My first questions is, is the ArrayList my best option? If not, please indicate what I should use with detailed description with what I should be doing.

Thus far, I have come up with the following code:
usingSystem;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
namespacePortMan
{
publicpartialclassfrmExpPosSelectionDMT:Form
{
ArrayListDIDS=newArrayList();
publicfrmExpPosSelectionDMT()
{
InitializeComponent();
}
...
privatevoidlbDivisionCode_Click(objectsender,EventArgse)
{
intcount=lbDivisionCode.SelectedItems.Count;
for(inti=0;i!=this.lbDivisionCode.SelectedItems.Count;i++)
{
intDID=(int)lbDivisionCode.SelectedIndex+1;
DIDS.Add(DID);
}
}
...
}
}

When I click click each item individually, they are added to the ArrayList, in the correct order. The problem with this approach is that the user may forget which value has already been selected, and select it for a second or even third time.But when I do multiple selections (Ctrl+Click) everything falls apart. Let me explain, I select 3 items in order [3,2,4], yet there are 6 items in the ArrayList [3,2,2,2,2,2].

What am I missing? What am I doing wrong? I have also tried various other events, incl LostFocus and SelectedIndexChanged. Irrespective what I do, this is the scenario I get.

Any and all advise will be greatly appreciated.

Many thanx.

It's not the blowing of the wind that determines your destination, it's the set of the sail. J Rohn
Excalibur7  Monday, February 16, 2009 8:46 AM
You'll need to check if the item is already in the list before calling Add(). The more natural collection object for this is HashSet<>.
Hans Passant.
  • Marked As Answer byExcalibur7 Wednesday, February 18, 2009 5:59 AM
  •  
nobugz  Monday, February 16, 2009 2:12 PM
Replace all your code with:

private void listView1_SelectedIndexChanged(object sender, EventArgs e) {
DIDS = new int[listView1.SelectedIndices.Count];
listView1.SelectedIndices.CopyTo(DIDS, 0);
}

Also check if you need DIDS at all, I doubt you do.

Hans Passant.
  • Marked As Answer byExcalibur7 Wednesday, February 18, 2009 12:00 PM
  •  
nobugz  Wednesday, February 18, 2009 10:49 AM
You'll need to check if the item is already in the list before calling Add(). The more natural collection object for this is HashSet<>.
Hans Passant.
  • Marked As Answer byExcalibur7 Wednesday, February 18, 2009 5:59 AM
  •  
nobugz  Monday, February 16, 2009 2:12 PM
If all you want is to get a collection of all the selected items, you could also use a checked list box and then simply do the following...

if(listBox.CheckedIndices.Count>0)
{
CheckedItemCollectiona=listBox.CheckedItems;
}

When the user is done selecting entries.

You can then foreach iterate through that collection.

The beauty of a checked list box, is that all the selection stuff is done for you, and the user will also get feedback on what is selected.

If you want a "realtime" selection, then you must do what has already been suggested, you must check your selection array if ithas already beenadded and then find out how you want to treat that (ignore, remove etc).

Cheers,
Chris
HalfAsleep  Monday, February 16, 2009 3:25 PM
Thanx Guys

I have changed the code in the event as follows:
privatevoidlbDivisionCode_Click(objectsender,EventArgse)
{
for(inti=0;i!=this.lbDivisionCode.SelectedIndices.Count;i++)
{
//if((Control.ModifierKeys==Keys.ControlKey)||
//(Control.ModifierKeys==Keys.ShiftKey)||(Control.ModifierKeys==Keys.None))
//{
DID=(int)lbDivisionCode.SelectedIndex+1;
if(DIDS.Contains(DID))
{
DIDS.Remove(DID);
}
else
{
DIDS.Add(DID);
}
//}
}
DIDS.Sort();
}

When selecting and deselecting items in the listbox (individually) the code works 100%. My problem is when selecting items using the Ctrl or Shift keys, only the items selected without these keys are added to the ArrayList.

I do realise that I would need to add further code for the Shift-select, but first I just want to get this to work. Is there something I am mising in the if-statement?

Alternatively, is there any way I can change the backcolor of an item when it is selected in the listbox? Thereby the user can only use the mouse when making a selection and still have record of what has been selected.

Thanx.
It's not the blowing of the wind that determines your destination, it's the set of the sail. J Rohn
Excalibur7  Wednesday, February 18, 2009 6:10 AM
Why are you calling Remove()?
Hans Passant.
nobugz  Wednesday, February 18, 2009 10:03 AM
My thinking is, once this works 100%, when the user selected an item it will have a blue backgound and the item will be added to the ArrayList.

The user may have made an invalid/wrong selection, and wishes to remove that single item from the ArrayList. Selecting the item in question for a second time should then remove it from the ArrayList as well as set the background for that item back to white.
It's not the blowing of the wind that determines your destination, it's the set of the sail. J Rohn
Excalibur7  Wednesday, February 18, 2009 10:11 AM
Replace all your code with:

private void listView1_SelectedIndexChanged(object sender, EventArgs e) {
DIDS = new int[listView1.SelectedIndices.Count];
listView1.SelectedIndices.CopyTo(DIDS, 0);
}

Also check if you need DIDS at all, I doubt you do.

Hans Passant.
  • Marked As Answer byExcalibur7 Wednesday, February 18, 2009 12:00 PM
  •  
nobugz  Wednesday, February 18, 2009 10:49 AM
Thanx nobugs

The event now looks as follows:
privatevoidlbDivisionCode_SelectedIndexChanged(objectsender,EventArgse)
{
DIDS=newint[lbDivisionCode.SelectedIndices.Count];
lbDivisionCode.SelectedIndices.CopyTo(DIDS,0);
for(intDID=0;DID!=DIDS.Length;DID++)
{
inttemp=DIDS[DID];
temp++;
DIDS[DID]=temp;
}
}

Is thereperhaps a better way of doing the iteration? I need the countvalue of each item as opposed to their index value. I have tested and seen that DIDS[DID]++ also works within the for-statement instead of the three lines above. Are there any risks indoing that instead?

Thanx for all your help.
It's not the blowing of the wind that determines your destination, it's the set of the sail. J Rohn
Excalibur7  Wednesday, February 18, 2009 12:02 PM

You can use google to search for other answers

Custom Search

More Threads

• Token Privilidges
• How to loop through lables?
• Need to open new browser window from WinForm
• Argument Exception
• Exception when binding a dll dynamically
• First time call ToolTip.Show() in balloon mode places tip pointer at wrong position
• SelectedValue remains null after selecting a ListBox item
• Splitcontainer position relative to Panels
• Stop Ctrl+Tab in the tabcontrol
• Where to find MenuStrip programming elements(messages-functions-Notifications-Macros)