Hack Forums
[.NET] GDI - Textbox with Cue Text - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] GDI - Textbox with Cue Text (/showthread.php?tid=4667060)



GDI - Textbox with Cue Text - BerryP - Feb 2, 2015

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 🤝


* - Naywyn - Feb 2, 2015

Hello, could you show us a screenshot of the textbox please ? thank you. :)


* - SIM-1847363 - Feb 2, 2015

^ Agreed please show us the textbox with some images please.


* - gghitman - Feb 2, 2015

hi
sorry but textbox.text is similar


* - Streetlight. - Feb 2, 2015

I made a gif for OP.
[Image: WHSmWcb.gif]


* - Only3 - Feb 2, 2015

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



* - Temploit - Feb 2, 2015

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.


* - Hawk HF - Feb 2, 2015

(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.


* - Streetlight. - Feb 2, 2015

Instead of watermark, should be more of a "hint" textbox.


* - nathan72389 - Feb 2, 2015

this is actually called "Cue Text".
google it, its been around for ages.


* - nuk - Feb 2, 2015

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 ;)


* - BerryP - Feb 3, 2015

(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 🤝


* - Only3 - Feb 3, 2015

(Feb 3, 2015 11:49 AM)Maember 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



* - BerryP - Feb 3, 2015

(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 🤝


* - KingDeath360 - Feb 3, 2015

(Feb 3, 2015 01:01 PM)Maember 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