propertygrid控件的selectedobject属性的功能。 C# [英] Functionality of the selectedobject property of the propertygrid control. C#

查看:471
本文介绍了propertygrid控件的selectedobject属性的功能。 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.





我尝试过的:



更新:我在这个问题下面添加了一个解决方案,Soluton#2





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;
}

推荐答案

您可以使用 Controls 集合,请参阅此处的示例: c# - 遍历表单上的所有控件,甚至是群组中的那些 - Stack Overflow [ ^ ]
You can use the Controls collection, see example here: c# - Loop through all controls on a form,even those in groupboxes - Stack Overflow[^]


这是一个非常优雅的解决方案,可能不是最好的方法它。如果有人可以批准,请让我知道:



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;
}


这篇关于propertygrid控件的selectedobject属性的功能。 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