2011-05-25 33 views
3

我正在制作自定义按钮控件,并且在Text属性中遇到了一些困难。只有在窗体设计器窗口打开时,我所输入的内容才会保留。当我关闭表单设计器并重新打开它时,我的Text属性重置为“”。另外如果我运行该程序,它会丢失在设计时输入的值。自定义控件中的文本属性丢失值

我也有我的控制的图像属性工作得很好。

下面是我的一些代码:

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

为什么数据停留在窗体关闭后放? – soandos 2011-05-25 22:11:45

+0

这是一个按钮控件。在设计时为按钮输入标题时,在运行程序或关闭时应该仍然存在,然后重新打开表单设计器。我的控制不是这样做的。 – 2011-05-26 13:41:27

回答

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 
+0

是的,这是终于为我工作的;我错过了DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)部分。 – 2013-01-08 23:30:47

1

我曾经遇到过这个问题。只要删除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 

末级

+0

好吧,我已经尝试删除这样的Shadows关键字:'公共属性文本()作为字符串'没有运气那里。然后我用Overrides关键字试了一下。仍然没有运气。 – 2011-05-26 13:51:31

+0

请稍候。我遇到了那个并解决了它。我会检查我的旧项目并尽快编辑我的答案(并在此后留下评论)。编辑1:并且,请告诉我你正在使用的.NET版本。我听说我论坛的一些成员也对此感到不幸。编辑2:并且,你从这个类继承了什么?控制或按钮? – 2011-05-26 15:20:58

+0

编辑答案。希望它会对你好! – 2011-05-26 15:30:52