65.9K
CodeProject is changing. Read more.
Home

CInputBox

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.76/5 (12 votes)

Dec 6, 2001

CPOL

2 min read

viewsIcon

84513

downloadIcon

1231

A CDialog derived class that provides functionality similar to the VB InputBox function

Screenshots

Sample Image

Sample Image

In my projects I usually need something in C++ like the InputBox function in Visual Basic. Nish [BusterBoy] has a CInputBox class derived from CFrameWnd. Although this class provides the basic functionalities, I created my own CInputBox class derived from CDialog. CInputBox class provides all the functionality of the InputBox function in Visual Basic. In addition, it provides some additional nice features:

  • It is possible to get integer values, instead of strings
  • It is possible to show a Browse button to get filenames
  • It is possible to use drag and drop to get the filenames
  • And most importantly, it offers a nice interface :)

How to use the class?

#include "DialogHeaderCtrl.h"
#include "FocusEditCtrl.h"

enum InputBox_DataType 
{
    DataType_String, 
    DataType_Integer
};

enum InputBox_BrowseOption
{
    Browse_None,
    Browse_Open,
    Browse_Save,
    Browse_Folder
};


/////////////////////////////////////
// CInputBox dialog

class CInputBox : public CDialog
{
// Construction
public:
    void SetLabel (const CString &sLabel);
    void SetDefaultText (const CString &sDefault);
    void SetDesc (const CString &sDesc);
    void SetTitle (const CString &sTitle);
    void SetAllowEmpty (bool bAllowEmpty);
    void SetDataType (InputBox_DataType nDataType);
    void SetBrowseOption (InputBox_BrowseOption nBrowse);
    void SetIcon (UINT nIcon);
    
    CInputBox (CWnd* pParent = NULL);
        
    // nBrowse = Browse_None => Hide browse button
    // nBrowse = Browse_Open => Browse button opens Open File dialog
    // nBrowse = Browse_Save => Browse button opens Save File dialog
    // nBrowse = Browse_Folder => Browse button opens Folder dialog
    // nDataType = DataType_String => String
    // nDataType = DataType_Integer => Integer
    CInputBox ( const CString &sTitle, const CString &sDesc, const CString &sLabel,
                const CString &sDefault, UINT    nIcon = NULL,
                InputBox_BrowseOption nBrowse = Browse_None, 
                InputBox_DataType nDataType = DataType_String,
                bool bAllowEmpty = false,
                CWnd* pParent = NULL);   // standard constructor

// Dialog Data
    //{{AFX_DATA(CInputBox)
    enum { IDD = IDD_INPUTBOX_DIALOG };
    CFocusEditCtrl    m_Edit1;
    CDialogHeaderCtrl    m_HeaderCtrl;
    CButton    m_BrowseBtn;
    CString    m_sLabel1;
    CString    m_sValue;
    //}}AFX_DATA


// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CInputBox)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL

// Implementation
protected:
    CString        m_sTitle, m_sDesc;
    InputBox_BrowseOption m_nBrowse;
    InputBox_DataType m_nDataType;
    bool        m_bAllowEmpty;
    UINT        m_nIcon;
    bool        m_bInitialized;
    
    // Generated message map functions
    //{{AFX_MSG(CInputBox)
    virtual BOOL OnInitDialog();
    afx_msg void OnBrowse();
    virtual void OnOK();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

Here are some examples that show how to use CInputBox class:
CInputBox dlg;
dlg.SetTitle ("Enter yout title here");
dlg.SetDesc ("Enter your description here");
dlg.SetLabel ("Enter your label here");
dlg.SetDefaultText ("enter the default text to be shown here");

if (dlg.DoModal () == IDOK)
    MessageBox (dlg.m_sValue, "InputBoxDemo", MB_OK | MB_ICONINFORMATION);
else
    MessageBox ("Cancel selected", "InputBoxDemo", MB_OK | MB_ICONINFORMATION);

You can set the title using void SetTitle (const CString &sTitle) method.
You can set the description using void SetDesc (const CString &sDesc) method.
You can set the label using void SetLabel (const CString &sLabel) method.
You can set the initial text to be shown in the Edit control using void SetDefaultText (const CString &sDefault) method.

You can set the icon to be show in the dialog using void SetIcon (UINT nIcon) method. Here is an example:

dlg.SetIcon (IDI_DOCUMENT);

You can set the data type to using void SetDataType (int nDataType) method.

