0

I developed a composite control using C# Windows Forms Control Library and code as follow:

public partial class MyControl : UserControl
{
    public event EventHandler NameChanged;
    protected virtual void OnNameChanged()
    {
        EventHandler handler = NameChanged;
        if (handler != null) handler(this, EventArgs.Empty);
    }

    private void WhenNameChanged(object sender , EventArgs e)
    {
        this.myGroupBox.Text = this.Name;
    }

    protected override void OnCreateControl()
    {
        base.OnCreateControl();

        IComponentChangeService changeService = (IComponentChangeService)GetService(typeof(IComponentChangeService));
        if (changeService == null) return; // not provided at runtime, only design mode

        changeService.ComponentChanged -= OnComponentChanged; // to avoid multiple subscriptions
        changeService.ComponentChanged += OnComponentChanged;
    }

    private void OnComponentChanged(object sender, ComponentChangedEventArgs e)
    {
        if(e.Component == this && e.Member.Name == "Name")
        {
            OnNameChanged();
        }
    }

    public MyControl()
    {
        InitializeComponent();
        this.NameChanged += new EventHandler(this.WhenNameChanged);
    }
}

The MyControl only has one GroupBox control named myGroupBox

private System.Windows.Forms.GroupBox myGroupBox;

I have a test program which is a C# Windows Forms Application, and I would like to see that when I change the name of myGroupBox in properties window, the text of myGroupBox will be the name I typed. so I typed a name Value to myGroupBox in the properties window, the text will change in designer, here is the screenshot: enter image description here

But when I rebuild the test program or at run time the text of myGroupBox will disappear, here is the screenshot:

enter image description here

what should I do with my code? thanks

TwenteMaster
  • 148
  • 2
  • 12
  • Can you try to debug this? (like a messagebox in `WhenNameChanged`). When you set the `Name` of `myGroupBox` you are setting a property of a `GroupBox`, not of a `MyControl`, so `WhenNameChanged` is not part here. `GroupBox` sets it's `Text` to it's `Name` when no other `Text` was specified. So it would be interesting when exactly that `Text` is reset. – René Vogt Jan 21 '16 at 14:01
  • I debugged, and found out that It's the constructor `MyControl` to reset the `Text` to an empty string, at the construction time, the `Name` is empty – TwenteMaster Jan 21 '16 at 14:10
  • I have no idea how to solve this – TwenteMaster Jan 21 '16 at 14:13
  • update my previous answer – René Vogt Jan 21 '16 at 14:37

1 Answers1

1

So the problem is that the Name of a Control (or UserControl) is empty after construction. The name given by you and stored in the resources will be set in OnLoad.
So a solution for you could be to override OnLoad with something like this:

public class MyControl : UserControl
{
    // ... the code so far

    // override OnLoad
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        myGroupBox.Text = Name; // or WhenNameChanged(this, EventArgs.Empty);
    }
}

So the name taken from the resources (e.g. after rebuild/reopen designer) will be set here again and so also set as myGroupBox.Text.

Hope that helps.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • I tried, but failed, I call `WhenNameChanged` like this:`WhenNameChanged(this , EventArgs.Empty);` – TwenteMaster Jan 21 '16 at 13:38
  • Yes, I called it like you did – TwenteMaster Jan 21 '16 at 13:41
  • I tried just now, it works fine, thank you very much again! – TwenteMaster Jan 22 '16 at 01:52
  • I have another problem, I create a new user control inherited from `MyControl` named `DerivedControl`, and in the test program in the design mode the text of `GroupBox` showed a wrong text, but when the test program running, the text of `GroupBox` showed correctly, why? – TwenteMaster Jan 22 '16 at 03:41
  • @TwenteMaster Sorry, can't tell you from that short description. We will not solve this in the comments...maybe open a new question? – René Vogt Jan 22 '16 at 09:19