1

我第一次尝试编写自定义控件。本质上,我正在使用继承的面板创建一组“图标”,用户可以单击该面板打开某些表单。该类包含一个图片框的实例和两个标签:

在此处输入图像描述

但是,我发现我需要能够在字符串描述中使用类似于“VbCrLF”的内容,以便在输入项目时将文本附加到下一行,而不是让描述继续运行。我想做的是在输入新的 txtDescription 或 txtHeaderDescription 时,允许我按 Enter 并让它为我插入回车。标签本身就是这样做的:

使用 vbCRLF 显示标签

我的问题是如何在 VS 中修改我的类以显示该类型的输入框,而不是典型的“行”条目?

我的课是:

Public Class WorkspaceIconControl
Inherits Panel
Dim pbIcon As New PictureBox
Dim lblHeader As New Label, lblDescrip As New Label
Public Sub New()

    'MODIFY THE BACKCOLOR
    Me.BackColor = Color.SteelBlue
    Me.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch
    Me.Width = 250
    Me.Height = 100
    Me.BorderStyle = BorderStyle.FixedSingle

    'ADD PICTUREBOX
    pbIcon.BackColor = System.Drawing.Color.Transparent
    pbIcon.Image = My.Resources.addressIcon2
    pbIcon.Dock = DockStyle.None
    pbIcon.Width = 96
    pbIcon.Height = 96
    pbIcon.Top = 5
    pbIcon.SizeMode = PictureBoxSizeMode.StretchImage
    Me.Controls.Add(pbIcon)


    'ADD HEADER
    lblHeader.Font = New System.Drawing.Font("Calibri", 12.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    lblHeader.Name = "lblHeader"
    lblHeader.Text = "txtHeader"
    lblHeader.Top = 15
    lblHeader.Left = 95
    lblHeader.Width = 300
    lblHeader.ForeColor = Color.White
    Controls.Add(lblHeader)

    'ADD DESCRIP
    lblDescrip.Font = New System.Drawing.Font("Calibri", 7.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    lblDescrip.Name = "lblDescrip"
    lblDescrip.Text = "txtDescription"
    lblDescrip.Top = 35
    lblDescrip.Left = 95
    lblDescrip.Width = 300
    lblDescrip.ForeColor = Color.White
    Me.Controls.Add(lblDescrip)


End Sub

<Description("The image associated with the picturebox of this control"), _
Category("Appearance")> _
Public Property MyImage() As Image

    Get
        Dim image1 As Image
        image1 = pbIcon.Image
        Return image1
    End Get
    Set(ByVal imgValue As Image)
        pbIcon.Image = imgValue
    End Set
End Property

<Description("The width of picturebox associated with the control"), _
Category("Appearance")> _
Public Property PictureBoxWidth() As Double

    Get
        Dim width As Double
        width = pbIcon.Width
        Return width
    End Get
    Set(ByVal widthVal As Double)
        pbIcon.Width = widthVal
    End Set
End Property


<Description("The height of picturebox associated with the control"), _
Category("Appearance")> _
Public Property PictureBoxHeight() As Double

    Get
        Dim height As Double
        height = pbIcon.Height
        Return height
    End Get
    Set(ByVal height As Double)

        pbIcon.Height = height
    End Set
End Property



<Description("The label header visible property associated with the control"), _
Category("Appearance")> _
Public Property HeaderVisible() As Boolean

    Get
        Dim bHeader As Boolean
        bHeader = lblHeader.Visible
        Return bHeader

    End Get
    Set(ByVal bHeader As Boolean)
        lblHeader.Visible = bHeader
    End Set
End Property


<Description("The label description text property associated with the control"), _
Category("Appearance")> _
Public Property txtDescription() As String

    Get
        Dim strText As String
        strText = lblDescrip.Text
        Return strText
    End Get
    Set(ByVal strText As String)
        lblDescrip.Text = strText
    End Set
End Property

<Description("The label description header text property associated with the control"), _
Category("Appearance")> _
Public Property txtHeaderDescription() As String

    Get
        Dim strText As String
        strText = lblHeader.Text
        Return strText
    End Get
    Set(ByVal strText As String)
        lblHeader.Text = strText
    End Set
End Property

End Class
4

1 回答 1

1

您可以查看 Label 控件的源代码,并通过向属性添加 UI Editor 属性来了解它是如何做到的:

<Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", GetType(UITypeEditor))> _
Property Text As String
于 2013-11-11T16:37:20.670 回答