Feb 2, 2015 10:19 AM
(This post was last modified: Feb 3, 2015 11:51 AM by BerryP.)
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.)
Peace 🤝
DO NOT DEAL WITH THIS MEMBER
Feb 2, 2015 10:52 AM
(This post was last modified: Feb 2, 2015 10:52 AM by Naywyn.)
Hello, could you show us a screenshot of the textbox please ? thank you. :)
DO NOT DEAL WITH THIS MEMBER
^ Agreed please show us the textbox with some images please.
hi
sorry but textbox.text is similar
I made a gif for OP.
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
Feb 2, 2015 03:28 PM
(This post was last modified: Feb 2, 2015 03:28 PM by Temploit.)
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.
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.
DO NOT DEAL WITH THIS MEMBER
this is actually called "Cue Text".
google it, its been around for ages.
DO NOT DEAL WITH THIS MEMBER
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]](https://imgur.com/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 🤝
(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 01:01 PM
(This post was last modified: Feb 3, 2015 01:09 PM by BerryP.)
(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 🤝
(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
|