moleInManc
asked on
ListBox.SelectedIndexCollection. Iteration failure.
Hi All,
This has been giving me trouble for hours now and I have finally lost it. I am trying to establish which member of a Forms.Listbox are selected (MultiExtended selection mode). I have tried 2 implementations, both of which result in an array index out of bounds exception. Can anyone see why?
First and most sensible effort:................... .......... ....
ListBox.SelectedIndexColle ction indexes = lstBoxPickable.SelectedInd ices;
try
{
foreach ( int index in indexes )
{
debugBox.Text += "" + index;
}
}
catch (System.Exception)
{
debugBox.Text = "D'oh. exception.";
}
************************** ********** ********** ********** ********
Getting desperate...
IEnumerator myEnum = indexes.GetEnumerator();
string current;
while(myEnum.MoveNext())
{
current= (string)myEnum.Current;
debugBox.Text += current;
}
************************** ********** ********** ********** *******
Any ideas appreciated.
Cheers
This has been giving me trouble for hours now and I have finally lost it. I am trying to establish which member of a Forms.Listbox are selected (MultiExtended selection mode). I have tried 2 implementations, both of which result in an array index out of bounds exception. Can anyone see why?
First and most sensible effort:...................
ListBox.SelectedIndexColle
try
{
foreach ( int index in indexes )
{
debugBox.Text += "" + index;
}
}
catch (System.Exception)
{
debugBox.Text = "D'oh. exception.";
}
**************************
Getting desperate...
IEnumerator myEnum = indexes.GetEnumerator();
string current;
while(myEnum.MoveNext())
{
current= (string)myEnum.Current;
debugBox.Text += current;
}
**************************
Any ideas appreciated.
Cheers
ASKER
Thats why I have been pulling my hair out because thats how it is supposed to wor. Unfortunately for me I get: An unhandled exception of type 'System.IndexOutOfRangeExc eption' occurred in system.windows.forms.dll everytime I try and run the blasted thing!
ASKER
bump
I think the problem is in another porstion of the code. Can you post something more? You don't use any indexing here so this exeption is not expected...
ASKER
yes, you are right. Its the fact that I am handling the mousedown event in the listbox to implement some drag and drop. I assume that this event is used by the listbox to handle the addition of new selections to selected collection. How can I call the default handler in my overridden portion?
It's automatically called, yoo don't have to do anything additionally.
So, When you mouse down, it's at that stage that you want to determine the selected entries ??
Have you actually overriden the mousedown function or are you just consuming the event ?
For the purposes of getting this working, I'd remove all your try/catch blocks.
I would only use try/catch if you are calling something that is out of your control, like a file/database access. That way you can see exactly where the error occurs, and you can examine variable values.
If you are still having problems, can you post some more of your code.. like the actual point where it is falling over.
Chrs
Smg.
Have you actually overriden the mousedown function or are you just consuming the event ?
For the purposes of getting this working, I'd remove all your try/catch blocks.
I would only use try/catch if you are calling something that is out of your control, like a file/database access. That way you can see exactly where the error occurs, and you can examine variable values.
If you are still having problems, can you post some more of your code.. like the actual point where it is falling over.
Chrs
Smg.
ASKER
Hi, yes I'm still having problems. I know this is simple but somehow my brain won't accept that argument. I have pasted my code in below. It works after a fashion but in order to drag something you have to select it and then reselect it for the drag. Thats maybe not so bad but what is unacceptable is that if you drag "One" "Two" and "Three" to one of the empty boxes - then select, maybe "Six" the wrong index is report!!
Any comments suggestions. Thanks for you help on this
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using System.Text;
namespace Packing
{
/// <summary>
/// Summary description for Export.
/// </summary>
public class FrmExport : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblSelect;
private System.Windows.Forms.ListB ox lstBoxPickable;
private System.Windows.Forms.ListB ox lstBoxOp1;
private System.Windows.Forms.ListB ox lstBoxOp2;
private System.Windows.Forms.ListB ox lstBoxOp3;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Cont ainer components = null;
private ListBox.SelectedIndexColle ction selectedList = null;
private int lastSelection = -1;
public FrmExport()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
lstBoxPickabel.Items.Add(" One");
lstBoxPickabel.Items.Add(" Two");
lstBoxPickabel.Items.Add(" Three);
lstBoxPickabel.Items.Add(" Four");
lstBoxPickabel.Items.Add(" Five");
lstBoxPickabel.Items.Add(" Six");
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lblSelect = new System.Windows.Forms.Label ();
this.lstBoxPickable = new System.Windows.Forms.ListB ox();
this.lstBoxOp1 = new System.Windows.Forms.ListB ox();
this.lstBoxOp2 = new System.Windows.Forms.ListB ox();
this.lstBoxOp3 = new System.Windows.Forms.ListB ox();
this.SuspendLayout();
//
// lblSelect
//
this.lblSelect.Location = new System.Drawing.Point(48, 24);
this.lblSelect.Name = "lblSelect";
this.lblSelect.Size = new System.Drawing.Size(112, 16);
this.lblSelect.TabIndex = 0;
this.lblSelect.Text = "Select orders to pick:";
//
// lstBoxPickable
//
this.lstBoxPickable.Locati on = new System.Drawing.Point(48, 48);
this.lstBoxPickable.Name = "lstBoxPickable";
this.lstBoxPickable.Select ionMode = System.Windows.Forms.Selec tionMode.M ultiExtend ed;
this.lstBoxPickable.Size = new System.Drawing.Size(184, 134);
this.lstBoxPickable.TabInd ex = 1;
this.lstBoxPickable.MouseD own += new System.Windows.Forms.Mouse EventHandl er(this.On PickListMo useDown);
this.lstBoxPickable.Select edIndexCha nged += new System.EventHandler(this.O nSelection Changed);
//
// lstBoxOp1
//
this.lstBoxOp1.AllowDrop = true;
this.lstBoxOp1.Location = new System.Drawing.Point(16, 216);
this.lstBoxOp1.Name = "lstBoxOp1";
this.lstBoxOp1.Size = new System.Drawing.Size(136, 173);
this.lstBoxOp1.TabIndex = 2;
this.lstBoxOp1.DragDrop += new System.Windows.Forms.DragE ventHandle r(this.OnD ragDrop);
this.lstBoxOp1.DragEnter += new System.Windows.Forms.DragE ventHandle r(this.OnD ragEnter);
//
// lstBoxOp2
//
this.lstBoxOp2.AllowDrop = true;
this.lstBoxOp2.Location = new System.Drawing.Point(176, 216);
this.lstBoxOp2.Name = "lstBoxOp2";
this.lstBoxOp2.Size = new System.Drawing.Size(136, 173);
this.lstBoxOp2.TabIndex = 3;
this.lstBoxOp2.DragDrop += new System.Windows.Forms.DragE ventHandle r(this.OnD ragDrop);
this.lstBoxOp2.DragEnter += new System.Windows.Forms.DragE ventHandle r(this.OnD ragEnter);
//
// lstBoxOp3
//
this.lstBoxOp3.AllowDrop = true;
this.lstBoxOp3.Location = new System.Drawing.Point(336, 216);
this.lstBoxOp3.Name = "lstBoxOp3";
this.lstBoxOp3.Size = new System.Drawing.Size(136, 173);
this.lstBoxOp3.TabIndex = 4;
this.lstBoxOp3.DragDrop += new System.Windows.Forms.DragE ventHandle r(this.OnD ragDrop);
this.lstBoxOp3.DragEnter += new System.Windows.Forms.DragE ventHandle r(this.OnD ragEnter);
//
// FrmExport
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(488, 446);
this.Controls.Add(this.lst BoxOp3);
this.Controls.Add(this.lst BoxOp2);
this.Controls.Add(this.lst BoxOp1);
this.Controls.Add(this.lst BoxPickabl e);
this.Controls.Add(this.lbl Select);
this.Name = "FrmExport";
this.StartPosition = System.Windows.Forms.FormS tartPositi on.CenterS creen;
this.Text = "Pickable";
this.ResumeLayout(false);
}
#endregion
private void OnPickListMouseDown(object sender, System.Windows.Forms.Mouse EventArgs e)
{
if ( lstBoxPickable.SelectedInd ices.Equal s(selected List) )
{
lstBoxPickable.DoDragDrop( lstBoxPick able.Selec tedItems.T oString(), DragDropEffects.Copy);
}
lastSelection = -1;
}
private void OnDragEnter(object sender, System.Windows.Forms.DragE ventArgs e)
{
//ensure that dragged data is suitable i.e text
if (e.Data.GetDataPresent(Dat aFormats.T ext, true))
{
e.Effect = DragDropEffects.Copy;
}
}
private void OnDragDrop(object sender, System.Windows.Forms.DragE ventArgs e)
{
foreach (int Index in lstBoxPickable.SelectedInd ices)
{
MessageBox.Show(Index.ToSt ring());
}
}
private void OnSelectionChanged(object sender, System.EventArgs e)
{
selectedList = lstBoxPickable.SelectedInd ices;
}
}
}
Any comments suggestions. Thanks for you help on this
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using System.Text;
namespace Packing
{
/// <summary>
/// Summary description for Export.
/// </summary>
public class FrmExport : System.Windows.Forms.Form
{
private System.Windows.Forms.Label
private System.Windows.Forms.ListB
private System.Windows.Forms.ListB
private System.Windows.Forms.ListB
private System.Windows.Forms.ListB
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Cont
private ListBox.SelectedIndexColle
private int lastSelection = -1;
public FrmExport()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
lstBoxPickabel.Items.Add("
lstBoxPickabel.Items.Add("
lstBoxPickabel.Items.Add("
lstBoxPickabel.Items.Add("
lstBoxPickabel.Items.Add("
lstBoxPickabel.Items.Add("
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lblSelect = new System.Windows.Forms.Label
this.lstBoxPickable = new System.Windows.Forms.ListB
this.lstBoxOp1 = new System.Windows.Forms.ListB
this.lstBoxOp2 = new System.Windows.Forms.ListB
this.lstBoxOp3 = new System.Windows.Forms.ListB
this.SuspendLayout();
//
// lblSelect
//
this.lblSelect.Location = new System.Drawing.Point(48, 24);
this.lblSelect.Name = "lblSelect";
this.lblSelect.Size = new System.Drawing.Size(112, 16);
this.lblSelect.TabIndex = 0;
this.lblSelect.Text = "Select orders to pick:";
//
// lstBoxPickable
//
this.lstBoxPickable.Locati
this.lstBoxPickable.Name = "lstBoxPickable";
this.lstBoxPickable.Select
this.lstBoxPickable.Size = new System.Drawing.Size(184, 134);
this.lstBoxPickable.TabInd
this.lstBoxPickable.MouseD
this.lstBoxPickable.Select
//
// lstBoxOp1
//
this.lstBoxOp1.AllowDrop = true;
this.lstBoxOp1.Location = new System.Drawing.Point(16, 216);
this.lstBoxOp1.Name = "lstBoxOp1";
this.lstBoxOp1.Size = new System.Drawing.Size(136, 173);
this.lstBoxOp1.TabIndex = 2;
this.lstBoxOp1.DragDrop += new System.Windows.Forms.DragE
this.lstBoxOp1.DragEnter += new System.Windows.Forms.DragE
//
// lstBoxOp2
//
this.lstBoxOp2.AllowDrop = true;
this.lstBoxOp2.Location = new System.Drawing.Point(176, 216);
this.lstBoxOp2.Name = "lstBoxOp2";
this.lstBoxOp2.Size = new System.Drawing.Size(136, 173);
this.lstBoxOp2.TabIndex = 3;
this.lstBoxOp2.DragDrop += new System.Windows.Forms.DragE
this.lstBoxOp2.DragEnter += new System.Windows.Forms.DragE
//
// lstBoxOp3
//
this.lstBoxOp3.AllowDrop = true;
this.lstBoxOp3.Location = new System.Drawing.Point(336, 216);
this.lstBoxOp3.Name = "lstBoxOp3";
this.lstBoxOp3.Size = new System.Drawing.Size(136, 173);
this.lstBoxOp3.TabIndex = 4;
this.lstBoxOp3.DragDrop += new System.Windows.Forms.DragE
this.lstBoxOp3.DragEnter += new System.Windows.Forms.DragE
//
// FrmExport
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(488, 446);
this.Controls.Add(this.lst
this.Controls.Add(this.lst
this.Controls.Add(this.lst
this.Controls.Add(this.lst
this.Controls.Add(this.lbl
this.Name = "FrmExport";
this.StartPosition = System.Windows.Forms.FormS
this.Text = "Pickable";
this.ResumeLayout(false);
}
#endregion
private void OnPickListMouseDown(object
{
if ( lstBoxPickable.SelectedInd
{
lstBoxPickable.DoDragDrop(
}
lastSelection = -1;
}
private void OnDragEnter(object sender, System.Windows.Forms.DragE
{
//ensure that dragged data is suitable i.e text
if (e.Data.GetDataPresent(Dat
{
e.Effect = DragDropEffects.Copy;
}
}
private void OnDragDrop(object sender, System.Windows.Forms.DragE
{
foreach (int Index in lstBoxPickable.SelectedInd
{
MessageBox.Show(Index.ToSt
}
}
private void OnSelectionChanged(object sender, System.EventArgs e)
{
selectedList = lstBoxPickable.SelectedInd
}
}
}
ASKER
Sorry theres a typo in the above :
public FrmExport()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
lstBoxPickable.Items.Add(" One");
lstBoxPickabel.Items.Add(" Two");
lstBoxPickabel.Items.Add(" Three);
lstBoxPickabel.Items.Add(" Four");
lstBoxPickabel.Items.Add(" Five");
lstBoxPickabel.Items.Add(" Six");
//
// TODO: Add any constructor code after InitializeComponent call
//
}
public FrmExport()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
lstBoxPickable.Items.Add("
lstBoxPickabel.Items.Add("
lstBoxPickabel.Items.Add("
lstBoxPickabel.Items.Add("
lstBoxPickabel.Items.Add("
lstBoxPickabel.Items.Add("
//
// TODO: Add any constructor code after InitializeComponent call
//
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks smeghead. I appreciate all of your help
no problem.. but grade 'b' ??? where did I go wrong ?
Smg
Smg
ASKER
my bad, sloppy mouse clicky... long lunch ;~)
foreach (int Index in listBox1.SelectedIndices)
{
MessageBox.Show(Index.ToSt
}