2014-02-18 26 views
1

我想知道如何爲表單中的兩個不同文本框創建「提示」?爲兩個不同的文本框(水印)創建一個文本框「提示」消息

這裏是我的代碼:

Imports System.Runtime.InteropServices

Form_Load事件:

SendMessage(Me.txtAmount.Handle, &H1501, 0, "$X.XX") 
SendMessage(Me.txtMemo.Handle, &H1501, 0, "Enter a transaction memo.") 

共享功能:

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

此代碼僅適用於txtAmount文本框。任何想法如何讓兩個工作在一個形式?

謝謝。

+4

我不認爲「線索旗幟」適用於多文本框。 – LarsTech

+0

@LarsTech,這是我的問題。我想我願意爲該功能轉向單行文本框。謝謝! – Sev09

+0

另外:謝謝,@valter無用的輸入。這是一個恥辱,你不能downvote評論。 – Sev09

回答

6

創建一個類:

Public Class MultilineTextBoxWaterMark 
Inherits TextBox 

Private Const WM_PAINT = &HF 

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) 
    MyBase.WndProc(m) 

    If m.Msg = WM_PAINT Then 
     If Text.Length <> 0 Or Me.Focused Then 
      Return 
     End If 
     Using g As Graphics = Me.CreateGraphics, format As New StringFormat() 
      format.LineAlignment = StringAlignment.Near 

      g.DrawString("Enter a transaction memo.", Me.Font, Brushes.LightGray, Me.ClientRectangle, format) 
     End Using 
    End If 
End Sub 

End Class 

然後在您的主要形式有:

Private MltLnTxtBxWrMrk As New MultilineTextBoxWaterMark 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    MltLnTxtBxWrMrk.Location = New Point(100, 30) 'whatever you want 
    MltLnTxtBxWrMrk.Width = 300 'whatever you want 
    MltLnTxtBxWrMrk.Height = 100 'whatever you want 
    MltLnTxtBxWrMrk.BorderStyle = BorderStyle.Fixed3D 'whatever you want 
    MltLnTxtBxWrMrk.Multiline = True 

    Me.Controls.Add(MltLnTxtBxWrMrk) 
End Sub 

,我認爲它的工作原理。謝謝

+0

工作就像一個魅力。謝謝!我其實很喜歡這種看起來很多的方式,我正在改變我的其他水印來模仿這個。 – Sev09

+0

我將如何將此代碼應用於其他文本框?這個類與特定的文本框有什麼聯繫?提前致謝。 – Sev09

+0

雖然這個其他文本框是單行文本框。這很重要嗎? – Sev09

1

您可以複製一個文本框,提示沒有API:

''' <summary> 
''' Indicates the Textbox Hint. 
''' </summary> 
Private ReadOnly TextBoxHint As String = "I'm a control hint." 

''' <summary> 
''' Handles the Hint event of the TextBox control. 
''' </summary> 
''' <param name="sender">The source of the event.</param> 
''' <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> 
Private Sub TextBox_Leave(ByVal sender As Object, ByVal e As EventArgs) Handles _ 
TextBox1.Leave, TextBox1.HandleCreated 

    Select Case CStr(sender.Text) 

     Case Is = TextBoxHint 
      sender.text = String.Empty 

     Case Is = String.Empty 
      sender.text = TextBoxHint 

    End Select 

End Sub 

''' <summary> 
''' Handles the Enter event of the TextBox control. 
''' </summary> 
''' <param name="sender">The source of the event.</param> 
''' <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> 
Private Sub TextBox_Enter(ByVal sender As Object, ByVal e As EventArgs) Handles _ 
TextBox1.Enter 

    Select Case CStr(sender.Text) 

     Case Is = TextBoxHint 
      sender.text = String.Empty 

    End Select 

End Sub 

不管怎麼說,試試這個選擇,我同時測試了各種文本框,工作不錯:

' Set Control Hint 
' (By Elektro) 
' 
' Usage Examples: 
' SetControlHint(TextBox1, "I'm a text hint.") 

''' <summary> 
''' Messages to send to an Edit control, such a TextBox. 
''' </summary> 
Private Enum EditControlMessages As Integer 

    ''' <summary> 
    ''' Sets the textual cue, or tip, that is displayed by the edit control to prompt the user for information. 
    ''' http://msdn.microsoft.com/en-us/library/windows/desktop/bb761639%28v=vs.85%29.aspx 
    ''' </summary> 
    SetCueBanner = &H1501I 

End Enum 

''' <summary> 
''' Sends the specified message to a window or windows. 
''' The SendMessage function calls the window procedure for the specified window 
''' and does not return until the window procedure has processed the message. 
''' http://msdn.microsoft.com/en-us/library/windows/desktop/ms644950%28v=vs.85%29.aspx 
''' </summary> 
''' <param name="hWnd"> 
''' A handle to the Control whose will receive the message. 
''' </param> 
''' <param name="msg"> 
''' The message to be sent. 
''' </param> 
''' <param name="wParam"> 
''' Additional message-specific information. 
''' </param> 
''' <param name="lParam"> 
''' Additional message-specific information. 
''' </param> 
''' <returns> 
''' The return value specifies the result of the message processing; it depends on the message sent. 
''' </returns> 
<System.Runtime.InteropServices.DllImport("user32.dll", 
EntryPoint:="SendMessage", 
CharSet:=System.Runtime.InteropServices.CharSet.Auto)> 
Private Shared Function SendEditMessage(
     ByVal hWnd As IntPtr, 
     ByVal msg As EditControlMessages, 
     ByVal wParam As IntPtr, 
     <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)> 
     ByVal lParam As String 
) As UInteger 
End Function 

''' <summary> 
''' Sets a text hint for an edit control such a TextBox. 
''' </summary> 
''' <param name="Control">Indicates the control.</param> 
''' <param name="Hint">Indicates the text hint.</param> 
''' <returns><c>true</c> if operation succeeds, <c>false</c> otherwise.</returns> 
Private Function SetControlHint(ByVal [Control] As Control, 
           ByVal Hint As String) As Boolean 

    Return SendEditMessage([Control].Handle, EditControlMessages.SetCueBanner, IntPtr.Zero, Hint) 

End Function 
0

剛剛碰到這個。 除了user3311691後,我建議增加幾行,以增強渲染文本的質量:

Using g As Graphics = Me.CreateGraphics, oFmt As New StringFormat() 
      oFmt.LineAlignment = StringAlignment.Near 
      g.Clear(SystemColors.Window) 
      g.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit 
      g.DrawString(CueBannerText, Me.Font, SystemBrushes.GrayText, Me.ClientRectangle, oFmt) 
     End Using