Apr 152009
 

What’s CMFCEditBrowseCtrl ?

CMFCEditBrowseCtrl is a specialized edit control (MFC Feature Pack VS2008) with a browse button attached to it’s 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 a cool control which I like a bit too much since I know how painful it is to implement one.

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 it’s 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 of CMFCEditBrowseCtrl

  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
    DDX_Control( pDX, IDC_EDIT_FILEBROWSE, m_EditBrowse);
  5. Then in OnInitDialog
    // 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 above calls take some parameters (which has default values) look up in MSDN.

Custom event handling for CMFCEditBrowseCtrl browse button

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

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

More on CMFCEditBrowseCtrl…

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 to 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). 🙂
Aug 292008
 

Call CEdit::SetLimitText or send EM_LIMITTEXT windows message to an edit control. There are some caveats associated with this functionality…

  1. This limitation does not affect the amount of text entered via a call to SetWindowText, or WM_SETTEXT.
  2. This limitation does not hold true for paste operation.
  3. This limitation does not affect text already entered into an edit control.

If above cases are true then the user is allowed to edit the text but not allowed to insert anymore unless the text count falls below allowed count.

Default limit is 32,767 characters.

extern CEdit* pSomeEdit;
pSomeEdit->SetLimitText( 10 );// Only 10 characters can be entered

pSomeEdit->SendMessage(EM_LIMITTEXT, 10, 0); // Using windows message

There is another version called EM_EXLIMITTEXT for rich edit controls which supports lengths above 64k.