Link to home
Start Free TrialLog in
Avatar of moleInManc
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.SelectedIndexCollection indexes = lstBoxPickable.SelectedIndices;

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
Avatar of smegghead
smegghead
Flag of United Kingdom of Great Britain and Northern Ireland image

This works fine for me...

      foreach (int Index in listBox1.SelectedIndices)
      {
            MessageBox.Show(Index.ToString());
      }
Avatar of moleInManc
moleInManc

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.IndexOutOfRangeException' occurred in system.windows.forms.dll everytime I try and run the blasted thing!
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...
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.
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.ListBox lstBoxPickable;
            private System.Windows.Forms.ListBox lstBoxOp1;
            private System.Windows.Forms.ListBox lstBoxOp2;
            private System.Windows.Forms.ListBox lstBoxOp3;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            private ListBox.SelectedIndexCollection 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.ListBox();
                  this.lstBoxOp1 = new System.Windows.Forms.ListBox();
                  this.lstBoxOp2 = new System.Windows.Forms.ListBox();
                  this.lstBoxOp3 = new System.Windows.Forms.ListBox();
                  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.Location = new System.Drawing.Point(48, 48);
                  this.lstBoxPickable.Name = "lstBoxPickable";
                  this.lstBoxPickable.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
                  this.lstBoxPickable.Size = new System.Drawing.Size(184, 134);
                  this.lstBoxPickable.TabIndex = 1;
                  this.lstBoxPickable.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnPickListMouseDown);
                  this.lstBoxPickable.SelectedIndexChanged += new System.EventHandler(this.OnSelectionChanged);
                  //
                  // 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.DragEventHandler(this.OnDragDrop);
                  this.lstBoxOp1.DragEnter += new System.Windows.Forms.DragEventHandler(this.OnDragEnter);
                  //
                  // 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.DragEventHandler(this.OnDragDrop);
                  this.lstBoxOp2.DragEnter += new System.Windows.Forms.DragEventHandler(this.OnDragEnter);
                  //
                  // 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.DragEventHandler(this.OnDragDrop);
                  this.lstBoxOp3.DragEnter += new System.Windows.Forms.DragEventHandler(this.OnDragEnter);
                  //
                  // FrmExport
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(488, 446);
                  this.Controls.Add(this.lstBoxOp3);
                  this.Controls.Add(this.lstBoxOp2);
                  this.Controls.Add(this.lstBoxOp1);
                  this.Controls.Add(this.lstBoxPickable);
                  this.Controls.Add(this.lblSelect);
                  this.Name = "FrmExport";
                  this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                  this.Text = "Pickable";
                  this.ResumeLayout(false);

            }
            #endregion

            private void OnPickListMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {      
                  if ( lstBoxPickable.SelectedIndices.Equals(selectedList) )
                  {
                              lstBoxPickable.DoDragDrop(lstBoxPickable.SelectedItems.ToString(), DragDropEffects.Copy);
                  }
                  
                  lastSelection = -1;
            }

            private void OnDragEnter(object sender, System.Windows.Forms.DragEventArgs e)
            {
                  //ensure that dragged data is suitable i.e text
                  if (e.Data.GetDataPresent(DataFormats.Text, true))
                  {
                        e.Effect = DragDropEffects.Copy;
                  }
            }

            private void OnDragDrop(object sender, System.Windows.Forms.DragEventArgs e)
            {      
                  foreach (int Index in lstBoxPickable.SelectedIndices)
                  {
                        MessageBox.Show(Index.ToString());
                  }
            }

            private void OnSelectionChanged(object sender, System.EventArgs e)
            {
                  selectedList = lstBoxPickable.SelectedIndices;
            }
      }
}
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
               //
          }

ASKER CERTIFIED SOLUTION
Avatar of smegghead
smegghead
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks smeghead. I appreciate all of your help
no problem.. but grade 'b' ??? where did I go wrong ?

Smg
my bad, sloppy mouse clicky... long lunch ;~)