views:

57

answers:

4

[.NET 2]

how should I list a form controls in a Combobox of the same form(like VS designer does)?

alt text

I tried:

cboObjectSelection.DataSource = Me.Controls

but this does not work.

Is there a possibility to filter(customize) this list?

A: 

You might be able to do it if you set the ComboBox.DisplayMember to "Name" but you'd then not get any controls contained in other controls so I think you'd have to get out the names of all the controls (recursively) and insert them into a collection and then pass that as the DataSource.

ho1
A: 

I have put the code in a button's click event, you can modify it according to you requirement. Hope this will help you.

private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.Controls.Count; i++)
            {
                s = this.Controls[i].GetType().ToString();
                comboBox1.Items.Add(s);

            }

        }
Pavan Navali
Suppose if you have a panel, which in turn contains other controls, you can check just put a condition -- check if the Control.Count is greater that zero...you can use the same logic recursively.
Pavan Navali
A: 
Andras Zoltan
oh... i forget to mention .NET 2.
serhio
how can I link the control to the selectedItem of the ComboBox ?
serhio
SelectedItem will be the instance of ControlInfo, which you can cast up ((ControlInfo)comboBox1.SelectedItem) to get at it's Control member. Or CType(comboBox1.SelectedItem, ControlInfo) is it.
Andras Zoltan
Assuming you're familiar with what the C#3/.Net 3.5 code is doing here, you should be able to modify easily to build a List() instead. Perhaps I'm being lazy (if I get time later I'll re-edit!)
Andras Zoltan
mdeas... Finally `foreach ctrl myCombo.Items.add(ctrl.Name+ctrl.GetType)` seems to have the same effect :)
serhio
Yes it certainly will - although if you need to resolve that item back to the control it was built from (i.e. on a selected item) you will have trouble.
Andras Zoltan
don't see any trouble: `foreach c in Controls if c.name = selected.Name`
serhio
A: 

You'll have to iterate each item in the Controls collection and add it to the ComboBox's Items collection. The simplest code would look like this:

For Each c As Control in Me.Controls
    cboObjectSelection.Items.Add(c.Name)
Next

The issues here are that Me.Controls is hierarchical. IE, a the controls inside a Panel on your form will be missed with this case. You would need to add all the Panel's Controls to get EVERYTHING on the form. Which is an ideal application of recursion:

Private Sub AddControls(ByVal Combo As ComboBox, ByVal Control As Control)
    For Each c As Control In Control.Controls
        Combo.Items.Add(c.Name)
        AddControls(Combo, c)
    Next
End Sub

To get the control back, you have to do this:

Dim c As Control = Me.Controls.Find(ComboBox1.SelectedItem.ToString(), True)(0)

The second parameter tells the find controls whether to recurse through the hierarchy of controls. The Find method returns an array of controls, hence the (0) at the end to get the first element. You should be safe here regarding out of bounds exceptions (IE, the Find method doesn't find anything) because everything in the ComboBox will have been added by code a few minutes ago.

Hope this helps!

Tim Coker