1

I want to use a property of an object that's inside an object. Is there a way to achieve this?

WebProxy proxy = new WebProxy("127.0.0.1:80");
ListBox listBox = new ListBox();
listBox.DisplayMember = **"Address.Authority"**; //Note: Address.Authority is an property inside the WebProxy object
listBox.Items.Add(proxy);

Thanks.

Denis Brat
  • 487
  • 5
  • 14

2 Answers2

1

Have a look at this question, it is essentially asking the same thing - the principle does not change between DataGridView and ListBox. Short answer: it's possible, but convoluted.

Community
  • 1
  • 1
Bradley Smith
  • 13,353
  • 4
  • 44
  • 57
  • I used the sabe approach that OP and created a new object that contained my WebProxy object and a string that gives me the Authority of the proxy. Thanks for your answer. – Denis Brat Nov 25 '10 at 17:21
0

How about you subclass WebProxy to, for instance, WebProxyEx and implement the IList interface which sort of(expects an object that implements the IList or IListSource interfaces) is a pre-requisite to use the .DataSource property of listbox. Like following:

class WebProxyEx : WebProxy, IList
    {
        private object[] _contents = new object[8];
        private int _count;

        public WebProxy w;

        public WebProxyEx(string address)
        {
            _count = 0;
            w = new WebProxy(address);
            this.Add(w.Address.Authority);
        }
...

And use it like:

ListBox lb;
public Form1()
{
    InitializeComponent();
    WebProxyEx w = new WebProxyEx("127.0.0.1:80");//Use your sub class
    lb = new ListBox();
    this.Controls.Add(lb);

    lb.DataSource = w;//assign the datasource.
    //lb.DisplayMember = "Address.Authority"; //Automatically gets added in the WebProxEx constructor.

}

Gives following output in the listbox:

127.0.0.1

KMån
  • 9,896
  • 2
  • 31
  • 41
  • Thanks for your answer. I don't know if its necessary to implement IList as I already have a List as my datasource. – Denis Brat Nov 25 '10 at 17:17
  • See [MSDN](http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.datasource%28VS.71%29.aspx) for `.DataSource` property; *>An object that implements the IList interface, such as a DataSet or an Array* – KMån Nov 26 '10 at 10:56