Watermarking a textbox

  • Thread starter Thread starter Johnny J.
  • Start date Start date
J

Johnny J.

Does anyone have any sample code on how to draw a semi transparent image as
a watermark on a multiline textbox?

TIA,
Johnny J.
 
If you are willing to use the AjaxControlToolkit, you can do something like
the following:

<asp:TextBox ID="txtWaterMark" runat="server" TextMode="MultiLine" />
<AJAX:TextBoxWatermarkExtender runat="server" TargetControlID="txtWaterMark"
WatermarkText="MultiLine WaterMark" />

Hopefully this helps.
 
Johny,

There is simply no method for that.

To do something with an image you need a picturebox

Cor
 
Johnny said:
Does anyone have any sample code on how to draw a semi transparent image
as a watermark on a multiline textbox?

TIA,
Johnny J.

You should be able to do this by extending the standard textbox, and
overriding the Paintevent in the derived class to draw the image then
the text.
 
That's precisely what I'm trying to do, but I can't get it to work, so
that's why I'm looking for a code sample

Cheers,
Johnny J.
 
Sorry Nathan, but you misread my question. By watermark, I don't mean a
descriptive text, I mean a semi transparent image.

Besides: I'm trying to do it in a windows app - not ASP.NET

But thanks anyway,
Johnny J.
 
Johnny said:
That's precisely what I'm trying to do, but I can't get it to work, so
that's why I'm looking for a code sample

Cheers,
Johnny J.

This should get you started. Add this class to a form project then set
the image property of the FancyTextbox.

Public Class FancyTextbox
Inherits TextBox

Private _Image As Image
Public Property Image() As Image
Get
Return _Image
End Get
Set(ByVal value As Image)
_Image = value
End Set
End Property

Public Sub New()
Multiline = True
SetStyle(ControlStyles.UserPaint, True)
End Sub

Protected Overrides Sub OnPaint _
(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
If (Image IsNot Nothing) Then
e.Graphics.DrawImage(Image, New PointF(0.0F, 0.0F))
End If
e.Graphics.DrawString(Text, Font, Brushes.Yellow, 0, 0)
End Sub

Private Sub FancyTextbox_TextChanged _
(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.TextChanged
Invalidate()
End Sub
End Class
 
That looks very much like what I have been trying to do, but I didn't get
all that far.

How do I solve the semi transparent thing?

Thanks,
Johnny
 
Johnny said:
That looks very much like what I have been trying to do, but I didn't
get all that far.

How do I solve the semi transparent thing?

Thanks,
Johnny

That is part of your scenario that I did not understand. You have a
white background layer, your image layer, then the text foreground
layer. Your image just replaces the white background. If you need to
show some white, make an image that shows some white through parts of
the image. My idea was the image would be an image of some watermark on
a white background.
 
Johnny J. said:
I'm sure you're right Mike, but I'm, afraid it doesn't help me much
without a code sample

The only code example I can give you is for VB6. Basically it involves
creating a suitable brush (a copy of the underlying Form background colour
or background image, or a translucent copy of it) and then subclassing the
TextBox and persuading it to use that brush each time it updates its
background rather than the solid white brush it usually uses. There is no
native way of doing that in VB6 so the code is basically just a bunch of API
calls. I'm not sure how VB.Net behaves when you sublass things in that way,
and there may even be some native VB.Net methods to help you do it. If
you're interested in the VB6 code and you think you may be able to convert
it to VB.Net then you should be able to find it on the web somewhere. I
think some suitable code used to be available on elitevb.com, but that site
seems to have gone down the pan over recent years :-(

Mike
 
Hi Johnny,

Not sure how much you know about calling the Windows API functions to set
control styles, but this will do what you're after:

SendMessage(textBox1.Handle, EM_SETCUEBANNER, 0, "Type your question")

You need to import the SendMessage API call and declare the EM_SETCUEBANNER
message, but that should do exactly what you're looking for. The nice part
is that it's handled by the OS (works on XP and later) so any changes to the
way the OS represents watermarks (different visual theme for example) will
be handled automatically without you having to change any code.

Hope that helps,
Alex
 
Sorry Alex, but you have misread my post. This is not what I mean by
watermarking.

Actually I don't think this should be called watermarking at all, because
this is something quite different. I would prefer calling it a cue instead
of a watermark. By a watermark I mean something like what you see with
stamps e.g.

But thanks for your time,
Johnny J.
 
That will work too, but I just thought that maybe there was some way of
setting the transparency of the image before painting it so I didn't have to
Photoshop it (it's quite a number of images that should be changed).

Another thing, though:

Will e.Graphics.DrawString work when it's multiline or do you think I have
to measure the string, parse it and paint each line separately?

Cheers,
Johnny




But
 
I searched the web for code like that (not specifically VB6 code), but
didn't find any - hence my post to this forum. But I think I'm on my way to
finding the .NET solution. In another thread to the post, a solution is
suggested by another Mike (or is it you in disguise?), and that looks
promising.

Thanks a lot for the help,
Johnny J.
 
OH, one more thing:

How do I convert a color to a brush?

I thought I would take MyBase.ForeColor in the subclassed textbox and
convert to a brush and use that color to paint the text.

Cheers,
Johnny J.
 
Forget the last question - that was easy:

Dim myColor As Color = Color.FromArgb(...)
Dim myBrush As SolidBrush = New SolidBrush(myColor)

Cheers,
Johnny J.
 
I have tested now, and it does work with multiline

But the textbox behaves strangely:

1) It flickers when you write
2) You can actually place the cursor outside the text
3) If you doubleclick the text, it becomes bold and when the textbox loses
focus, it returns to normal

If somebody has experience solving these problems, I'd really like to
know... ;-)

Cheers
Johnny J.
 
Back
Top