2

在此用户控件中,设计者将项目添加到表单上的此控件中,并动态地将它们放置在表单上。不幸的是,一旦这些控件显示在设计器中的表单上,我就可以抓住一个并将其拉到自定义控件之外。

我该如何防止这种情况?

编辑

请注意,我不是在寻找处理单选按钮的解决方案或替代方案,这只是这种情况下的选择控制。如果它有助于在此处插入您想要的任何类型的控件(按钮、标签、文本框、自定义控件 1),则此问题与处理具有集合/数组类型的属性有关。最重要的是,用户不应该能够在设计器中抓取任何这些控件,其中定义了该控件的实例并将其移动到其他地方。就像 DataGridView 或 ListView 一样,尽管对象类型不同,但用户必须使用属性来更改这些控件的内容。这就是我正在寻找的行为。

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 RadioButton[] _items;

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

        public RBLTest()
        {
            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 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

    }
}
4

1 回答 1

1

嗯,这取决于你想怎么做。首先,我不太喜欢允许直接访问我的用户控件上的控件。出于这个原因,我确保没有任何控件可以公开访问。我所做的是将所有内容设为私有(或有时受保护,因为派生类具有一定的灵活性很好),然后我创建属性来访问控件的必须可访问的属性和由父容器处理的事件。

如果这一切对您来说真的无关紧要,那么我相信您正在寻找的属性是 Control.Locked 属性。在上面的代码中,将动态控件添加到 Controls 集合后,将 Locked 设置为 true。所以:

            this.Controls.Add(rb);
            rb.Locked = true;


尝试这样的事情:

    public class RadioButtonProperties
    {
        private RadioButton _realtedRadioButton = new RadioButton();

        public RadioButtonProperties(RadioButton radio)
        {
            _realtedRadioButton = radio;

            Name = radio.Name;
            Checked = radio.Checked;
            //add other properties
        }

        public string Name
        {
            get
            {
                //logic to to handle if checkbox is null here

                return _realtedRadioButton.Name;
            }

            set
            {
                //logic to to handle if checkbox is null here

                _realtedRadioButton.Name = value;
            }
        }

        public bool Checked
        {
            get
            {
                //logic to to handle if checkbox is null here

                return _realtedRadioButton.Checked;
            }

            set
            {
                //logic to to handle if checkbox is null here

                _realtedRadioButton.Checked = value;
            }
        }
    }

现在在您的自定义控件中,您无需公开访问整个单选框数组,而是授予对数组表示的访问权限,仅公开您想要的单选框属性。例如:

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]  
    public List<RadioButtonProperties> Items { get; set; }
于 2012-09-17T16:06:29.893 回答