【发布时间】:2019-06-17 13:25:42
【问题描述】:
这是我继承的 Button 控件的代码:
Public Class ButtonRefreshSmall
Inherits Button
Public Sub New()
MyBase.New
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.SuspendLayout()
Me.Text = ""
MyBase.Text = ""
Me.ResumeLayout()
End Sub
End Class
但是,当我重建此按钮并将其拖动到表单时,文本始终为ButtonRefreshSmall1。我尝试了没有Inherits 声明的变体(因为它已经在.Designer.vb 文件中,我尝试在控件的设计器视图/类中设置Text,但无济于事。
有时重建后它甚至不会显示在工具箱中。
我只想让按钮的文本为空(因为它在设计器中定义了Image)。
这是我在 Designer 文件中的内容:
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.SuspendLayout()
'
'ButtonRefreshSmall
'
Me.BackColor = System.Drawing.Color.Transparent
Me.FlatAppearance.BorderSize = 0
Me.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.Image = Global.TraxCashFlow.My.Resources.Resources.Refresh_grey_16x
Me.Size = New System.Drawing.Size(23, 23)
Me.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText
Me.UseVisualStyleBackColor = False
'MyBase.Text = ""
Me.ResumeLayout(False)
End Sub
所有其他属性都像我设置的那样设置。我试图用TextImageRelation 欺骗它,但无论如何“B”总是可见的。
更新:Jimi 在他的答案下方的评论中给了我这个想法,所以我添加了一个新的PropertyMyText,这就像我想要的那样工作(不知道为什么我需要打电话给Refresh,如果我不要在失去焦点后更新):
Imports System.ComponentModel
Public Class ButtonRefreshSmall
Public Property MyText As String
Get
Return Me.Text
End Get
Set(value As String)
Me.Text = value
Me.Refresh()
End Set
End Property
Public Sub New()
'MyBase.New
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
'Me.Text = ""
End Sub
<Browsable(False)>
<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
Public Overrides Property Text As String
End Class
更新 #2,请参阅@TnTinMn 的回答。
【问题讨论】:
-
拖动按钮后自己删除文本可能会更简单。
标签: vb.net winforms custom-controls windows-forms-designer