Click here to Skip to main content
13,352,175 members (70,714 online)
Click here to Skip to main content
Add your own
alternative version


79 bookmarked
Posted 3 Aug 2006

iTunes like Find Edit Control

, 3 Aug 2006
Rate this:
Please Sign up or sign in to vote.
Edit Control with Button similar to iTunes Find Edit Control

iTunes like Find Edit Control


CEditWithButton is a simple edit control class derived from

(MFC) that looks like the iTunes Find Edit Control. The edit control can show an icon and a button. This class can be used when ever an edit control and a button go together. Example: Browsing a file, folder; Finding etc.

This is my first article at CodeProject. For years I have only used great code from CodeProject. I thank you all for sharing excellent code. Thank you very much. I hope to write more articles in the near future. Please do not hesitate to comment on my code. I wish to learn and improve.

Thanks to Johan Rosengren for his article An edit box with an icon. He revealed the affect of WM_SETFONT message on an edit control in this article.

Using the code

1. Defining the class

Create an edit control in your dialog box and add a variable of type CEditWithButton. If you have already added a variable which is of type CEdit, then you can just rename CEdit to

and #include "EditWithButton.h" at the top of the dialog header file.

#include "editwithbutton.h"

// CFindEditDlg dialog
class CFindEditDlg : public CDialog

CEditWithButton m_editFind;


2. Using the class

The class accepts 2 bitmaps. The first bitmap is drawn as a background of the edit control when it is empty. The second bitmap is drawn as a background when the edit control contains some text in it. The first bitmap is a background without the button. The second bitmap is the background with the button.


Next, we need to set the edit area and the button area. We need to do this because only part of the edit control is editable. The left and right area are covered by the icon (magnifying glass in our case) and the button (x button in our case).

//edit area is rectangle without the icon and the button
//the white space where the user will type
CRect rcEditArea(20,4,125,17);

//button area used to determine the mouse button click
CRect rcButtonArea(128,3,143,18);

Next, when the user clicks on the button, this message is sent to the the owner in order to let it handle the button click event.

//owner window to which the button click notification will be sent

//message that will be sent to the owner window

If the button exists always (even if the edit control does not have any text) then you need to set this flag to TRUE using the SetButtonExistsAlways method. This enables the class to send the button click even even when there is no text in the edit control.

In our case, we have set to FALSE because we are using it as a find edit control where in the button is hidden when the edit control is empty.


Points of Interest (Must Read)

  1. In order to set the edit area for an edit control, we use the CEdit::SetRect method internally. This method succeeds only if the edit control has the multi-line style set (ES_MULTILINE). If you are inserting the edit control in a dialog, make sure the multi-line property is set to True using the resource editor. Also make sure that
    is set to False.

  2. If you are creating the edit control dynamically you must set the edit control's font explicitly in order to make it work perfectly. See the following example code:
    //Get the font from the parent (typically a dialog) and set it to the
    //edit control


  • 3rd August 2006: Updated the code so that the edit control can be created dynamically.
  • 2nd August 2006: First public release


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Gautam Jain
Web Developer
India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Member 102523249-Sep-13 18:15
memberMember 102523249-Sep-13 18:15 
NewsMy code based on this control. Pin
Yaukey28-Apr-13 2:27
memberYaukey28-Apr-13 2:27 
Questionproblem with ES_PASSWORD Pin
jerome8125-Jun-12 21:07
memberjerome8125-Jun-12 21:07 
AnswerRe: problem with ES_PASSWORD Pin
Gautam Jain25-Jun-12 23:50
memberGautam Jain25-Jun-12 23:50 
QuestionCould you please change these to VB.NET or C# Pin
Tinhiuvanoinho23-Jul-11 1:08
memberTinhiuvanoinho23-Jul-11 1:08 
AnswerRe: Could you please change these to VB.NET or C# Pin
Gautam Jain23-Jul-11 2:45
memberGautam Jain23-Jul-11 2:45 
GeneralRe: Could you please change these to VB.NET or C# Pin
Tinhiuvanoinho23-Jul-11 6:28
memberTinhiuvanoinho23-Jul-11 6:28 
Answerfor CRichEditCtrl Pin
xinkmt2-Sep-10 19:41
memberxinkmt2-Sep-10 19:41 
GeneralMy vote of 5 Pin
Lianhai Zhu13-Jul-10 18:31
memberLianhai Zhu13-Jul-10 18:31 
GeneralThe edit box's cue banner is not being drawn Pin
FiSTY14-Jan-10 12:25
memberFiSTY14-Jan-10 12:25 
GeneralRe: The edit box's cue banner is not being drawn Pin
Gautam Jain14-Jan-10 18:17
memberGautam Jain14-Jan-10 18:17 
GeneralRe: The edit box's cue banner is not being drawn Pin
mwhouser25-Jan-10 13:51
membermwhouser25-Jan-10 13:51 
GeneralRe: The edit box's cue banner is not being drawn Pin
Gautam Jain25-Jan-10 20:09
memberGautam Jain25-Jan-10 20:09 
GeneralNice Pin
codiyer23-Jul-09 22:00
membercodiyer23-Jul-09 22:00 
Generalcreate the control dynamically Pin
Minkyu Ha4-Jul-07 17:54
memberMinkyu Ha4-Jul-07 17:54 
GeneralRe: create the control dynamically Pin
Gautam Jain4-Jul-07 19:20
memberGautam Jain4-Jul-07 19:20 
GeneralRe: create the control dynamically Pin
Minkyu Ha5-Jul-07 23:37
memberMinkyu Ha5-Jul-07 23:37 
GeneralRe: create the control dynamically Pin
Gautam Jain6-Jul-07 18:31
memberGautam Jain6-Jul-07 18:31 
GeneralThanks!!! Pin
Zaqro11-Dec-06 0:25
memberZaqro11-Dec-06 0:25 
GeneralA few issues Pin
WalderMort13-Nov-06 0:28
memberWalderMort13-Nov-06 0:28 
GeneralRe: A few issues Pin
Gautam Jain13-Nov-06 1:33
memberGautam Jain13-Nov-06 1:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 3 Aug 2006
Article Copyright 2006 by Gautam Jain
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid