6.17.2011

Textual Cue For TextBox Control

In this article, every thing is based on this : EM_SETCUEBANNER

this is a Windows Message that we can send to an edit control such as a textbox, so we can have something like this :
 
this tip "Enter your full name here" on the textbox is usally useful to the user and avoid some confusion.
All we need to do, is to send this message to the handle of the control, via SendMessage Api.

Declare Unicode Function SendMessageW Lib "User32.dll" (ByVal Hwnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Boolean, ByVal lParam As String) As Boolean
    Const EM_SETCUEBANNER = 5377
    Function SetCueText(ByRef Textbox1 As TextBox, ByVal CueText As String, Optional ByVal ShowOnFocus As Boolean = False) As Boolean
        Return SendMessageW(Textbox1.Handle, EM_SETCUEBANNER, ShowOnFocus, CueText)
    End Function

Usage :
Call SetCueText wih these parameters: :
Textbox1 : the textbox control that you want to add a cue text to it. (Textbox)
CueText : The Tip that will show on the textbox (String)
ShowOnFocus : Set to true if you want the cue text visible even if the textbox has the focus (boolean)
Return value : returns true if it succeeds, otherwise it returns false.
Even Better, we can extend the textbox control, and add a property Called "CueText", just like this :

Imports System.Windows.Forms

Public Class mTextBox
    Inherits TextBox
    Declare Unicode Function SendMessageW Lib "User32.dll" (ByVal Hwnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Boolean, ByVal lParam As String) As Boolean
    Const EM_SETCUEBANNER = 5377
    Private _CueText As String
    Private _CueFocus As Boolean
    Public Property CueText() As String
        Get
            Return _CueText
        End Get
        Set(ByVal value As String)
            _CueText = value
            If Me.Handle <> IntPtr.Zero Then SendMessageW(Me.Handle, EM_SETCUEBANNER, New IntPtr(CInt(_CueFocus)), _CueText)
        End Set
    End Property
    Property CueTextFocus() As Boolean
        Get
            Return _CueFocus
        End Get
        Set(ByVal value As Boolean)
            _CueFocus = value
            CueText = CueText
        End Set
    End Property
End Class

What we did, we created a control that inherits from the usual textbox, and added two properties that allow us to get and set the cue text, and also whether to show on focus or not.

2 comments:

Anonymous said...

can you do combobox in listview? :D

Brahim Hadriche said...

Actually, it's possible, in two ways.

First method, is to "override" the paint event in the listview class so you can "draw" a progress bar instead of text in the desired field. it is creating a custom listview.

the second method is to add a progressbar control to the controls of the listview (progressbar becomes child control of the listview) and keep an eye on the column resize event, scrolling, ect.. so you can move and resize the progressbar in the correct place where is should be. i find this a little laggy hack, but it does the job. i hope i explained in clear simple way.