How would I set the label of my UserControl at des

2020-07-09 02:26发布

Here's the setup: I've created a user control MyUserControl. On this control is a simple label. When I drop a new instance of this control on a form, I would like the label to be the name of the control. For example, if this is the first instance of the control, VStudio automatically sets the name of it to be "myUserControl1". I would simply like the label to read "myUserControl1". If this is the fifth control, VStudio will name it myUserControl5. I would like the label to change accordingly... but only if it hasn't already been set. Once I set it to read "The Label", it should be left alone.

I thought it would be easy. In the constructor, set label1.Text = this.Name. But then I realized, at instantiation this.Name is simply MyUserControl. It hasn't been named yet by VisualStudio and the CONTAINING InitializeComponent() method.

Ah! I'll just override the "Name" field. When it gets set, check for DesignMode and update the label!... but I can't override the name. It's not virtual or abstract. I can use "new" to force it, but then, it never fires, thus defeating the purpose.

Which event can I use to denote when the designer has named the control? I know that the redmond folks do it all the time when they drop a control on the form, so it's very possible. I'm actually quite stumped by this little riddle.

标签: c# winforms
3条回答
劫难
2楼-- · 2020-07-09 02:38

Edit the designer file. For example, say I have a Form named Form1. So, I would have Form1.cs, Form1.resx, and Form1.Designer.cs in visual studio. If opening Form1.Designer.cs you will see this:

   #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>

This is a warning, but you can still edit this file.

Here's a sample edit, I am setting label3 text to label1's name:

            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(371, 146);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(35, 13);
            this.label3.TabIndex = 10;
            this.label3.Text = "label3";
            //My Edit
            this.label3.Text = this.label1.Name;

Now in the form designer in Visual Studio I see that label3 has label1's name:

enter image description here

A similiar approach could be used for your user control.

查看更多
姐就是有狂的资本
3楼-- · 2020-07-09 02:49

So, here's the final answer:

Per Hans' suggestion, it did require a ControlDesigner. I created a simple designer that would interface with some internal properties, and I did get it to work. Here's the catch, though: It's a bit of a hack. There doesn't seem to be a single field that has the value I'm looking for, so I had to do a .ToString() on an object, then parse out the first section. I figured that's about as clean as it is going to get.

public class MyControlDesigner : ControlDesigner
{
    public MyControlDesigner()
    {
    }


    public override void InitializeNewComponent(IDictionary defaultValues)
    {
        base.InitializeNewComponent(defaultValues);

        MyControl control1 = this.Component as MyControl;
        control1.LabelText = control1.ToString().Split(" ".ToCharArray())[0];
    }
}

Then at the top of MyControl, I simply added a new attribute:

[DesignerAttribute(typeof(MyControlDesigner))]
public partial class MyControl : UserControl
{
    ...

    // Using the property "Text" Causes Visual Studio to crash!!!
    public string LabelText
    {
        get { return label1.Text; }
        set { label1.Text = value; }
    }

    ...
}

As you can see, it wasn't very complicated, but it is a bit hack-ish. The control locked up the IDE when I tried to override "Text". Not sure why, but when I called it "LabelText", it worked perfectly.

查看更多
男人必须洒脱
4楼-- · 2020-07-09 03:02

What about overriding the OnLoad method, which calls the Load event?

If that doesn't work, it's a bit sloppy, but you could use the TextChanged event, or the SizeChanged event as these are both called after the Name property is set by the designer (it sets the values in alphabetical order).

查看更多
登录 后发表回答