C#Composite Control将在设计和运行时丢失名称(C# Composite Control will lose name at design and run time)

系统教程 行业动态 更新时间:2024-06-14 17:02:18
C#Composite Control将在设计和运行时丢失名称(C# Composite Control will lose name at design and run time)

我使用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

本文发布于:2023-04-21 18:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/8d87c6c17b33c7729c7f9b0faad9f2df.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:将在   名称   Control   Composite   time

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!