Click here to Skip to main content
15,887,285 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I'm creating a usercontrol and I want to replicate the SelectedObject property of the .NET PropertyGrid Control.  i.e.... I want the user to be able to select a control, from a list of controls which have been placed on my UserControls' Parent form. They will be selecting the control in the propertygrid inside Visual Studio.  Well, technically, they can manually assign it in code, like you can with the SelectedObject property.


What I have tried:

Update : I added a solution, down below this question, "Soluton #2

private string[] ObjectBuffer;
private string _SelectedObject = "Unknown";
[Category("Custom"), TypeConverter(typeof(SelectedObjectConverter))]
public string SelectedObject
{
    get { fillit(); return _SelectedObject; }
    set { _SelectedObject = value; }
}

private void fillit()
{
    if (this.Parent == null) return;

    List<string> listBuffer = new List<string>();

    foreach(Control c in (this.Parent as Control).Controls)
    {
        listBuffer.Add(c.Name);
    }

    ObjectBuffer = listBuffer.ToArray();

    listBuffer.Clear();
}

private class SelectedObjectConverter : StringConverter
{
    //This works ....
    //private static StandardValuesCollection SelectedObjects =
    //      new StandardValuesCollection(
    //         new string[]{"Mother", "Father", "Sister",
    //    "Brother", "Daughter", "Son",
    //    "Aunt", "Uncle", "Cousin"});

    //But, how do I access the above ObjectBuffer[]
    private static StandardValuesCollection SelectedObjects =
          new StandardValuesCollection(ObjectBuffer);

    public override bool GetStandardValuesSupported(ITypeDescriptorContext context) => true;
    public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) => false;

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) => SelectedObjects;
}
Posted
Updated 11-Dec-18 5:52am
v10
Comments
OriginalGriff 9-Dec-18 1:41am    
"But, I already know how to do that."

Then what is the problem?
Where are you stuck?
What have you tried?
What help do you need?
Member 13839622 9-Dec-18 12:50pm    
I updated the question and added a code sample I just tried.
Member 13839622 10-Dec-18 19:43pm    
I'd updated and changed the question a million times. I've got a working version, of what I need. BUT, it only works by manually creating a string[] inside the SelectedObjectConverter class. I can't access anything outside the class. So, I can't iterate over the controls and build my real string[]. Need help figuring this out.

You can use the Controls collection, see example here: c# - Loop through all controls on a form,even those in groupboxes - Stack Overflow[^]
 
Share this answer
 
Comments
Member 13839622 11-Dec-18 10:44am    
That part I can do. I'm really close. I've posted my latest code snippet.
Here's a non elegant solution, which probably isn't the best way to do it. If anyone can approve upon it, PLEASE LET ME KNOW:

private string _SelectedObject = "Unknown";
[Category("Custom"), TypeConverter(typeof(SelectedObjectConverter))]
public string SelectedObject
{
    get { fillit(); return _SelectedObject; }
    set { _SelectedObject = value; }
}

private void fillit()
{
    if (this.Parent == null) return;

    string[] controlStrings = new string[(this.Parent as Control).Controls.Count];

    for (int i = 0; i < (this.Parent as Control).Controls.Count; i++)
    {
        controlStrings[i] = (this.Parent as Control).Controls[i].Name;
    }

    SelectedObjectConverter.SelectedObjects = new SelectedObjectConverter.StandardValuesCollection(controlStrings);
}

private class SelectedObjectConverter : StringConverter
{
    public static StandardValuesCollection SelectedObjects = new StandardValuesCollection(new string[0]);

    public override bool GetStandardValuesSupported(ITypeDescriptorContext context) => true;
    public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) => false;

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) => SelectedObjects;
}
 
Share this answer
 
v2

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