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
Solved

Overriding the ToString method of SelectedItems property of a ListBox Control

Posted on 2004-08-05
9
1,183 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
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 9

Assisted Solution

by:s_sansanwal
s_sansanwal earned 100 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 400 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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