TextBox.BackgroudImage

I don't really see a background image working so swell in a textbox. It would probably become difficult to read the text unless the image is very light. Textbox does inherit the .BackgroundImage property from the Control class, however it does not do anything.
 
I wanted to make the textbox works as what label do.

The reason I do so is:
1. If I use label, my Chinese Character will not be displayed when I deploy my system to Windows 98 and Windows ME.
2. The amazing thing is, textbox could view the Chinese Characters. So, I was thinking on adding a backgroundImage to textBox. Then, my problem can be easily solved.

If you have any idea, please share it with me. Thanks a lot.
 
Why is it that a textbox will support chinese characters but not a label (under 98/ME)? They both use unicode, right? Same fonts, same character sets?

[Edit]Have you tried drawing your own text with the Graphics.DrawString() method to see if that worked for chinese chars?[/Edit]
 
I have anoher idea of doing this.

How can we make the background of the textbox transparent. If I could do so, I could just place a panel with the picture under the textbox.

And with this approach, I need to make the textbox, transparentable, which it is not able to set to transparent by default. I went thru MSDN and found out the codes as below:

Code:
Public Class MyTextBox
    Inherits System.Windows.Forms.TextBox

    Public Sub New()
        SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        UpdateStyles()
    End Sub
End Class

However, it doesn't seems to work... :confused:
 
Why is it that a textbox will support chinese characters but not a label (under 98/ME)? They both use unicode, right? Same fonts, same character sets?

Tell you what? I am confuse with this as well. Whhy it works in Textbox but not Label. Can anyone from Microsoft tell me why?????/
 
Hi George...You'll need to sub class a text box control and over ride the WndProc to get a back ground image in the text box.

I knocked this up for you. It has two public properities for BackImage and ImageAlign and supports all other textbox properities. I hope it helps.

Visual Basic:
Imports System
Imports System.Drawing

Public Class ImageTextBox
    Inherits System.Windows.Forms.TextBox

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.DoubleBuffer, True)
        SetStyle(ControlStyles.ResizeRedraw, True)

    End Sub

    'UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

#End Region

    Private _backImage As Image = Nothing
    Private _imageAlign As ContentAlignment = ContentAlignment.TopLeft


#Region " Public Properties"

    Public Property BackImage() As System.Drawing.Image
        Get
            Return _backImage
        End Get
        Set(ByVal Value As Image)
            _backImage = Value
            If Me.DesignMode = True Then
                Me.Invalidate()
            End If
        End Set
    End Property

    Public Property ImageAlign() As ContentAlignment
        Get
            Return _imageAlign
        End Get
        Set(ByVal Value As ContentAlignment)
            _imageAlign = Value
            If Me.DesignMode = True Then
                Me.Invalidate()
            End If
        End Set
    End Property
#End Region

#Region " WndProc Overrides Sub"

    Protected Overrides Sub WndProc(ByRef m As Message)
        MyBase.WndProc(m)

        Dim g As Graphics = MyBase.CreateGraphics

        If m.Msg = &HF Then
            g.FillRectangle(New SolidBrush(MyBase.BackColor), 0, 0, MyBase.Width, MyBase.Height)
            If Not _backImage Is Nothing Then
                Dim rect As New Rectangle()

                'find rect.X
                Select Case ImageAlign
                    Case ContentAlignment.BottomLeft, ContentAlignment.MiddleLeft, ContentAlignment.TopLeft
                        rect.X = 0
                    Case ContentAlignment.BottomCenter, ContentAlignment.MiddleCenter, ContentAlignment.TopCenter
                        rect.X = CInt((MyBase.Width - _backImage.Width) / 2)
                    Case ContentAlignment.BottomRight, ContentAlignment.MiddleRight, ContentAlignment.TopRight
                        rect.X = MyBase.Width - _backImage.Width
                End Select

                'find rect.Y
                Select Case ImageAlign
                    Case ContentAlignment.TopCenter, ContentAlignment.TopLeft, ContentAlignment.TopRight
                        rect.Y = 0
                    Case ContentAlignment.MiddleCenter, ContentAlignment.MiddleLeft, ContentAlignment.MiddleRight
                        rect.Y = CInt((MyBase.Height - _backImage.Height) / 2)
                    Case ContentAlignment.BottomCenter, ContentAlignment.BottomLeft, ContentAlignment.BottomRight
                        rect.Y = MyBase.Height - _backImage.Height
                End Select
                rect.Width = _backImage.Width
                rect.Height = _backImage.Height

                g.DrawImage(_backImage, rect)
            End If

            If Not MyBase.Text Is Nothing Then

                Dim sFmt As New StringFormat()
                Select Case MyBase.TextAlign
                    Case HorizontalAlignment.Left
                        sFmt.Alignment = StringAlignment.Near
                    Case HorizontalAlignment.Center
                        sFmt.Alignment = StringAlignment.Center
                    Case HorizontalAlignment.Right
                        sFmt.Alignment = StringAlignment.Far
                End Select

                Dim rectF As New RectangleF()
                rectF.Width = MyBase.Width
                rectF.Height = MyBase.Height

                g.DrawString(MyBase.Text, MyBase.Font, New SolidBrush(MyBase.ForeColor), rectF, sFmt)
                sFmt.Dispose()
            End If

        End If

        If Not g Is Nothing Then g.Dispose()
    End Sub
#End Region

#Region " OnTextChanged Overrides Sub"

    Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
        MyBase.OnTextChanged(New EventArgs())
        Me.Invalidate()
    End Sub
#End Region

#Region " OnTextAlignChanged Sub"

    Protected Overrides Sub OnTextAlignChanged(ByVal e As System.EventArgs)
        MyBase.OnTextAlignChanged(New EventArgs())
        Me.Invalidate()
    End Sub
#End Region

End Class
 
Last edited:
The first word came to my mind was "Damn cool man....."

Thanks a lot.... I don't know how could I express my thanks. But, really I really want to say thanks to you.... I am from Malaysia, if you wish to come here, I am willing to spend you a trip....
 
Back
Top