Question transparent label and textbox

mtnlvy

New member
Joined
Jun 6, 2011
Messages
3
Programming Experience
3-5
i've a form with an image covered oll the form i want to put on it labels and textboxes but i want it to be transparent so the user willl see only the text and not the white Rectangle around it. he will see only the image and the text.
 
try Simulating transparent TextBox by Overriding OnPaintBackground?

mm the label is simple, just change the BackColor property to Transparent.

The textbox.. i'm not sure if there's an easy way to do that? If anyone knows (or you find out) Please post that on here!
The only way i can think of since changing the backcolor property on a textbox to transparent will cause an error, is to override the paint method.. and make it transparent yourself by drawing the graphic thats behind it or something...


I don't feel like figuring out all the glitches, but maybe this can get you started? I used the richtextbox because the textbox kept redrawing the white background when the text is being edited and the Richtextbox seemed to behave differently. Anyways this class that inherits the RichTextBox, simulates a transparent textbox. However since the paint method is overrided, you need to write code to handle the cursor and when the text is selected and so forth.. This class just takes whatever the parent background is, crops the location the textbox is over, and draws it as its background. Then it draws its the text on top manually... if you test this code you will see that there is no support for when the text needs to scroll and so forth.

Prob not the best approach? But its a start, i don't feel like writing the code to get this to work perfectly, so heres part of it to get you started..

*again,.. If anyone knows of a better way, please share.!

Public Class transTextBox
    Inherits RichTextBox

    Sub New()
        SetStyle(ControlStyles.UserPaint, True)
        MyBase.BorderStyle = Windows.Forms.BorderStyle.None
        MyBase.Multiline = False
        MyBase.Height = 20
    End Sub

    Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
        drawBg(pevent)
    End Sub

    Sub drawBg(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim img As Image = MyBase.Parent.BackgroundImage
        Dim startPoint As Point = MyBase.Location
        Dim size As Size = MyBase.Size

        Dim rectBackground As New Rectangle(startPoint, size)
        Dim imgBg As New Bitmap(size.Width, size.Height)

        e.Graphics.DrawImage(img, 0, 0, rectBackground, GraphicsUnit.Pixel)
        e.Graphics.DrawString(MyBase.Text, MyBase.Font, New Drawing.SolidBrush(MyBase.ForeColor), New Point(0, 0))

        e.Graphics.Dispose()
    End Sub
End Class
 
Last edited:
Back
Top