.net 如何更改自定义控件的默认大小
我不熟悉在VB.NET中添加自定义控件。 我想要一个类似PictureBox的控件,该控件具有默认大小和图片,两者最好都不可更改。.net 如何更改自定义控件的默认大小,.net,vb.net,winforms,controls,custom-controls,.net,Vb.net,Winforms,Controls,Custom Controls,我不熟悉在VB.NET中添加自定义控件。 我想要一个类似PictureBox的控件,该控件具有默认大小和图片,两者最好都不可更改。 我首先向项目中添加了一个新类,然后添加了以下代码: Public Class CustomControl Inherits Windows.Forms.PictureBox Protected Overrides Sub OnCreateControl() MyBase.OnCreateControl() Me.Image
我首先向项目中添加了一个新类,然后添加了以下代码:
Public Class CustomControl
Inherits Windows.Forms.PictureBox
Protected Overrides Sub OnCreateControl()
MyBase.OnCreateControl()
Me.Image = Global.Mazerino.My.Resources.Resources.ControlImage
MyBase.Size = New System.Drawing.Size(20, 20) 'Also tried setting Width and Height
'properties instead.
End Sub
End Class
我执行项目,关闭,然后添加控件;图像已添加,但大小未更改。默认控件的大小为150,50
因此,我添加了以下代码:
Private ControlSize As Size = New Size(10, 10)
Overloads Property Size As Size
Get
Return ControlSize
End Get
Set(value As Size)
'Nothing here...
End Set
End Property
但它也不起作用,所以我试着:
Shadows ReadOnly Property Size As Size
Get
Return ControlSize
End Get
End Property
在将控件添加到窗体时,它起作用,但当我执行该程序时,我得到以下错误:“属性大小仅为只读”。双击时,表单设计中会出现以下代码:
Me.CustomControl1.Size = New System.Drawing.Size(10, 10)
这导致我将属性更改为读写,但当我再次这样做时,控件大小保持在150,50
那么,如何将默认大小设置为特定大小,而不在将控件添加到表单时遇到问题呢
Public Class CustomControl : Inherits Windows.Forms.PictureBox
Private ReadOnly INMUTABLE_SIZE As Size = New Size(20, 20)
Public Shadows Property Size As Size
Get
Return INMUTABLE_SIZE
End Get
Set(value As Size)
MyBase.Size = INMUTABLE_SIZE
End Set
End Property
Protected Overrides Sub OnSizeChanged(e As System.EventArgs)
MyBase.Size = INMUTABLE_SIZE
MyBase.OnSizeChanged(e)
End Sub
End Class
您是否尝试过设置最小和最大尺寸
Public Class CustomControl Inherits Windows.Forms.PictureBox
Protected Overrides Sub OnCreateControl()
MyBase.OnCreateControl()
MyBase.SizeMode = PictureBoxSizeMode.StretchImage
Me.Image = Global.Mazerino.My.Resources.Resources.ControlImage
MyBase.Size = New System.Drawing.Size(20, 20) 'Also tried setting Width and Height
'properties instead.
MyBase.MaximumSize = New Size(20,20)
MyBase.MinimumSize = New Size(20,20)
End Sub
End Class