首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >自定义控件中的文本属性丢失值

自定义控件中的文本属性丢失值
EN

Stack Overflow用户
提问于 2011-05-26 06:08:17
回答 2查看 2.9K关注 0票数 3

我正在制作一个自定义按钮控件,并且在处理我的文本属性时遇到一些困难。我键入的任何内容仅在窗体设计器窗口打开时保留。当我关闭窗体设计器并重新打开它时,我的Text属性重置为"“。另外,如果我运行该程序,它将丢失在设计时输入的值。

我的控件也有一个Image属性,它工作得很好。

下面是我的一些代码:

代码语言:javascript
复制
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

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

提前感谢你在这方面的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-07 20:46:14

添加一个属性似乎是可行的:

代码语言:javascript
复制
<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
票数 1
EN

Stack Overflow用户

发布于 2011-05-26 09:33:06

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

编辑:我不知道为什么Overrides关键字没有成功。只有Image属性强制我改用Overloads。下面是我的代码:

代码语言:javascript
复制
Imports System.ComponentModel

公共类UserControl1

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6131573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档