我正在制作自定义按钮控件,并且我的Text属性有些困难。我输入的任何内容仅在表单设计器窗口打开时保留。当我关闭表单设计器并重新打开它时,我的Text属性重置为“”。此外,如果我运行该程序,它将丢失在设计时输入的值。
我的控件也有一个Image属性,工作得很好。
以下是我的一些代码:
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Windows.Forms
Imports System.ComponentModel
Public Class BlackButton
Private iText As String
Private iImage As Image
''' <summary>
''' Gets/Sets the text displayed in the button.
''' </summary>
<Browsable(True), Description("Gets or sets the text displayed on the button")> _
Public Shadows Property Text() As String
Get
Return iText
End Get
Set(ByVal value As String)
iText = value
ReDrawMe()
End Set
End Property
''' <summary>
''' Gets/Sets the image to be displayed on the button
''' </summary>
<Browsable(True), Description("Gets or sets the image displayed on the button")> _
Public Shadows Property Image() As Image
Get
Return iImage
End Get
Set(ByVal value As Image)
iImage = value
ReDrawMe()
End Set
End Property
我仔细梳理了我的代码并确保我没有在任何地方重置它。
提前感谢您提供任何帮助。
答案 0 :(得分:1)
我曾经遇到过这个问题。只需删除Shadows关键字即可。我不知道Override是否可以在那里工作,但如果没有,只需忽略有关文本和图像属性的VS警告。
编辑:我不知道为什么你没有成功使用Overrides
关键字。只有Image
属性强迫我使用Overloads
。这是我的代码:
Imports System.ComponentModel
Public Class UserControl1
Dim _Text As String
Dim _Image As Image
<Browsable(True), Description("Gets or sets the text displayed on the button")> _
Overrides Property Text() As String
Get
Return _Text
End Get
Set(ByVal value As String)
_Text = value
'This line just for update
'the UI when I design to check
'if the values are saved.
MyBase.Text = value
End Set
End Property
<Browsable(True), Description("Gets or sets the image displayed on the button")> _
Overloads Property Image() As Image
Get
Return _Image
End Get
Set(ByVal value As Image)
_Image = value
'ReDrawMe()
End Set
End Property
结束班
答案 1 :(得分:1)
似乎可以添加属性:
<Browsable(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
Public Overrides Property Text() As String
Get
Return MyBase.Text
End Get
Set(ByVal value As String)
MyBase.Text = value
LabInfo.Text = MyBase.Text
End Set
End Property