VB.NET文本框提示横幅没有设置 - 没有错误

时间:2015-03-12 16:56:49

标签: vb.net winforms textbox watermark

(我删除了旧部件,因为它是多余的)

编辑 -

即使不了解C#,我也试图将Lars指向我的代码转换为...管理以进行编译。并且可以从工具箱中使用。但是,它似乎不是 CueTextBox1.Cue =“Test”。同样,似乎没有产生任何错误。我已经检查过并且cue属性已添加到CueTextBox的属性中,但是更改它不会改变提示,或者看似对此有所作为。这是转换后的代码:

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

Class CueTextBox
    Inherits TextBox
    Public Property Cue() As String
        Get
            Return mCue
        End Get
        Set(value As String)
            mCue = value
            updateCue()
        End Set
    End Property

    Private Sub updateCue()
        If (Me.IsHandleCreated And mCue = Nothing) Then
            SendMessage(Me.Handle, &H1501, New IntPtr(1), mCue)
        End If
    End Sub

    Protected Overrides Sub OnHandleCreated(e As EventArgs)
        MyBase.OnHandleCreated(e)
        updateCue()
    End Sub

    Private mCue As String

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

正如Plutonix建议的那样,我已经改变了PInvoke的最后一个参数。可悲的是,没有任何变化。下面是更新的块。

    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, <MarshalAs(UnmanagedType.LPWStr)> LParm As String) As IntPtr
    End Function

EDIT2 -

仍然遇到标题中给出的相同问题。任何帮助将不胜感激。 cue在设计视图中工作,但是在编译时,在某些项目中,cue无法设置。

1 个答案:

答案 0 :(得分:0)

非常感谢@Plutonix。固定和完全工作的代码如下。 问题出在updateCue()中,主要是 mCue = Nothing 。将其更改为 String.IsNullOrEmpty(mCue)= False 可修复此问题。但正如@Plutonix指出的那样,删除检查将允许传递空字符串。

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

Class CueTextBox
    Inherits TextBox
    Public Property Cue() As String
        Get
           Return mCue
        End Get
        Set(value As String)
            mCue = value
            updateCue()
        End Set
    End Property

    Private Sub updateCue()  
        If (Me.IsHandleCreated) Then
            SendMessage(Me.Handle, &H1501, New IntPtr(1), mCue)
        End If
    End Sub

    Protected Overrides Sub OnHandleCreated(e As EventArgs)
        MyBase.OnHandleCreated(e)
        updateCue()
    End Sub

    Private mCue As String

    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, <MarshalAs(UnmanagedType.LPWStr)> LParm As String) As IntPtr
    End Function
End Class

编辑 - 这半部作品。出于某种原因,提示仍然不会在运行时出现在某些项目上。让我相信这些项目存在问题。关于什么,我不知道。