  • If nDataType is DataType_String then the input is a string
  • If nDataType is DataType_Integer then the input is numeric. Only 0123456789 are accepted as inputs

If you want to be able to get empty inputs, use SetAllowEmpty (bool bAllowEmpty) method.

  • If bAllowEmpty is false, then the user is forced to enter a value.
  • If bAllowEmpty is true, then the user can just leave the input field empty.

To show Browse button, use SetBrowseOption (int nBrowseOption) method. The parameter nBrowseOption can take 4 values:

  • If nBrowseOption is Browse_None, Browse button is hidden
  • If nBrowseOption is Browse_Open, Browse button is visible and it shows Open File Dialog when clicked
  • If nBrowseOption is Browse_Save, Browse button is visible and it shows Save File Dialog when clicked
  • If nBrowseOption is Browse_Folder, Browse button is visible and it shows Browse Folder Dialog when clicked

Here are additional examples:

CInputBox dlg;
dlg.SetTitle ("Find");
dlg.SetDesc ("Please enter the user name to be found");
dlg.SetLabel ("User name:");
dlg.SetIcon (IDI_DOCUMENT);
if (dlg.DoModal () == IDOK)
    MessageBox (dlg.m_sValue, "InputBoxDemo", MB_OK | MB_ICONINFORMATION);
else
    MessageBox ("Cancel selected", "InputBoxDemo", MB_OK | MB_ICONINFORMATION);
CInputBox dlg;
dlg.SetTitle ("Age");
dlg.SetDesc ("Please enter your age below.");
dlg.SetLabel ("Age:");
dlg.SetIcon (IDI_DOCUMENT);
dlg.SetDataType (DataType_Integer);

if (dlg.DoModal () == IDOK)
    MessageBox (dlg.m_sValue, "InputBoxDemo", MB_OK | MB_ICONINFORMATION);
else
    MessageBox ("Cancel selected", "InputBoxDemo", MB_OK | MB_ICONINFORMATION);
CInputBox dlg;
dlg.SetTitle ("Create Directory");
dlg.SetDesc ("Please enter the directory to be created.");
dlg.SetLabel ("Directory:");
dlg.SetDefaultText ("c:\\temp");
dlg.SetIcon (IDI_DIRECTORY);
dlg.SetBrowseOption (Browse_Folder);
    
if (dlg.DoModal () == IDOK)
    MessageBox (dlg.m_sValue, "InputBoxDemo", MB_OK | MB_ICONINFORMATION);
else
    MessageBox ("Cancel selected", "InputBoxDemo", MB_OK | MB_ICONINFORMATION);

Or you can initialize all the member variables just using the constructor:

CInputBox dlg ("Create Directory", "Please enter the directory to be created", 
               "Directory:", "c:\\temp", IDI_DIRECTORY, Browse_Folder);
    
if (dlg.DoModal () == IDOK)
    MessageBox (dlg.m_sValue, "InputBoxDemo", MB_OK | MB_ICONINFORMATION);
else
    MessageBox ("Cancel selected", "InputBoxDemo", MB_OK | MB_ICONINFORMATION);

Some points that you should keep in mind

  • To use CInputBox class in your projects, you have to add files InputBox.h, InputBox.cpp, DialogHeaderCtrl.h, DialogHeaderCtrl.cpp, FocusEditCtrl.h and FocusEditCtrl.cpp to your project. Also you have to copy the dialog resource IDD_INPUTBOX_DIALOG to your project.
  • Keep in mind that my implementation may not be the most efficient way of doing such a class. I am neither a GUI guru, nor a genius :)