我使用C#Windows窗体控件库和代码开发了一个复合控件,如下所示:
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); } }MyControl只有一个名为myGroupBox GroupBox控件
private System.Windows.Forms.GroupBox myGroupBox;我有一个测试程序,它是一个C#Windows窗体应用程序,我想看到当我在属性窗口中更改myGroupBox的名称时, myGroupBox的文本将是我输入的名称。 所以我在属性窗口中输入了一个名称Value to myGroupBox ,文本将在设计器中更改,这里是截图:
但是当我重建测试程序或在运行时myGroupBox的文本将消失时,屏幕截图如下:
我该怎么处理我的代码? 谢谢
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:
But when I rebuild the test program or at run time the text of myGroupBox will disappear, here is the screenshot:
what should I do with my code? thanks
最满意答案
所以问题是Control (或UserControl )的Name在构造之后是空的。 您提供并存储在资源中的名称将在OnLoad设置。 所以你的解决方案可能是用这样的方式覆盖OnLoad :
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); } }因此,从资源中获取的名称(例如,在重建/重新打开设计器之后)将再次设置在此处,因此也设置为myGroupBox.Text 。
希望有所帮助。
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.
更多推荐
myGroupBox,Windows,void,changeService,电脑培训,计算机培训,IT培训"/> <meta na
发布评论