自定义控件在Visual Studio设计器中无法更新

7
我正在创建一个自定义控件,用于布局单选按钮(不一定是单选按钮。我只是想学习如何这样做,以便在未来制作包含多个控件列表的更复杂的东西),这些按钮通过Items属性添加(类似于其他一些控件)。
我可以构建项目,从组件面板将其拖放到表单上,并通过Items属性添加单选按钮。不幸的是,除非您执行以下操作之一,否则设计器中不会更新此内容:
- 重新构建项目2-3次 - 在设计器中关闭并重新打开窗体
起初,我将放置这些按钮的逻辑包含在Initialize后的构造函数中,但那并没有起作用,所以我将其移到了Form_Load中。
我错过了什么?上述选项只是短期解决方法,而不是解决方案。
RBLTest.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WFCL_Library
{
    public partial class RBLTest : UserControl
    {
        private List<RadioButton> _items;

        private int leftSpacing = 100;
        private int topSpacing = 25;

        public RBLTest()
        {
            _items = new List<RadioButton>();
            InitializeComponent();
        }

        private void RadioButtonList_Load(object sender, EventArgs e)
        {
            int curLeftPos = 0;
            int curTopPos = 0;
            foreach (RadioButton rb in _items)
            {
                rb.Location = new Point(curLeftPos, curTopPos);
                rb.Size = new Size(85, 17);

                curLeftPos += leftSpacing;

                if (curLeftPos > this.Width)
                {
                    curLeftPos = 0;
                    curTopPos += topSpacing;
                }

                this.Controls.Add(rb);                
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public List<RadioButton> Items
        {
            get
            {
                return _items;
            }
            set
            {
                _items = value;
            }
        }
    }       
}

RBLTest.Designer.cs

namespace WFCL_Library
{
    partial class RBLTest
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // RBLTest
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "RBLTest";
            this.Size = new System.Drawing.Size(407, 44);
            this.Load += new System.EventHandler(this.RadioButtonList_Load);
            this.ResumeLayout(false);

        }

        #endregion

    }
}
2个回答

4

不应使用Load事件或构造函数,因为使用设计器工具添加控件时会创建UserControl的实例并触发Load事件。在您的情况下,当这种情况发生时,_item仍为空。另一个问题是存在一些序列化列表的问题,因此我建议使用数组:

public partial class RBLTest : UserControl {
    private RadioButton[] _items;

    private int leftSpacing = 100;
    private int topSpacing = 25;

    public RBLTest( ) {
        InitializeComponent( );
    }

    [DesignerSerializationVisibility( DesignerSerializationVisibility.Content )]
    public RadioButton[] Items {
        get {
            return _items;
        }
        set {
            _items = value;

            int curLeftPos = 0;
            int curTopPos = 0;
            foreach ( RadioButton rb in _items ) {
                rb.Location = new Point( curLeftPos, curTopPos );
                rb.Size = new Size( 85, 17 );

                curLeftPos += leftSpacing;

                if ( curLeftPos > this.Width ) {
                    curLeftPos = 0;
                    curTopPos += topSpacing;
                }

                this.Controls.Add( rb );
            }
        }
    }
}

在表单设计器中的结果:
这里输入图片描述

1
哦,我现在只想抱抱你,我花了很长时间玩弄这个问题,但是在我的谷歌搜索中没有找到任何有关此主题的信息。你是否有参考文章讨论在设计师中序列化泛型列表的问题? - Mohgeroth
2
@Mohgeroth,您不需要另一个集合,因为您已经有了一个控件集合,这也是您的子控件必须存在的地方。 - Tergiver
1
不好意思,我从来没有使用过列表,也不知道是否可能,但如果您需要一个列表,可以在属性设置器中执行以下操作:List<RadioButton> list = _items.ToList(); - Omar

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接