如何创建水印TextBox?

时间:2017-02-10 10:46:04

标签: vb.net winforms textbox watermark

我在Watermark TextBox in WinForms中找到了代码,但它是C#版本,所以我使用http://converter.telerik.com/将代码转换为VB版本,但我仍然遇到错误......

此行发生错误: -

SendMessage(Me.Handle,& H1501,DirectCast(1,IntPtr),mCue)

我该如何解决?

  

错误消息:BC30311类型值'整数'无法转换为' IntPtr'。

Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Class CueTextBox
    Inherits TextBox
    <Localizable(True)> _
    Public Property Cue() As String
        Get
            Return mCue
        End Get
        Set
            mCue = value
            updateCue()
        End Set
    End Property

    Private Sub updateCue()
        If Me.IsHandleCreated AndAlso mCue IsNot Nothing Then
            SendMessage(Me.Handle, &H1501, DirectCast(1, IntPtr), mCue)   'this line get the error msg
        End If
    End Sub
    Protected Overrides Sub OnHandleCreated(e As EventArgs)
        MyBase.OnHandleCreated(e)
        updateCue()
    End Sub
    Private mCue As String

    ' PInvoke
    <DllImport("user32.dll", CharSet := CharSet.Unicode)> _
    Private Shared Function SendMessage(hWnd As IntPtr, msg As Integer, wp As IntPtr, lp As String) As IntPtr
    End Function
End Class

1 个答案:

答案 0 :(得分:1)

根据PInvoke.net,VB.Net中SendMessage的正确签名是:

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function

无论如何,问题在于这一行:

SendMessage(Me.Handle, &H1501, DirectCast(1, IntPtr), mCue)  

尝试通过以下方式替换它:

SendMessage(Me.Handle, &H1501, New IntPtr(1)), mCue)