Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Overriding the ToString method of SelectedItems property of a ListBox Control

Posted on 2004-08-05
9
Medium Priority
?
1,190 Views
Last Modified: 2010-07-27
Hi

I am trying to create a custom ListBox Control that returns a concatenated string of all the selecteditems. This I have been trying to do it by overriding the ToString method of the 'SelectedObjectCollection' class, but having issues with it. Is there an easy and a better way to do this?

Thanks,
Kalyan
0
Comment
Question by:kkondapalli
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 10

Expert Comment

by:ibost
ID: 11728856
Are you inheriting the regular ListBox control?

Why not override the ListBox's ToString() method.

public class myListBox: System.Windows.Forms.ListBox
{

      public override string ToString()
      {
            string s = "";
            for (int x = 0; x < this.Items.Count; x++)
            {
                  s += (s.Length > 0) ? ", " : "";
                  s += this.Items[x].ToString();
            }
            return s;
      }

}
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11729034
it would be SelectedItems not Items but other thatn that the above would work.

As for why your mechanism did not work ... you were inheriting from 'SelectedObjectCollection'  but your subclass was not the one being used in the listbox ...
0
 

Author Comment

by:kkondapalli
ID: 11729073
Yes I am inherting the regular ListBox control.

I am looking to concatenate all the selected Items only, and yes i can do that also by creating a new method or override the ToString method of the Listbox control

But conceptually if some other programmer uses this control, he wont that the ToString override for Selected Items is ListBox ToString override, rather he would try to look under the SelectedItems.ToString() method and this is the Method I am trying to overwrite.

I have written some code that basically creates new poperty called TpeSelectedItems to the ListBox which has an overload for the ToString() Method that does the concatenation. It works properly when I have a breakpoint in that loop and run while debugging, but when I remove the debug point and run it, it gives me 'ArgumentOutofRangeException'.  

Also here is the most bizarre thing. If u see the code i have new property for ListBox called TpeSelectedIndices which also has the ToString method that returns the concatenated string of all the selectedIndexes. If i call the TpeSelectedIndices.ToString method before the TpeSelectedItems.ToString method, the later gives me the proper output all the time.


Here is the code snippet For TpeListBox:

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace TpeFormControls
{
      /// <summary>
      /// Summary description for TpeListBox.
      /// </summary>
      public class TpeListBox : System.Windows.Forms.ListBox
      {
            public class TpeSelectedIndexCollection: System.Windows.Forms.ListBox.SelectedIndexCollection
            {
                  public TpeSelectedIndexCollection(ListBox owner): base(owner)
                  {

                  }
                  /// <summary>
                  /// returns a concatenated string of all the selected items
                  /// </summary>
                  /// <param name="seperator">seperator used to concatenate all the selected items</param>
                  /// <returns></returns>
                  public string ToString(Char seperator)
                  {
                        String returnvalue = "";
                        try
                        {                        
                              for (Int32 row = 0; row < this.Count; row++)
                              {
                                    returnvalue += String.Concat(this[row].ToString(), seperator);
                              }
                              if (returnvalue != "")
                                    returnvalue = returnvalue.Remove(returnvalue.Length - 1, 1);
                        }
                        catch (IndexOutOfRangeException _InEx)
                        {
                              MessageBox.Show(_InEx.Message);
                        }
                        return (returnvalue);
                  }
            }
            public class TpeSelectedObjectCollection: System.Windows.Forms.ListBox.SelectedObjectCollection
            {
                  public TpeSelectedObjectCollection(ListBox owner): base(owner)
                  {

                  }
                  /// <summary>
                  /// returns a concatenated string of all the selected items
                  /// </summary>
                  /// <param name="seperator">seperator used to concatenate all the selected items</param>
                  /// <returns></returns>
                  public string ToString(Char seperator)
                  {
                        String returnvalue = "";
                        try
                        {      
                              for (Int32 row = 0; row < this.Count; row++)
                              {
                                    returnvalue += String.Concat(this[row].ToString(), seperator);
                              }
                              if (returnvalue != "")
                                    returnvalue = returnvalue.Remove(returnvalue.Length - 1, 1);
                        }
                        catch (IndexOutOfRangeException _InEx)
                        {
                              MessageBox.Show(_InEx.Message);
                        }
                        return (returnvalue);
                  }
            }
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;
            private TpeFormControls.TpeListBox.TpeSelectedIndexCollection tpeselectedindices;
            private TpeFormControls.TpeListBox.TpeSelectedObjectCollection tpeselecteditems;

            public TpeListBox()
            {
                  // This call is required by the Windows.Forms Form Designer.
                  InitializeComponent();

                  // TODO: Add any initialization after the InitializeComponent call

                  tpeselectedindices = new TpeSelectedIndexCollection(this);
                  tpeselecteditems = new TpeSelectedObjectCollection(this);
            }

            /// <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 Component 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()
            {
                  components = new System.ComponentModel.Container();
            }
            #endregion

            public TpeFormControls.TpeListBox.TpeSelectedIndexCollection TpeSelectedIndices
            {
                  get {return tpeselectedindices;}
            }
            public TpeFormControls.TpeListBox.TpeSelectedObjectCollection TpeSelectedItems
            {
                  get {return tpeselecteditems;}
            }

      }
}

