locked
Add COM Objects as ComboBox items RRS feed

  • Question

  • Hi there!

    I need help regarding something I thought would work but doesn't seems so: I am working with objects that are provided via a COM extension (to be specific from the Novell GroupWise Object API, if anyone wants to have a look). I can work quite nicely with the Objects and their properties. What I don't get is how to use and Address objects as Items for the CoboBox.

    /* type definition: https://www.novell.com/documentation/developer/groupwise_sdk/gwsdk_gwobjapi/data/hq16f8tm.html */
    GroupwareTypeLibrary.Application gw = new GroupwareTypeLibrary.Application();
    /* type definition: https://www.novell.com/documentation/developer/groupwise_sdk/gwsdk_gwobjapi/data/hbkjddyi.html */
    GroupwareTypeLibrary.Addresses proxies = gw.ProxyHistory;
    
    foreach(GroupwareTypeLibrary.Address a in proxies)
    {
        /* type definition: https://www.novell.com/documentation/developer/groupwise_sdk/gwsdk_gwobjapi/data/hg33vr0y.html */
        this.comboBox1.Items.Add(a);
    }
    this.comboBox1.ValueMember = "DisplayName";
    this.comboBox1.DisplayMember = "DisplayName";

    When Iadd the Address objects to the ComboBox it seems that the properties are not accessible and I get this view:

    The properties for the objects are accessible when I use them in my code:

    Any ideas?

    Monday, January 26, 2015 3:05 PM

Answers

  • The datasource has to be something that implements the Ilist Interface.

    https://msdn.microsoft.com/en-us/library/system.collections.ilist(v=vs.110).aspx


    Success
    Cor

    Monday, January 26, 2015 6:53 PM
  • Hi SebastianWiass,

    If you added the Address objects to the item of the ComboBox, you could override the ToString() method to generate the text the user will see. A simple code below might be useful to you:

    public class ComboboxItem
    {
        public string Text { get; set; }
        public object Value { get; set; }
        public override string ToString()
        {
            return Text;
        }
    }
    private void Test()
    {
        ComboboxItem item = new ComboboxItem();
        item.Text = "Item text1";
        item.Value = 12;
        comboBox1.Items.Add(item);
        comboBox1.SelectedIndex = 0;
        MessageBox.Show((comboBox1.SelectedItem as ComboboxItem).Value.ToString());
    }

    If you want to use the DisplayMember, you could bind the combobox datasource and set the property like below, the displaymember property is used when binding datasource:

           private void button1_Click(object sender, EventArgs e)
            {
                ComboboxItem item1 = new ComboboxItem();
                item1.Text = "Item text1";
                item1.Value = 12;
                ComboboxItem item2 = new ComboboxItem();
                item2.Text = "Item text2";
                item2.Value = 22;
                //this.comboBox1.Items.Add(item1);
                //this.comboBox1.Items.Add(item2);
                List<ComboboxItem> list=new List<ComboboxItem>();
                list.Add(item1);
                list.Add(item2);
                this.comboBox1.DataSource = list;
                this.comboBox1.DisplayMember="Text";
    
            }
            public class ComboboxItem
            {
                public string Text { get; set; }
                public object Value { get; set; }
                //public override string ToString()
                //{
                //    return Text;
                //}
            }
    

    In addition, if you have any questions about “GroupwareTypeLibrary”, I would recommend you go to the office site. If you have any further questions about C#, please feel free to post in this forums.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, January 27, 2015 5:19 AM

All replies

  • The datasource has to be something that implements the Ilist Interface.

    https://msdn.microsoft.com/en-us/library/system.collections.ilist(v=vs.110).aspx


    Success
    Cor

    Monday, January 26, 2015 6:53 PM
  • Hi SebastianWiass,

    If you added the Address objects to the item of the ComboBox, you could override the ToString() method to generate the text the user will see. A simple code below might be useful to you:

    public class ComboboxItem
    {
        public string Text { get; set; }
        public object Value { get; set; }
        public override string ToString()
        {
            return Text;
        }
    }
    private void Test()
    {
        ComboboxItem item = new ComboboxItem();
        item.Text = "Item text1";
        item.Value = 12;
        comboBox1.Items.Add(item);
        comboBox1.SelectedIndex = 0;
        MessageBox.Show((comboBox1.SelectedItem as ComboboxItem).Value.ToString());
    }

    If you want to use the DisplayMember, you could bind the combobox datasource and set the property like below, the displaymember property is used when binding datasource:

           private void button1_Click(object sender, EventArgs e)
            {
                ComboboxItem item1 = new ComboboxItem();
                item1.Text = "Item text1";
                item1.Value = 12;
                ComboboxItem item2 = new ComboboxItem();
                item2.Text = "Item text2";
                item2.Value = 22;
                //this.comboBox1.Items.Add(item1);
                //this.comboBox1.Items.Add(item2);
                List<ComboboxItem> list=new List<ComboboxItem>();
                list.Add(item1);
                list.Add(item2);
                this.comboBox1.DataSource = list;
                this.comboBox1.DisplayMember="Text";
    
            }
            public class ComboboxItem
            {
                public string Text { get; set; }
                public object Value { get; set; }
                //public override string ToString()
                //{
                //    return Text;
                //}
            }
    

    In addition, if you have any questions about “GroupwareTypeLibrary”, I would recommend you go to the office site. If you have any further questions about C#, please feel free to post in this forums.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, January 27, 2015 5:19 AM
  • So since I can not directly add to the COM classes myself (as they are provided as-is and as a COM extension) I need to write a wrapper class to make this work. I think I should do that. The COM extension has a lot of classes and I could make a more general wrapper class for this sort of thing.

    Thanks for the reply Cor!

    Tuesday, January 27, 2015 6:35 AM
  • Hello Edward!

    Since I dont have access to the source code of the COM library I can not override the to string method. As Cor suggested I could implement it in a way that the adresses get stuffed into a IList implementing class and then bind this as the DataSource to the ComboBox.

    My intention was to work with the types provided by the library as-is. But unfortunately it looks like this is not possible. If I now implement a wrapper class for this and use it, I need to reverse search the right object to work with it again. But that will be allright I guess.

    Thanks for your help!

    Tuesday, January 27, 2015 6:42 AM