首页 > 解决方案 > Vb.net在面板内的自定义文本上添加占位符

问题描述

我创建了一个继承面板的类,然后用Textbox. 然后我尝试Textbox使用以下代码添加水印/占位符:

Protected Overrides Sub WndProc(ByRef m As Message)
    MyBase.WndProc(m)
    Dim sTxt As String = Me.Text
    sTxt = IIf(sTxt.Length = 0, txt.Text, sTxt)
    If m.Msg = &HF Then
        If Not Me.Focused AndAlso String.IsNullOrEmpty(sTxt) AndAlso Not String.IsNullOrEmpty(Me.WaterMark) Then
            Using g = Me.CreateGraphics()
                TextRenderer.DrawText(g, Me.WaterMark, Me.Font, Me.ClientRectangle, Color.Red, Me.BackColor, TextFormatFlags.Top Or TextFormatFlags.Left)
            End Using
        End If
        MyBase.Update()
    End If
End Sub

我只尝试了该代码Textbox并且WaterMark确实出现了,但是当我尝试在我的自定义文本上使用它时,它WaterMark永远不会出现。我认为问题是因为我在面板内WndProc添加了一个,所以永远不会触发事件。Textbox那么我怎样才能覆盖Panel_WndProcto Txt_WndProc

标签: .netvb.netvisual-studio-2015

解决方案


推荐阅读