Click here to Skip to main content
Click here to Skip to main content

CInputBox

, 21 Jun 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
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 Smile | :)

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 Smile | :)

License

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

Share

About the Author

Mustafa Demirhan
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
GeneralSmall Enhancement PinmemberCodeHead20-Jan-06 4:21 
GeneralInstructions PinmemberRandy Charles Morin12-Dec-03 16:51 
GeneralRe: Instructions PinmemberRandy Charles Morin12-Dec-03 17:00 
GeneralNice one! PinmemberJeremy Davis30-Jan-03 23:41 
GeneralRe: Nice one! PinmemberMustafa Demirhan31-Jan-03 10:42 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 22 Jun 2004
Article Copyright 2001 by Mustafa Demirhan
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid