我正在制作一个自定义按钮控件,并且在处理我的文本属性时遇到一些困难。我键入的任何内容仅在窗体设计器窗口打开时保留。当我关闭窗体设计器并重新打开它时,我的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我仔细梳理了我的代码,并确保我没有在任何地方重置它。
提前感谢你在这方面的帮助。
发布于 2011-09-07 20:46:14
添加一个属性似乎是可行的:
<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发布于 2011-05-26 09:33:06
我曾经遇到过这个问题。只要删除Shadows关键字即可。我不知道Override是否可以在那里工作,但如果不能,就忽略关于Text和Image属性的VS警告。
编辑:我不知道为什么Overrides关键字没有成功。只有Image属性强制我改用Overloads。下面是我的代码:
Imports System.ComponentModel公共类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结束类
https://stackoverflow.com/questions/6131573
复制相似问题