简体   繁体   中英

How to expose TextBox Text Property in a User Control

I hope someone can help with what I am sure must be a stupid error on my part.

I have striped a user control I am working on down to its basic elements. I have placed a Text box on a User Control and to expose the TextBox Text property I have created the following

[Browsable(true)]
    public override string Text 
    { 
        get
        {
            return textBox1.Text;
        }
        
        set
        {
            textBox1.Text = value;
        }
    
    }

Everything appears to work fine with one exception. When I place the control on the form the TextBox displays the name of the Control ie Control 1. I have tried setting the properties Default Attribute as follows to no affect.

DefaultValue((string)null),

How can I stop the control from displaying the control name?

Thank you in advice for any guidance.

A solution for this problem is a custom ParentControlDesigner where you can override the InitializeNewComponent method to set or clear the Text property.

Example

using System;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Security.Permissions;

namespace YourProject
{
    [DefaultProperty(nameof(Text))]
    [DefaultEvent(nameof(TextChanged))]
    [Designer(typeof(MyUserControlDesigner))]
    public partial class MyUserControl : UserControl
    {
        public MyUserControl()
        {
            InitializeComponent();

            // Update the base.Text whenever the TextBox.Text property is changed.
            textBox1.TextChanged += (s, e) =>
            {
                if (textBox1.Text != Text) Text = textBox1.Text;
            };
        }

        // Get and set the text of the base property instead of the TextBox's
        // to get the TextChanged event raised.
        [Browsable(true),
            EditorBrowsable(EditorBrowsableState.Always),
            DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override string Text
        {
            get => base.Text;
            set => base.Text = value;
        }

        protected override void OnTextChanged(EventArgs e)
        {
            // Update the TextBox.Text property whenever the base property is changed.
            if (Text != textBox1.Text) textBox1.Text = Text;
            base.OnTextChanged(e);
        }

        // If you need to handle the TextChanged event...
        /// <inheritdoc cref="Control.TextChanged"/>
        [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
        public new event EventHandler TextChanged
        {
            add { base.TextChanged += value; }
            remove { base.TextChanged -= value; }
        }
    }

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    public class MyUserControlDesigner : ParentControlDesigner
    {
        public override void InitializeNewComponent(IDictionary defaultValues)
        {
            base.InitializeNewComponent(defaultValues);

            if (Component is MyUserControl c) c.Text = "StephenH"; // c.Text = "";
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM