2

このユーザーコントロールでは、デザイナーがフォームのこのコントロールにアイテムを追加し、フォームに動的に配置します。残念ながら、これらのコントロールがデザイナーのフォームに表示されたら、それをつかんでカスタムコントロールのすぐ外に引き出すことができます。

これを防ぐにはどうすればよいですか?

編集

ラジオボタンを処理するための解決策や代替手段を探しているわけではないことに注意してください。これは、このシナリオで選択するコントロールにすぎません。ここに必要なタイプのコントロール(Button、Label、TextBox、CustomControl1)を挿入するのに役立つ場合、この問題はコレクション/配列タイプのプロパティの処理に関するものです。肝心なのは、このコントロールのインスタンスが定義されているデザイナーで、ユーザーがこれらのコントロールを取得して別の場所に移動できないようにすることです。オブジェクトのタイプは異なりますが、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 に答える