【问题标题】:Text property in custom Control loses value自定义控件中的文本属性失去价值
【发布时间】:2011-05-25 22:08:17
【问题描述】:

我正在制作自定义按钮控件,但我的 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

我仔细梳理了我的代码,并确保我没有在任何地方重置它。

在此先感谢您提供任何帮助。

【问题讨论】:

  • 为什么表格关闭后数据仍然保留?
  • 这是一个按钮控件。当您在设计时为按钮输入标题时,当您运行程序或关闭然后重新打开表单设计器时,它应该仍然存在。我的控件没有这样做。

标签: vb.net controls properties


【解决方案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

【讨论】:

  • 是的,这最终对我有用;我错过了 DesignerSerializationVisibility(DesignerSerializationVisibility.Visible) 部分。
【解决方案2】:

我曾经遇到过这个问题。只需删除 Shadows 关键字即可。我不知道 Override 是否可以在那里工作,但如果不能,请忽略 VS 关于 Text 和 Image 属性的警告。

编辑:我不知道你为什么没有成功使用 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

结束类

【讨论】:

  • 好的,我试过像这样删除 Shadows 关键字:'Public Property Text() As String' 没有运气。然后我用 Overrides 关键字尝试了它。还是没有运气。
  • 请稍候。我遇到了那个并解决了它。我将检查我的旧项目并尽快编辑我的答案(然后发表评论)。 EDIT1:请告诉我您正在使用的 .NET 版本。我听说我论坛中的一些成员对此也有不好的运气。 EDIT2:而且,你从什么继承这个类?控件还是按钮?
  • 终于通过将我的属性重命名为 TextShown 来解决它。不是最优雅的,但似乎没有其他东西对我有用。 @W.N. - 它基于 Control。
猜你喜欢
  • 1970-01-01
  • 2013-02-04
  • 2012-01-11
  • 2011-08-19
  • 1970-01-01
  • 2012-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多