Click here to Skip to main content
15,922,427 members
Articles / Desktop Programming / MFC

MFC Feature Pack - CMFCEditBrowseCtrl

Rate me:
Please Sign up or sign in to vote.
4.69/5 (5 votes)
20 Apr 2009CPOL2 min read 77.5K   18   17
MFC Feature Pack - CMFCEditBrowseCtrl

CMFCEditBrowseCtrl?

It’s a specialized edit control (MFC Feature Pack VS2008) with a browse button attached to its right side, when we click on this button, we get an open file dialog or an open folder dialog. See this sample screenshot.

Browse edit control sample

Browse edit control sample

Also, this control allows us to implement our own event handling by overriding OnBrowse function of this class. This is a cool control which I like a bit too much since I know how painful it is to get one going.

Some tips on how to make such a control…

  1. Handle nc calcsize event, so that you can specify size of the area that the browse button will take which in turn results in edit control resizing its client area to adjust the button.
  2. Handle nc paint to draw the button, also you have to force generate an nc calcsize message for once in the beginning.
  3. Handle mouse up and mouse down event.

Usage

  1. You should be working in VS2008 SP1 or with Feature pack installation
  2. Add an edit control to a dialog
  3. Open .h file of this dialog’s class and add a member variable - CMFCEditBrowseCtrl m_EditBrowse;
  4. Open .cpp file and add a call to sub class this item in DoDataExchange
    C++
    DDX_Control( pDX, IDC_EDIT_FILEBROWSE, m_EditBrowse);
  5. Then in OnInitDialog
    C++
    // Note: Only one of these calls will work at a time!
    m_EditBrowse.EnableFileBrowseButton(); // To show file open dialog
    m_EditBrowse.EnableFolderBrowseButton(); // To show folder browse dialog
    m_EditBrowse.EnableBrowseButton(); // To do custom event handling
  6. That’s it, now you’ve got the browse edit working. :)
  7. Note that the above calls take some parameters (which has default values) look up in MSDN.

Custom Event Handling for Browse Button

Here is a small sample on how to handle custom browse button event handling, taken from MSDN samples…

C++
class CMyBrowseEdit : public CMFCEditBrowseCtrl
{
    virtual void OnBrowse()
    {
        MessageBox(_T("Browse item..."));
        SetWindowText(_T("New value!"));
    }
};

More…

Use SetBrowseButtonImage to change browse button image, also has an option to maintain such a bitmap or an icon. There are some more virtual functions which could be useful…

  1. OnDrawBrowseButton - Override for some additional painting from your side
  2. OnChangeLayout - Called when browse button mode changes, i.e. from folder to file mode, etc. (I guess so) :)

Posted in CodeProject, MFC Tagged: CEdit, CMFCEditBrowseCtrl, Extending edit controls, MFC Feature Pack Image 4 Image 5 Image 6 Image 7 Image 8 Image 9

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Microsoft
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionImproper Argument Pin
rbrunton14-Oct-21 7:46
rbrunton14-Oct-21 7:46 
I have not seen any comments on this, but I am wondering whether others have experienced it and, if so, what is the solution.

I have a dialog with an edit control for which I defined a variable
C++
m_ecImageFolder
. I have created a class
C++
CMyEditBrowser
derived from
C++
CMFCEditBrowseCtrl
. Next, I changed the variable type from CEdit to CMyEditBrowser in the header file and included the line
C++
m_ceImageFolder.EnableFolderBrowseButton()
. When I open this dialog, I get the message "Encountered an improper argument". (The same thing happens if I just make m_ecImageFoler a CMFCEditBrowseCtrl without defining a derived class.)

If I change the "EnableFolderBrowseButton" to
C++
m_ceImageFolder.EnableBrowseButton()
the dialog opens with the edit control and associated "..." browse button. (In my derived class I have an OnBrowse() function that calls
C++
CFolderPickerDialog
to do the job of what I thought was the purpose of
EnableFolderBrowseButton
)

All the other comments seem to suggest that this is an odd problem (i.e. no one else has it). I would greatly appreciate any thoughts other might have.

I am programming in C++, MFC application using VS 2019 Community Edition.
AnswerRe: Improper Argument Pin
rbrunton16-Oct-21 5:15
rbrunton16-Oct-21 5:15 
AnswerRe: Improper Argument Pin
rbrunton16-Oct-21 5:32
rbrunton16-Oct-21 5:32 
Questionkeep CString on CEditBrowse Control Pin
Hyosoka Poipo4-Sep-13 18:03
Hyosoka Poipo4-Sep-13 18:03 
GeneralSetting the initial directory?... Pin
John D. Marinuzzi3-Feb-11 5:52
John D. Marinuzzi3-Feb-11 5:52 
GeneralProblems using it within a DLL (on XP) Pin
Joerg Hoffmann16-Oct-09 2:19
Joerg Hoffmann16-Oct-09 2:19 
GeneralCMFCEditBrowserCtrl Pin
fangxing6-Aug-09 16:40
fangxing6-Aug-09 16:40 
QuestionRe: CMFCEditBrowserCtrl Pin
David Crow1-May-10 16:54
David Crow1-May-10 16:54 
GeneralSome little help needed about that new class Pin
super_ttd16-Jul-09 5:36
super_ttd16-Jul-09 5:36 
GeneralRe: Some little help needed about that new class Pin
Nibu babu thomas16-Jul-09 17:58
Nibu babu thomas16-Jul-09 17:58 
GeneralRe: Some little help needed about that new class Pin
super_ttd16-Jul-09 21:34
super_ttd16-Jul-09 21:34 
GeneralRe: Some little help needed about that new class Pin
Nibu babu thomas17-Jul-09 1:33
Nibu babu thomas17-Jul-09 1:33 
GeneralRe: Some little help needed about that new class Pin
super_ttd17-Jul-09 2:50
super_ttd17-Jul-09 2:50 
GeneralRe: Some little help needed about that new class Pin
Nibu babu thomas17-Jul-09 3:33
Nibu babu thomas17-Jul-09 3:33 
GeneralRe: Some little help needed about that new class Pin
super_ttd17-Jul-09 3:54
super_ttd17-Jul-09 3:54 
GeneralAlready implemented Pin
PJ Arends22-Apr-09 16:25
professionalPJ Arends22-Apr-09 16:25 
GeneralRe: Already implemented Pin
mluri21-Apr-10 3:39
mluri21-Apr-10 3:39 

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.