突出显示文本框

时间:2013-09-12 20:13:52

标签: vb.net textbox custom-controls border

我正在尝试在自定义文本框控件周围绘制突出显示的边框,以便我可以为我创建的每个新程序重用突出显示功能。到目前为止,我的方法是在设置了我创建的自定义属性后覆盖控件库(dll)中的paint事件。控件的代码如下。

Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Drawing
Imports System.ComponentModel.Design

<ToolboxBitmap(GetType(Button))>
Public Class Textbox_Custom
    Inherits System.Windows.Forms.TextBox

    Public Event OnEnterKeyPress()
    Public Event MissingInfo_Change As EventHandler
    Dim iMissing_Info As Boolean
    Dim iCharacterInput As Cinput

    Public Property CharacterInput As Cinput
        '<Browsable(True), DefaultValue("AllowAll")>
        Get
            Return Me.iCharacterInput
        End Get
        Set(ByVal value As Cinput)
            Me.iCharacterInput = value
        End Set
    End Property

    Public Property Missing_Info As Boolean
        '<Browsable(True), DefaultValue(True)>
        Get
            Return iMissing_Info
        End Get
        Set(value As Boolean)
            iMissing_Info = value
            **MyBase.Refresh()**

        End Set
    End Property

    Protected Overrides Sub OnKeyPress(e As KeyPressEventArgs)
        MyBase.OnKeyPress(e)

        If Asc(e.KeyChar) = 13 Then
            RaiseEvent OnEnterKeyPress()
        End If

        Select Case Me.iCharacterInput
            Case Cinput.CharactersOnly
                If IsNumeric(e.KeyChar) Then
                    e.Handled = True
                End If
            Case Cinput.NumericOnly
                If Not IsNumeric(e.KeyChar) And Asc(e.KeyChar) <> 8 Then
                    e.Handled = True
                End If
        End Select
    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        MyBase.OnPaint(e)

        **If iMissing_Info = True Then**
            Dim rect As New Rectangle(New Point(0, 0), New Size(Me.Size.Width + 2, Me.Size.Height + 2))
            Dim pen As New Pen(Brushes.OrangeRed, 2)

            e.Graphics.DrawRectangle(pen, rect)

            e.Dispose()
        End If
    End Sub

End Class

Public Enum Cinput
    AllowAll
    NumericOnly
    CharactersOnly
End Enum

在调试时我在OnPaint覆盖中设置了一个断点(行**),但它从未命中它。然后我在Missing_Info属性的Set部分中放置一个断点,我试图使控件无效以重​​绘。它确实击中了MyBase.Refresh断点,所以我不明白我错过了什么。

我意识到在这个主题上还有其他一些帖子,但据我所知,他们似乎需要在控件后面放置面板。我觉得我应该能够在自定义控件中包含此操作,而不必为每个新项目编写新的突出显示部分。感谢您提前提供任何帮助。

1 个答案:

答案 0 :(得分:0)

最后我决定将控制背景更改为半透明的红色,这对我正在做的事情应该足够明显。