Click here to Skip to main content
15,911,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!
Im new in C# and want to ask a little help.

I have array of objects and System.Windows.Forms.ComboBox element on form.
I need fill item list of ComboBox and attach object to every item.
Using Delphi I can do this with existing method ComboBox.AddObject.
Delphi
var ArrayOfObjects = array of Objects;
...
ArrayOfObjects := GetArrayOfObjectsFromSource(); 
...
for i:=0 to Length(ArrayOfObjects)-1 do
begin
  ComboBox.AddItem(ArrayOfObjects[i].stringproperty, TObject(ArrayOfObjects[i]));
end;

// After this I can do the next:
...
var O : Object;
...
  O := Object(ComboBox.Items.Objects[cbAccounts.ItemIndex]);
...
//and make something with received object.
...


But I can't find the same method in C#. Using method ComboBox.Items.Add I can only fill list of items.
C#
foreach(Object O in ArrayOfObjects)
{
  // I know how to add new string to ComboBox
  ComboBox.Items.Add(O.stringproperty); 
  //and I don't know how insert Object :( 
}
...
  Object NewObject = ????????


Can anyone help me, please?
Posted
Updated 31-Dec-11 4:16am
v2

Rather than rely on objects, in C# it is considered good practice to use explicit objects. For example:
C#
public class MyItem
    {
    public string String { get; set; }
    public int Int { get; set; }
    public MyItem(string s, int i) { String = s; Int = i; }
    public override string ToString()
        {
        return "MyItem: " + String;
        }
    }

private void myForm_Load(object sender, EventArgs e)
    {
    MyItem[] list = new MyItem[2];
    list[0] = new MyItem("Hello", 1);
    list[1] = new MyItem("Goodbye", 2);
    myComboBox.Items.AddRange(list);
    }

private void myButton_Click(object sender, EventArgs e)
    {
    MyItem mi = (MyItem)myComboBox.Items[1];
    Console.WriteLine(mi.String + mi.Int);
    }

What this does is put the whole of your item into the ComboBox - the displayed string comes from the overridden ToString method - and you can access all the properties you want when you get the item from the ComboBox.

Personally, I would use a List rather than an array to init the ComboBox, but you seemed to use an array, so that is what I showed.

XML
private void myForm_Load(object sender, EventArgs e)
    {
    List<MyItem> list = new List<MyItem>();
    list.Add(new MyItem("Hello", 1));
    list.Add(new MyItem("Goodbye", 2));
    myComboBox.Items.AddRange(list.ToArray());
    }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Dec-11 12:20pm    
Exactly. My 5.
--SA
Shadow373 31-Dec-11 13:31pm    
Thank you! Your explanation really help me not only with comboboxes.
OriginalGriff 31-Dec-11 15:58pm    
You are welcome! Happy new year!
In those good old days it was not advised but sure common to mingle everything together as one large user interface driven bowl of spaghetti. But when you think about it, why would you retrieve an array with object instances (that probably have nothing to do with the user interface at all) and store that into a ComboBoxItem as an object? Besides the fact that the object is now untyped you also have an array of a lot of instances of which the user will choose one that is useful and the rest can be discarded.

The solution would be to make user interface more clean and therefor devide the GetArrayOfObjectsFromSource up into 2 different methods. One method that will give you only the names which you can use to full the combobox and another method that gives you the necessary object by name (selected in the combobox).

If, by the way, the object that you request has nothing to do with the user interface at all then it would even be better it is a method that would do the processing all together. The user interface is just for entering the necessary parameters required to start the processing. You would just pass the selected combobox value to that method (in a specialized class) and let it handle it from there. This is to avoid the so called magic-push-button-pattern that cripples software projects beyond believe.

Well, a lot of explanation that hopefully made it clear that the key is to keep the UI clean from all kind of stuff that doesn't belong there.

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900