C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 155 を表示中)
■90171 / )  Re[1]: カスタムコントロールのデフォルト値を設定するには?
□投稿者/ 魔界の仮面弁士 (2051回)-(2019/02/13(Wed) 21:59:56)
No90167 (じきじき さん) に返信
> カスタムコントロールを新規作成した時に
> 最初に設定されるデフォルト値を設定したいのですが、
> どうしたら良いですか?

上記がデザイナー依存の話と仮定して、カスタムコントロールの Text プロパティに、
「フォーム名.コントロール名」を初期値として与えてみる例。


<System.ComponentModel.Designer(GetType(CustomControl.CustomControlDesigner))>
Partial Public Class CustomControl
 Inherits System.Windows.Forms.Control

 Public Sub New()
  InitializeComponent()
 End Sub

 Protected Overrides Sub OnPaint(e As PaintEventArgs)
  MyBase.OnPaint(e)
  e.Graphics.DrawString(Me.Text, Me.Font, Brushes.Black, PointF.Empty)
 End Sub

 Protected Overrides Sub OnControlAdded(e As ControlEventArgs)
  MyBase.OnControlAdded(e)
 End Sub

 Friend Class CustomControlDesigner
  Inherits System.Windows.Forms.Design.ControlDesigner
  Public Overrides Sub InitializeNewComponent(ByVal defaultValues As System.Collections.IDictionary)
   MyBase.InitializeNewComponent(defaultValues)
   Dim f As Form = Me.Control.FindForm()
   If f IsNot Nothing Then
    Me.Control.Text = f.Name & "." & Me.Control.Name
   Else
    Me.Control.Text = Me.Control.Name
   End If
  End Sub
 End Class
End Class
返信 編集キー/


管理者用

- Child Tree -