Programmer's Corner Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Programmer's Corner - Forums


Custom Textbox [with 'Background Title Text']

 
Post new topic   Reply to topic    Programmer's Corner Forum Index -> Visual Basic .NET
Author Message
WannaBe
Wiggles


Joined: 22 Oct 2004
Posts: 714
Location: CA

PostPosted: Thu Nov 30, 2006 1:29 pm    Post subject: Custom Textbox [with 'Background Title Text'] Reply with quote

I couldn't figure out the correct term nor the source I found a long time ago for VB6 to do this (was like a win api call [or 2]).

Anyways, you know how some textboxes have a grayed text titling the textbox (eg: "Search", "Username") well I tried searching around for how to do that in .Net but had no luck since I couldn't figure out what it is even called Razz

It is a pretty simple method, if the textbox has no focus and blank it would display a light gray text of something so... I created a custom textbox just to do that!

Code:

Imports System.Windows.Forms
Imports System.Drawing
Imports System.ComponentModel

Public Class clsCustomTextbox
    Inherits TextBox
    Private strBackText As String
    Private bshBackText As SolidBrush

    <Category("Appearance"), DefaultValue(GetType(Color), "BackText"), _
    Description("The foreground color of the background title text."), _
    RefreshProperties(RefreshProperties.Repaint)> _
    Public Property BackText() As String
        Get
            Return strBackText
        End Get
        Set(ByVal value As String)
            strBackText = value
        End Set
    End Property

    <Category("Appearance"), DefaultValue(GetType(Color), "ForeColorBackText"), _
    Description("The foreground color of the background title text."), _
    RefreshProperties(RefreshProperties.Repaint)> _
    Public Property ForeColorBackText() As Color
        Get
            Return bshBackText.Color
        End Get
        Set(ByVal value As Color)
            bshBackText = New SolidBrush(value)
        End Set
    End Property

    Public Sub New()
        MyBase.New()
        bshBackText = SystemBrushes.GrayText
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Const WM_PAINT As Integer = &HF
        MyBase.WndProc(m)

        If m.Msg = WM_PAINT Then
            If String.IsNullOrEmpty(MyBase.Text) Then
                If String.IsNullOrEmpty(strBackText) = False Then
                    If MyBase.Focused = False Then
                        MyBase.CreateGraphics.DrawString(strBackText, MyBase.Font, bshBackText, 0, 0)
                    End If
                End If
            End If
        End If
    End Sub
End Class
Back to top
Eric
Idea Hamster


Joined: 21 Oct 2004
Posts: 679
Location: Bangor, Maine

PostPosted: Mon Jan 14, 2008 3:11 pm    Post subject: Reply with quote

Handy... I had routines to do that in VB Classic.

Unfortunately, I don't do my UI based stuff these days, but I'll stash this in my arsenal anyhow...
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Programmer's Corner Forum Index -> Visual Basic .NET All times are GMT - 5 Hours
Page 1 of 1

 


Powered by phpBB © 2001, 2002 phpBB Group