Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WinForms-UserControl-List<;布尔>;收藏<;布尔>;作为UserControl的可浏览属性_C#_Winforms_User Controls_Propertygrid_Design Time - Fatal编程技术网

C# WinForms-UserControl-List<;布尔>;收藏<;布尔>;作为UserControl的可浏览属性

C# WinForms-UserControl-List<;布尔>;收藏<;布尔>;作为UserControl的可浏览属性,c#,winforms,user-controls,propertygrid,design-time,C#,Winforms,User Controls,Propertygrid,Design Time,为什么在更改第一个属性的值时MessageBox和复选框会在设计时显示,而在向第二个属性添加项时不会显示 private string _first; [Description(""), Category("GostcompSettings"), DefaultValue("27017")] public string First { get { return __first; } set {

为什么在更改第一个属性的值时MessageBox和复选框会在设计时显示,而在向第二个属性添加项时不会显示

    private string _first;
    [Description(""), Category("GostcompSettings"), DefaultValue("27017")]
    public string First
    {
        get { return __first; }
        set
        {

            _searchAreasChceckBoxList.Clear();
            pPanelWithCheckboxies.Controls.Clear();
            int x = 10;
            int y = 10;

            CheckBox _tempCheck = new CheckBox();
            _tempCheck.Checked = true;
            _tempCheck.Location = new Point(x, y);

            _searchAreasChceckBoxList.Add(_tempCheck);
            pPanelWithCheckboxies.Controls.Add(_tempCheck);

            MessageBox.Show("zmiana");
            _first = value;
        }
    }


    private Collection<bool> _second= new Collection<bool>();
    [Description(""), Category("*")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public Collection<bool> Second
    {
        get
        {
            return _second;
        }
        set
        {

            _searchAreasChceckBoxList.Clear();
            pPanelWithCheckboxies.Controls.Clear();
            int x = 10;
            int y = 10;

            CheckBox _tempCheck = new CheckBox();
            _tempCheck.Checked = true;
            _tempCheck.Location = new Point(x, y);

            _searchAreasChceckBoxList.Add(_tempCheck);
            pPanelWithCheckboxies.Controls.Add(_tempCheck);

            MessageBox.Show("*");
            _second= value;
        }
    }
private string\u优先;
[说明(“”),类别(“GostcompSettings”),默认值(“27017”)]
公共字符串优先
{
获取{return\uu first;}
设置
{
_searchAreasChceckBoxList.Clear();
pPanelWithCheckboxies.Controls.Clear();
int x=10;
int y=10;
复选框_tempCheck=新复选框();
_tempCheck.Checked=true;
_临时检查位置=新点(x,y);
_searchAreasChceckBoxList.Add(_tempCheck);
pPanelWithCheckboxies.Controls.Add(_tempCheck);
MessageBox.Show(“zmiana”);
_第一个=值;
}
}
私人收藏_second=新收藏();
[说明(“”),类别(“*”)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
第二次公开募捐
{
得到
{
返回秒;
}
设置
{
_searchAreasChceckBoxList.Clear();
pPanelWithCheckboxies.Controls.Clear();
int x=10;
int y=10;
复选框_tempCheck=新复选框();
_tempCheck.Checked=true;
_临时检查位置=新点(x,y);
_searchAreasChceckBoxList.Add(_tempCheck);
pPanelWithCheckboxies.Controls.Add(_tempCheck);
MessageBox.Show(“*”);
_秒=值;
}
}
当我将集合更改为列表时,情况也是如此。。。 值被保留(或在第二种情况下添加到集合中),设计器为InitializeComponent()生成代码

在@taffer answear之后编辑

public class SearchAreaInfo
{
    public SearchAreasEnum searchArea
    {
        get; set;
    }
}

public class SearchAreaInfoCollection : Collection<SearchAreaInfo>
{
    private Panel _checkboxParent;

    public SearchAreaInfoCollection(Panel checkboxParent) : base()
    {
        _checkboxParent = checkboxParent;
    }

    // called on Add/Insert
    protected override void InsertItem(int index, SearchAreaInfo item)
    {
        base.InsertItem(index, item);
        RepaintChackboxPanel();
    }

    // called on Remove/RemoveAt
    protected override void RemoveItem(int index)
    {
        base.RemoveItem(index);
        RepaintChackboxPanel();
    }

    // called when an element is set by the indexer
    protected override void SetItem(int index, SearchAreaInfo item)
    {
        base.SetItem(index, item);
        RepaintChackboxPanel();
    }

    private void RepaintChackboxPanel()
    {
        //_searchAreasChceckBoxList.Clear();
        _checkboxParent.Controls.Clear();
        int x = 0;
        int y = 0;

        foreach (var item in this)
        {
            CheckBox _tempCheck = new CheckBox();
            _tempCheck.Checked = true;
            _tempCheck.Location = new Point(x, y);
            _tempCheck.BringToFront();

            //_searchAreasChceckBoxList.Add(_tempCheck);
            _checkboxParent.Controls.Add(_tempCheck);                
            x += 5;
            y += 5;
        }
        _checkboxParent.Invalidate();
    }
}

    private SearchAreaInfoCollection _searchAreas;
    [Description(""), Category("GostcompSettings")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public SearchAreaInfoCollection SearchAreas
    {
        get
        {
            return _searchAreas;
        }
    }
公共类搜索区域信息
{
公共搜索区
{
获得;设置;
}
}
公共类SearchAreaInfoCollection:集合
{
专用面板_checkboxParent;
公共SearchAreaInfoCollection(面板复选框父项):base()
{
_checkboxParent=checkboxParent;
}
//在添加/插入时调用
受保护的覆盖无效插入项(int索引,SearchAreaInfo项)
{
基本插入项(索引,项目);
重新绘制ChackboxPanel();
}
//在移除/移除时调用
受保护的覆盖void removietem(int索引)
{
基本删除项(索引);
重新绘制ChackboxPanel();
}
//当索引器设置元素时调用
受保护的覆盖无效集合项(int索引、SearchAreaInfo项)
{
base.SetItem(索引,项);
重新绘制ChackboxPanel();
}
私有void重新绘制ChackboxPanel()
{
//_searchAreasChceckBoxList.Clear();
_checkboxParent.Controls.Clear();
int x=0;
int y=0;
foreach(本文件中的var项目)
{
复选框_tempCheck=新复选框();
_tempCheck.Checked=true;
_临时检查位置=新点(x,y);
_tempCheck.BringToFront();
//_searchAreasChceckBoxList.Add(_tempCheck);
_checkboxParent.Controls.Add(_tempCheck);
x+=5;
y+=5;
}
_checkboxParent.Invalidate();
}
}
私人搜索区域信息收集\u搜索区域;
[说明(“”),类别(“GostcompSettings”)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
公共搜索区域InfoCollection搜索区域
{
得到
{
返回搜索区域;
}
}
现在的问题是:当我在编辑器中将第三项添加到集合中时,它只会绘制一个复选框,但应该绘制三个复选框。。。
此外,在调试过程中,我看到foreach循环进行了3次:第一次收集有1项,第二次收集有2项,第三次收集有3项,但最后我在checkboxPanel中只看到一个复选框。

,因为只有在替换整个收集时才会执行
Second
属性的setter。添加/删除项时,getter返回集合实例(
\u second
),并且将对该对象实例调用
add
/
remove
方法

如果要对元素添加/删除执行检查,请改为创建自定义集合类型:

public class MyBoolCollection: Collection<bool>
{
    // called on Add/Insert
    protected override void InsertItem(int index, bool item)
    {
        // do some checks here
        base.InsertItem(index, item);
    }

    // called on Remove/RemoveAt
    protected override void RemoveItem(int index)
    {
        // do some checks here
        base.RemoveItem(index, item);
    }

    // called when an element is set by the indexer
    protected override void SetItem(int index, bool item)
    {
        // do some checks here
        base.SetItem(index, item);
    }
}
公共类MyBoolCollection:集合
{
//在添加/插入时调用
受保护的覆盖无效插入项(整数索引,布尔项)
{
//在这里检查一下
基本插入项(索引,项目);
}
//在移除/移除时调用
受保护的覆盖void removietem(int索引)
{
//在这里检查一下
基本删除项(索引,项目);
}
//当索引器设置元素时调用
受保护的覆盖无效集合项(整数索引,布尔项)
{
//在这里检查一下
base.SetItem(索引,项);
}
}

您现在使用的内置集合编辑器只会更改集合对象的内容。这很好,但根本不会调用setter。换句话说,它从不创建新的集合对象,也不知道如何创建

要调用属性setter,必须创建自己的UITypeEditor,并让它从其EditValue()方法重写返回新集合。很容易做到,首先添加对System.Design的引用,然后使您的代码看起来类似于以下内容:

using System.Drawing.Design;
...
        [Editor(typeof(MyEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public Collection<bool> Second {
            // etc...
        }

        private class MyEditor : UITypeEditor {
            public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) {
                return UITypeEditorEditStyle.Modal;
            }
            public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {
                var editor = new System.ComponentModel.Design.CollectionEditor(typeof(Collection<bool>));
                var retval = (Collection<bool>)editor.EditValue(context, provider, value);
                return new Collection<bool>(retval);

            }
        }
使用System.Drawing.Design;
...
[编辑器(typeof(MyEditor)、typeof(System.Drawing.Design.UITypeEditor))]
第二次公开募捐{
//等等。。。
}
私有类MyEditor:UITypeEditor{
公共重写UITypeEditorEditStyle GetEditStyle(ITTypeDescriptorContext上下文){
返回UITypeEditorEditStyle.Modal;
}
公共组织