Search code examples
c#visual-studiowinformstextboxgroupbox

How to create TextBoxes in a GroupBox using the Collection Editor window?


I would like to create a customized control that inherits from GroupBox and have a property that is a collection of TextBox. I intent to create in Designer as many TextBoxes as I want, similarly what could be done with TabControl, which one could create pages in TabPages properties through the Collection Editor window. I created the property TextBoxList that appears in properties window and when I click in the “…” Collection Editor window opens to create the TextBox and set its properties, but when I click the ok button, none TextBox is added to my GroupBox. The TextBox instances were created, but were not added to the GroupBox. Does somebody could help me with the TextBox addition to the GroupBox? Follows the code.

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Forms;

namespace CustomizedControl
{
    public partial class GroupBoxWithTextBox : GroupBox
    {
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(System.ComponentModel.Design.CollectionEditor),
                       typeof(System.Drawing.Design.UITypeEditor))]
        [Description("The TextBoxes in GroupBox control."), Category("Behavior")]
        public Collection<TextBox> TextBoxList
        {
            get
            {
                return _textBoxList;
            }
        }
        private Collection<TextBox> _textBoxList = new Collection<TextBox>();

        public GroupBoxWithTextBox()
        {
            InitializeComponent();
        }
    }
}


Solution

  • Based on my research, we need to override the class CollectionEditor if we want to add the textbox in design.

    I write the following code and you can have a look.

    Code:

    [Serializable]
        public partial class GroupBoxWithTextBox : GroupBox
        {
    
            public GroupBoxWithTextBox()
            {
                SetStyle(ControlStyles.DoubleBuffer, true);
                SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                Padding = new Padding(0);
                textBox = new TextBox();
                textBox.Size = new System.Drawing.Size(60, 30);
                Controls1.Add(textBox);
                textBox.Dock = DockStyle.Top;
                
            }
            [EditorAttribute(typeof(NewCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
            public   ControlCollection Controls1
            {
                get
                {
                    return base.Controls;
                }
                set
                {
                    
                  
                }
            }
    
    
            private TextBox textBox;
    
    
         
        }
    
        public partial class NewCollectionEditor : CollectionEditor
        {
            public NewCollectionEditor(Type t) : base(t)
            {
            }
    
            // *** Magic happens here: ***
            // override the base class to SPECIFY the Type allowed
            // rather than letting it derive the Types from the collection type
            // which would allow any control to be added
            protected override Type[] CreateNewItemTypes()
            {
                Type[] ValidTypes = new[] { typeof(TextBox) };
                return ValidTypes;
            }
    
            public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
            {
                return base.EditValue(context, provider, value);
            }
        }
    

    Result:(You need to set the textbox location manually)

    Hope this could help you.

    enter image description here