Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding own objects to combobox

Status
Not open for further replies.

VBmim

Programmer
Jun 25, 2001
361
BE
Hello,

I am trying to add 3 of my own objects to a combobox. I can see that 3 items were added, but I cannot see any string or description, only 3 whitespaces.

I have made an interface that implements (among other things) the function ToString(). All my objects are implementing this interface.

Code:
//here I want to populate my combobox
IMyInterface i1=  new MyClass1();
cmbMyCombo.Items.Add(i1);
IMyInterface i2= new MyClass2()
cmbMyCombo.Items.Add(i2);
IMyInterface i3 = new MyClass3();
cmbMyCombo.Items.Add(i3);

//--------------------
public interface IMyInterface 
    {
        string ToString();
        //...
    }

class MyObject1: IMyInterface, System.Data.DataTable
{
     public override string ToString()
     {
        return "some string";
     }
     //...
}

When I am debugging through my code and I am checking the result of i1.ToString(), it gives me the proper result...

I could just add the string to the combobox, but I want to use these objects later on in my program.

Obviously I would like to see some text in my combobox. What am I doing wrong?


Greetz

Mim
 
You should have your objects held in memory somewhere, maybe in a List or an Array. Loop through the list/array and add the object's ToStrings() to the combobox, then when you need to reference the objects later just reference the object in the list by index, which will be the ComboBox's SelectedIndex.

For example: (adjust to suit your program)
Code:
myList.Add(myObj1)
myList.Add(myObj2)
etc...

foreach (object o in myList)
{
    myComboBox.Items.Add(o.ToString())
}

and later to reference the selected object:
Code:
mySelectedObject = myList(myComboBox.SelectedIndex)

I haven't used C# for a while so sorry if the syntax isn't 100%.

Pete
 
Hello PGO01,

Thanks for your response.

I know for sure that adding objects to a listbox/combobox in c# must be possible. What I find in MSDN confirms all that.
If I can't find a solution for my problem I'll have to resolve to adding the strings to my combobox, but my code should work and I want to know why it doesn't...

Greetz,

Mim
 
O
I found what my problem was...

My code was indeed correct, but the properties of my combobox weren't. Somehow I set the 'FormattingEnabled' property of my combobox to true and left the 'FormattingString' blank..... resulting in an empty string.

Voila.

Greetz,

Mim
 
Glad you got your problem resolved.

I create a helper class for this purpose (storing objects in a combo):
Code:
internal class NameObjectMap
{
   private object _object;
   private string _name;

   public NameObjectMap(string name, object obj)
   {
      _object = obj;
      _name = name;
   }

   Public object Object
   {  // getter, setter go here  }
   Public string Name
   {  // getter, setter go here }

   public override string ToString()
   {
      return _name;
   }
}
and you'd file the combo like this:
Code:
   try
   {
      cboInvoice.SuspendLayout();
      cboInvoice.Items.Clear();
      
      foreach (Invoice inv in myInvoices)
      {
         cboInvoice.Items.Add(new NameObjectMap(inv.Payee, inv));
      }
   } finally {   
      cboInvoice.ResumeLayout();
   }
To get what the user selected:
Code:
   if (cboInvoices.SelectedIndex > -1)
   {
      NameObjectMap nom = cboInvoices.SelectedItem as NameObjectMap;
      if (nom == null)
         throw new InvalidOperationException();

      Invoice inv = nom.Object as Invoice;
      if (inv == null)
         throw new InvalidOperationException();

      // Use Invoice object
   }
If you knew that all the objects in the combo were of the same type, you could genericize NameObjectMap.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top