如何使用集合编辑器窗口在 GroupBox 中创建文本框?

Posted

技术标签:

【中文标题】如何使用集合编辑器窗口在 GroupBox 中创建文本框?【英文标题】:How to create TextBoxes in a GroupBox using the Collection Editor window? 【发布时间】:2021-05-31 12:44:18 【问题描述】:

我想创建一个继承自 GroupBox 的自定义控件,并拥有一个 TextBox 集合的属性。我打算在 Designer 中创建尽可能多的 TextBox,就像使用 TabControl 可以完成的一样,可以通过 Collection Editor 窗口在 TabPages 属性中创建页面。 我创建了出现在属性窗口中的属性 TextBoxList,当我单击“...”集合编辑器窗口打开以创建 TextBox 并设置其属性时,但是当我单击确定按钮时,没有一个 TextBox 被添加到我的 GroupBox 中。 TextBox 实例已创建,但未添加到 GroupBox。有人可以帮助我将 TextBox 添加到 GroupBox 中吗?跟着代码走。

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();
        
    


【问题讨论】:

考虑使用包含 GoupBox 等的用户控件。 你的班级是partial。你是如何创建GroupBoxWithTextBox的? 这应该如何工作? Collection&lt;TextBox&gt;(或 IList)不可序列化(TextBox 不可序列化)。您那里有自定义编辑器,带有自定义对话框吗?编辑器不工作吗? -- 什么是LabeledTextBox?您在同一属性中有两种不同的类型(LabeledTextBoxTextBox)。 吉米,“不可序列化”是什么意思?当我将 GroupBoxWithTextBox 控件添加到窗体并转到 Visual Studio 属性窗口时,有属性“TextBoxList”。它的字段包含单词“(集合)”,当我单击时,“...”出现在场的右侧。单击“...”会打开一个 Visual Studio 标准窗口,我可以在其中添加 TextBoxes 并设置其属性(类似于使用 TabControl 的属性“TabPages”创建 TsbPages 的过程)。问题是在此窗口中创建 TextBox 后,没有任何内容添加到 GroupBox。我更正了代码。 @F***o Augusto,有更新吗?如果您的问题已经解决,您可以点击“✔”将相应的回复标记为答案。 【参考方案1】:

根据我的研究,如果我们想在设计中添加文本框,我们需要覆盖 CollectionEditor 类。

我写了如下代码,你可以看看。

代码:

[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);
        
    

结果:(需要手动设置文本框位置)

希望对你有帮助。

【讨论】:

以上是关于如何使用集合编辑器窗口在 GroupBox 中创建文本框?的主要内容,如果未能解决你的问题,请参考以下文章

如何在对话框编辑器中创建窗口占位符

如何在 iOS8 的通知窗口中创建按钮?

如何检查是不是在 Xcode UI 测试中创建了新场景?

如何在 C# 中创建/编辑 PNG 文件?

如何在PPT中创建自己的幻灯片母版

ARCGIS10软件中创建要素窗口不见了,怎么弄出来啊