Here is the Code Snippet from the sample app where i am using this control


            private void button1_Click(object sender, System.EventArgs e)
            {
                  tpeListBox1.Refresh();
                  //MessageBox.Show(this.tpeListBox1.TpeSelectedIndices.Count.ToString());
                  MessageBox.Show(this.tpeListBox1.SelectedItems.Count.ToString());
                  //MessageBox.Show(this.tpeListBox1.TpeSelectedIndices.ToString(','));                  
                  MessageBox.Show(this.tpeListBox1.TpeSelectedItems.ToString(','));
            }

            private void tpeListBox1_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  //MessageBox.Show(this.tpeListBox1.TpeSelectedIndices.Count.ToString());
                  MessageBox.Show(this.tpeListBox1.SelectedItems.Count.ToString());                                    
                  //MessageBox.Show(this.tpeListBox1.TpeSelectedIndices.ToString(','));                  
                  MessageBox.Show(this.tpeListBox1.TpeSelectedItems.ToString(','));
            }


Hope this makes the question more clear
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Assisted Solution

by:s_sansanwal
s_sansanwal earned 400 total points
ID: 11731350
Change code

                         for (Int32 row = 0; row < this.Count; row++)
                         {
                              returnvalue += String.Concat(this[row].ToString(), seperator);
                         }

to

foreach(object item in this)
{
returnvalue += String.Concat(item.ToString(), seperator);
}
avoid out of index error
0
 

Author Comment

by:kkondapalli
ID: 11731763
Hi sansanwal,

Thanks for the tip, now I am not getting the Out of range Error, but this time, its not refreshing the selectedItem value, it always shows the first Item that was selected. For Eg: I have the following values in the list
Line1
Line2
Line3
Line4
Line5

Now the first time I select Line3, the ToString Method return value is Line3, but from then on when i try to select a different item, it still gives me Line3 as the return value.

Thanks,
Kalyan.
0
 

Author Comment

by:kkondapalli
ID: 11731793
Again as I said, the above logic works fine when I am stepping thru the code in the debug mode. But when not in debug mode, it gives me the wrong selected Items.
0
 
LVL 9

Expert Comment

by:s_sansanwal
ID: 11731885
It seems the selecteditems is not set before calling your custom collection

I think you need to call
this.tpeListBox1.SelectedItem.ToString(); // Just write this code in button click before messagebox
in button click event to re-initalise your collection

0
 
LVL 1

Accepted Solution

by:
hammadraza earned 1600 total points
ID: 11733566
Hi kkondapalli,

Just change the property "TpeSelectedItems" with code below

public TpeFormControls.TpeListBox.TpeSelectedObjectCollection TpeSelectedItems
{
                  
      get {

                 tpeselecteditems = new TpeSelectedObjectCollection(this);
                 return tpeselecteditems;
           }
}


Hope it will work.

0
 

Author Comment

by:kkondapalli
ID: 11735784
hammadraza -
 Your solution worked like a charm. It all makes sense now why the collection was not getting refreshed. Thanks a lot for that.

Thanks a lot guys for taking time to take a look at the problem.

Thanks,
Kalyan.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question