次のように、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
が 1 つだけありますGroupBox
myGroupBox
private System.Windows.Forms.GroupBox myGroupBox;
C# Windows フォーム アプリケーションであるテスト プログラムがありmyGroupBox
、プロパティ ウィンドウでの名前を変更するmyGroupBox
と、入力した名前が のテキストになることを確認したいと思います。プロパティウィンドウに名前を入力するValue
とmyGroupBox
、デザイナーでテキストが変更されます。スクリーンショットは次のとおりです。
しかし、テスト プログラムを再構築するか、実行時にテキストmyGroupBox
が消えると、スクリーンショットは次のようになります。
コードをどうすればいいですか?ありがとう