Click here to Skip to main content
15,892,059 members
Articles / Desktop Programming / MFC
Article

A smart edit and linked slider control

Rate me:
Please Sign up or sign in to vote.
3.80/5 (3 votes)
2 Feb 2000 138.2K   2.8K   28   20
An edit control for entering text, numbers, hexadecimal or floating point values, and which can be linked to a slider control
  • Download demo project - 23 Kb
  • Download source files - 6 Kb
  • Sample Image - smartedit.jpg

    The code included here implements what I call a Smart Edit control.  This began life in a sample from the MSDN called CTRLTEST.  This control was originally called CParsedEdit and it allows one to specify types of allowable characters that can be entered into an edit box.  I have changed its name to CSmartEdit and added more functionality to it.  It now supports more character types including numbers, characters, hexadecimal, floating point (with exponents), underscores, and negative signs.  The biggest enhancement in functionality is that one can associate or link an edit box with a slider to provide what I call coordinated updates.  This means that if you drag the slider around you will see a corresponding change in the number displayed in the edit box and vice-versa.  I call the derived slider control class CLinkSlider.

    As an added bonus, I have included some bitmapped button images that came with the original sample and some that I drew myself.  The bitmaps I drew are for the disabled states of ok and cancel and for all four states of the apply and help buttons.  The four button states are up, down, focused, and disabled.  The names of bitmaps end in U, D, F, and X for the four states.

    It is very easy to use the CSmartEdit control.  These are the steps :

    • Declare a member variable of type CSmartEdit in the AFX_DATA section of the dialog.
    • Add a DDX_Control statement in the AFX_DATA_MAP to associate the resource to the member.
    • In OnInitDialog set the type of the control with SetParseType.

    As you probably know, the class wizard can do the first two steps for you.  Note that the resource style of the edit box does NOT have to be anything special.

    It is also very easy to use the CLinkSlider control.  These are the steps :

    • Add a CSmartEdit control as in steps 1 and 2 above.
    • Add a CLinkSlider control similar to steps 1 and 2 above.
    • In OnInitDialog link the slider and edit box by calling SetSlideLink and pass the resource id of the slider.
    • Also in OnInitDialog, set the minimum and maximum values and the number of ticks for the slider with SetParams  There are two versions of this function, one for integers and one for floating point doubles.  The floating point version also takes a format string that specifies how the value will be displayed.

    Here is a code snippet that illustrates using a smart edit box and two linked slider-edit boxes, one integer and one floating point.

    // Dialog Data in dialog class declaration
    
    //{{AFX_DATA(CTestSlidersDlg)
    enum { IDD = IDD_SLIDE_DLG };
    CSmartEdit	m_Edit1;
    CSmartEdit	m_Edit2;
    CSmartEdit	m_Edit3;
    CLinkSlider	m_Slider1;
    CLinkSlider	m_Slider2;
    //}}AFX_DATA
    
    ...
    
    // in dialog's DoDataExchange function
    
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CTestSlidersDlg)
    DDX_Control(pDX, IDC_EDIT1, m_Edit1);
    DDX_Control(pDX, IDC_EDIT2, m_Edit2);
    DDX_Control(pDX, IDC_EDIT3, m_Edit3);
    DDX_Control(pDX, IDC_SLIDER1, m_Slider1);
    DDX_Control(pDX, IDC_SLIDER2, m_Slider2);
    //}}AFX_DATA_MAP
    
    ...
    
    // in dialog's OnInitDialog function
    
    CDialog::OnInitDialog();
    	
    // setup first slider-edit box - integer
    
    m_Edit1.SetSlideLink( this, IDC_SLIDER1 );
    m_Edit1.SetParams( -100, 100, 10 );
    m_Edit1.SetValue( 0 );
    
    // setup second slider-edit box - floating point
    
    m_Edit2.SetSlideLink( this, IDC_SLIDER2 );
    m_Edit2.SetParams( 0.0, 10.0, 10, "%6.3f" );
    m_Edit2.SetValue( 2.0 );
    
    // setup third edit box - it is not linked and accepts only letters
    
    m_Edit3.SetParseType( SES_LETTERS );

    Lastly, I will briefly describe how to use the bitmapped buttons.

    • Define a button resource in the dialog that has Owner Draw style enabled.
    • Declare a variable in the dialog of type CBitmapButton.
    • In OnInitDialog call Button.AutoLoad( ButtonId, this )

    That's all there is to it.  The one gotcha to using AutoLoad is that the text of the button must match the name of the bitmap.  This means that for the cancel button, its text MUST be Cancel and for the apply button, its text MUST be Apply.  Note that case does not matter for the text of the button.  See the documentation on CBitmapButton for more details.

    A note about Unicode: first of all, I have attempted to make this compatable with Unicode but I have not tested it with a MBCS.  The principle area where it matters use Unicode-compatable functions for checking each character entered into the edit box.  Please let me know of any problems encountered (and successes :)

    The demo project is a dialog app having four dialogs.  One is the choser dialog and the others are for testing just the edit boxes, just the buttons, and one that shows all of the controls together as depicted in the image.

    License

    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


    Written By
    Software Developer (Senior)
    United States United States
    I work on an industrial HPC project that can run on either the CPU or the GPU. I usually use which ever one has the most horsepower on a given machine. It's written with CUDA with very few ifdefs used. My company is quite large, in the top five in our industry in North America, and I work in a small group with just five programmers.

    Comments and Discussions

     
    QuestionNeed to use this component in my employer's application Pin
    Member 220527422-Mar-23 3:59
    Member 220527422-Mar-23 3:59 
    QuestionSlider moves back to last position!?!? Pin
    KO080928-Aug-06 8:21
    KO080928-Aug-06 8:21 
    GeneralError Pin
    Dansveen25-Jan-06 6:44
    Dansveen25-Jan-06 6:44 
    GeneralUNICODE Pin
    collingwoody6-Oct-04 3:09
    collingwoody6-Oct-04 3:09 
    QuestionCSmartEdit inactivity? Pin
    a_user_321-Sep-04 0:38
    a_user_321-Sep-04 0:38 
    GeneralInsert CSmartEdit/CLinkedSlider into Class Wizard Pin
    a_user_38-Sep-04 0:52
    a_user_38-Sep-04 0:52 
    GeneralRe: Insert CSmartEdit/CLinkedSlider into Class Wizard Pin
    a_user_321-Sep-04 0:20
    a_user_321-Sep-04 0:20 
    GeneralCLinkSlider : a small improvement Pin
    Anonymous29-Jul-04 4:04
    Anonymous29-Jul-04 4:04 
    GeneralGreat but little bug Pin
    Cedric Moonen27-May-03 21:42
    Cedric Moonen27-May-03 21:42 
    GeneralGreat Tool! Miss two things! Pin
    Diego A. Castaño18-May-02 3:44
    Diego A. Castaño18-May-02 3:44 
    GeneralGreat Slider/Edit Controls Pin
    Ancient Dragon30-Mar-02 10:37
    Ancient Dragon30-Mar-02 10:37 
    GeneralRe: Great Slider/Edit Controls Pin
    Ancient Dragon30-Mar-02 10:55
    Ancient Dragon30-Mar-02 10:55 
    GeneralRe: Great Slider/Edit Controls Pin
    Rick York30-Mar-02 11:06
    mveRick York30-Mar-02 11:06 
    GeneralRe: Great Slider/Edit Controls Pin
    Rick York30-Mar-02 11:05
    mveRick York30-Mar-02 11:05 
    GeneralSlider control only Pin
    Ericandice1-Feb-02 14:49
    Ericandice1-Feb-02 14:49 
    GeneralRe: Slider control only Pin
    Rick York1-Feb-02 17:56
    mveRick York1-Feb-02 17:56 
    Generalvery nice work Pin
    10-Sep-01 12:30
    suss10-Sep-01 12:30 
    GeneralOutput of edit boxes. Pin
    john kappas10-May-01 22:22
    john kappas10-May-01 22:22 
    GeneralComplements Pin
    Salim Khan (India)29-May-00 6:24
    sussSalim Khan (India)29-May-00 6:24 
    GeneralComplements Pin
    Salim Khan (India)29-May-00 6:24
    sussSalim Khan (India)29-May-00 6:24 

    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.