Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

GDI - Textbox with Cue Text

Hey,

I am coding a project atm and needed a textbox with a Cue Text function. I designed a little class, which does the job nearly perfect I think :)

The search just showed up an crappy example, so I decided to share it with HF.

PHP Code

<?php 
Imports System
.Runtime.InteropServices

Public Class CueTextbox
    Inherits TextBox

    
<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

    Private 
_CueText As String
    
Public Property CueText As String
      Get
      
Return _CueText
      End Get
      Set
(value As String)
      
SendMessage(Me.Handle, &H1501, 0, value)
      
_CueText = value
      End Set
    End Property

End 
Class 

Its nothing special, but it works great 👍
You can change the watermark in the Visual Studio designer itself or do it like this:

PHP Code

<?php 
WatermarkedTextbox
.CueText = "Username..." 

Will look like this:
(Thanks to: StreetLight.)

[Image: WHSmWcb.gif]

Peace 🤝
[Image: xAhLMBg.png]
Hello, could you show us a screenshot of the textbox please ? thank you. :)
^ Agreed please show us the textbox with some images please.
hi
sorry but textbox.text is similar
I made a gif for OP.
[Image: WHSmWcb.gif]
without api

Code
Class WMTextBox
    Inherits TextBox

    Private WaterMark As String = "Water Mark"
    Sub New()
      ForeColor = Color.LightGray
      Text = WaterMark
      AddHandler Me.Leave, AddressOf Me_Leave
      AddHandler Me.Enter, AddressOf Me_Enter
    End Sub

    Private Sub Me_Leave(sender As Object, e As EventArgs)
      If Text.Length = 0 Then
      Text = WaterMark
      ForeColor = Color.LightGray
      End If
    End Sub
    Private Sub Me_Enter(sender As Object, e As EventArgs)
      If Text = WaterMark Then
      Text = ""
      ForeColor = Color.Black
      End If
    End Sub
End Class
Not sure but isn't this called a placeholder/hint? A watermark would refer to the 'created with gifrecorder.com' in the gif. Other than that, it's still a nice snippet.
[Image: IQE6i8G.png]

BlackCap Wrote:You're even more verbose than Java.
About using GoTo's in VB/C#:
Only3 Wrote:asm use it why u shouldnt
(Feb 2, 2015 02:37 PM)Only3 Wrote:  without api

Code
Class WMTextBox
    Inherits TextBox

    Private WaterMark As String = "Water Mark"
    Sub New()
      ForeColor = Color.LightGray
      Text = WaterMark
      AddHandler Me.Leave, AddressOf Me_Leave
      AddHandler Me.Enter, AddressOf Me_Enter
    End Sub

    Private Sub Me_Leave(sender As Object, e As EventArgs)
      If Text.Length = 0 Then
      Text = WaterMark
      ForeColor = Color.LightGray
      End If
    End Sub
    Private Sub Me_Enter(sender As Object, e As EventArgs)
      If Text = WaterMark Then
      Text = ""
      ForeColor = Color.Black
      End If
    End Sub
End Class

I prefer this method more than the original. I just like to avoid API calls.
Thanks for this.
Instead of watermark, should be more of a "hint" textbox.
this is actually called "Cue Text".
google it, its been around for ages.
OP: Thanks for sharing this...although I don't need it & Nathan: Thanks for that info "Cue Text" - I actually never heard of that before ;)
(Feb 2, 2015 02:27 PM)Streetlight. Wrote:  I made a gif for OP.
[Image: WHSmWcb.gif]

Thanks awesome 🤝🎉

(Feb 2, 2015 02:37 PM)Only3 Wrote:  without api

Code
Class WMTextBox
    Inherits TextBox

    Private WaterMark As String = "Water Mark"
    Sub New()
      ForeColor = Color.LightGray
      Text = WaterMark
      AddHandler Me.Leave, AddressOf Me_Leave
      AddHandler Me.Enter, AddressOf Me_Enter
    End Sub

    Private Sub Me_Leave(sender As Object, e As EventArgs)
      If Text.Length = 0 Then
      Text = WaterMark
      ForeColor = Color.LightGray
      End If
    End Sub
    Private Sub Me_Enter(sender As Object, e As EventArgs)
      If Text = WaterMark Then
      Text = ""
      ForeColor = Color.Black
      End If
    End Sub
End Class

And what if I write the Watermark string itself in the textbox ? This code is buggy, sorry...

(Feb 2, 2015 04:22 PM)Hawk HF Wrote:  I prefer this method more than the original. I just like to avoid API calls.
Thanks for this.

Why wouldnt you use an API call ? Mh. But use what you like :)

(Feb 2, 2015 06:33 PM)nathan72389 Wrote:  this is actually called "Cue Text".
google it, its been around for ages.

Going to add this to the title 🎉 Thanks

Peace 🤝
[Image: xAhLMBg.png]
(Feb 3, 2015 11:49 AM)BerryP Wrote:  And what if I write the Watermark string itself in the textbox ? This code is buggy, sorry...

🤦‍♂ change it to Public

Code
Class WMTextBox
    Inherits TextBox

    Public WaterMark As String = "Water Mark"
    Sub New()
      ForeColor = Color.LightGray
      Text = WaterMark
      AddHandler Me.Leave, AddressOf Me_Leave
      AddHandler Me.Enter, AddressOf Me_Enter
    End Sub

    Private Sub Me_Leave(sender As Object, e As EventArgs)
      If Text.Length = 0 Then
      Text = WaterMark
      ForeColor = Color.LightGray
      End If
    End Sub
    Private Sub Me_Enter(sender As Object, e As EventArgs)
      If Text = WaterMark Then
      Text = ""
      ForeColor = Color.Black
      End If
    End Sub
End Class
(Feb 3, 2015 11:59 AM)Only3 Wrote:  🤦‍♂ change it to Public

Doesnt work 🤝
And why would this change anything ? 😊
You have to work with MyBase and Me, to tell the difference... This is to much work. Just use the API...

AND what do you do if you want to get the real text ? You will get the Watermark if the text is nothing...

[img]http://www11.pic-upload.de/03.02.15/mcx9qzchaoj5.gif[/img]

Peace 🤝
[Image: xAhLMBg.png]
(Feb 3, 2015 01:01 PM)BerryP Wrote:  This is to much work. Just use the API...
Made this real quick, probably the best cue text so far.
I left some bugs in it for anti-leech :)

Code
Public Class YetAnotherCueText : Inherits Control
    Private watermark_select As Boolean = True, _watermark As String = "WaterMark"
    Sub New()

      AddHandler Me.MouseLeave, Sub()
            watermark_select = IIf(Me.Text.Length = 0, True, False) : Me.Invalidate()
            End Sub
      AddHandler Me.MouseEnter, Sub()
            watermark_select = False
            End Sub
      AddHandler Me.KeyUp, Sub(f As Object, s As KeyEventArgs)
             Me.Text += Chr(s.KeyValue): Me.Invalidate()
             End Sub

      AddHandler Me.Paint, Sub(se As Object, ev As PaintEventArgs)
             ev.Graphics.FillRectangle(New SolidBrush(Color.White), New Rectangle(0, 0, Me.Width - 1, Me.Height - 1)): ev.Graphics.DrawRectangle(Pens.Black, New Rectangle(0, 0, Me.Width - 1, Me.Height - 1)):  ev.Graphics.DrawString(IIf(watermark_select = True, Me._watermark, Me.Text), Me.Font, IIf(watermark_select = True, Brushes.Gray, Brushes.Black), New Point(0, Me.FontHeight / 2))
             End Sub
    End Sub
    Public Property WaterMark As String
      Get
      Return _watermark
      End Get
      Set(value As String)
      _watermark = value : Me.Invalidate()
      End Set
    End Property  
End Class


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by ๖ۣۜMephobiaNov 14, 2025 07:54 PM 171 21,476 Nov 14, 2025 07:54 PM
Last Post: ProxyLand
[.NET] by QuobiAug 6, 2022 02:23 PM 0 407 Aug 6, 2022 02:23 PM
Last Post: Quobi
[.NET] by PolarisGangJun 21, 2022 10:09 AM 1 353 Jun 21, 2022 10:09 AM
Last Post: gigajew