SmartEdit control

SmartEdit and Slider Test

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, 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.

This code was developed and tested using Visual C++ v5.0 on NT v4.0 SP3.

Download demo project - 16 KB

Download source - 6 KB



Comments

  • ExEditors Library

    Posted by Legacy on 03/19/2002 12:00am

    Originally posted by: Mike Philips

    A complete collection of editors in a single file:
    
    

    http://www.exontrol.com/sg.jsp?content=products/exeditors

    Regards,
    Mike

    Reply
  • Problem with edit control

    Posted by Legacy on 01/20/2001 12:00am

    Originally posted by: Rajasekhar

    Hello Everybody,

    I am using eVC 3.0 to develop applications for pocketPC.

    I developed one SDI application using DVArchitecture.My main View is CView.It's working fine.Later i included one dialog and using ClassWizard i created a class with CEditView as the base calss. Using CSingleDocTemplate i am including this view as another template.The view is getting displayed perfectly for typing in the edit control,saving. But when i used Open option, after selecting a file, main view is coming up and the edit view is going back.I am not implementing the open and saving dialog boxes.

    What might be the reason?How to keep the editview as the top window until i close it?


    How to get the text i typed in CEditView to a CString?

    Thanking you.

    Regards...
    Rajasekhar

    Reply
  • Problem pasting alphas into numeric fields

    Posted by Legacy on 02/25/2000 12:00am

    Originally posted by: Jack Renet

    I was able to paste alphabetic characters into a numeric field (using Windows 2000 and VC 6.0 SP3). Did I make a mistake somewhere or how can it be prevented.?

    Als I was able to enter 'E' twice into a floatingpoint field but was not able to enter 'D' itno a floatingpoint field ('D' originates from FORTRAN to distinguish between single and double precision, in an environment where FORTRAN is still being used it might be good to except it as well).

    Reply
  • Usage with A CFormView

    Posted by Legacy on 01/28/1999 12:00am

    Originally posted by: Rick York

    A small change is needed to use these controls with CFormView.
    In SmartEdit.h, the prototype of the member SetSlideLink
    should be changed to read :

    void SetSlideLink( CWnd *pParent, int sliderid );

    The implementation must be changed to reflect this also
    (in SmartEdit.cpp). Originally, the pParent arg was a
    CDialog* and this did not work with a CFormView. As it
    turns out, it is needed for a call to GetDlgItem which
    is a member of CWnd so a CDialog was never actually
    required anyway.

    Thanks to Steven McNeese for discovering this problem.

    Please let me know if anyone makes enhancements or
    corrections to this.

    Rick

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds