Click here to Skip to main content
15,881,248 members
Articles / Desktop Programming / MFC
Article

Rapid data entry: An edit box with a history

Rate me:
Please Sign up or sign in to vote.
4.72/5 (17 votes)
20 Jun 2004Public Domain3 min read 80.2K   2.6K   42   14
A way of simplifying rapid data entry

Sample Image - bufferededit.gif

Introduction

For rapid data entry, the normal solution with a drop down list where earlier entries can be selected is not always of any help. A drop down is far too interactive, and demands - in my opinion - quite a lot of concentration on the part of the user.

This class is far simpler - close to annoyingly basic. It stores the entries from the edit box in a CStringArray, and this list can be navigated by pressing the page up or page down keys. As an added feature, pressing the right-arrow key when the cursor is located at the right end of the text will - if appropriate - add the character at this same position in the last history string to the edit box. Ctrl+PgUp will find the previous item of the history in alphabetical order, Ctrl++PgDn the previous. If the appropriate style is set, the history will be circular (wrap around) and/or sorted (non-case sensitive). It might also be autocompleting, and the max size of the history can be set.

Using the code

CBufferedEdit is a simple CEdit-derived class. It can be instantiated dynamically, by calling Create, or from a dialog template.

The class has a private CStringArray member m_history, which holds the earlier strings. The current contents of the edit box is added to this array as soon as WM_SETTEXT is sent to the control (such as when SetWindowText is called), The class also contains a private int member - m_current - holding the current position in the list.

WM_CHAR is mapped, of course, for handling VK_PRIOR, VK_NEXT and VK_RIGHT.

As the history strings are set, a few contortions has to be made in the code to remove them from the history list - otherwise, SetWindowText would have triggered the WM_SETTEXT-mapping, and strings would have been added to the history by just navigating it. This is accomplished in UpdateFromHistory.

Public interface

void CBufferedEdit::ClearBuffer()

Clears the history buffer and resets the current pointer

void CBufferedEdit::SetBufferStyle( int style )

Sets the buffer style for the control. Styles can be ORed together. The styles are:

BES_CIRCULAR - when the current pointer reaches either the start or end of the history, the pointer is moved to the other end of the history array.

BES_AUTOCOMPLETE - The control tries to complete the current input from the history after each keypress.

BES_SORT - The history is sorted at all times. No duplicates are added.

The history will be cleared, so this function should normally be called during startup.

void CBufferedEdit::SetBufferMax( int max )

Sets the maximum number of history strings. Set to 0 if no max is to be used. The history will be cleared, so this function should normally be called during startup.

int CBufferedEdit::GetBufferStyle()

Returns the current style of the control

int CBufferedEdit::GetBufferMax()

Returns the current max number of history entries.

Points of Interest

This was not a difficult class to write, as far as the implementation goes. Tricky was to get it to work intuitively, however. Should the text be selected when a new history string is displayed? Should the right arrow show the last history string, or the current? As it works now, it is perfectly adapted for my intended use, but it goes to show that it is perhaps not the programming itself that is a problem, but rather creating something that works in a productive manner.

History

  • 2004/06/15 - As per a suggestion from Clevedon_Peanut (thanks for the feedback!), I've added automatic handling of the ENTER-key by adding a new style, BES_AUTOFILL. If this style is set, the ENTER-key empties the edit field and adds the string to the buffer. This avoids having a default key for the updates, and thus works better for several edits on the same dialog, or in a form view.
  • 2004/04/22 - The ico and rc2 added to the res-directory, and as per a suggestion, I added a release-build exe.
  • 2004/04/16 - Added history entry max, circular buffer, sorted buffer, sorted navigation and autocompletion.
  • 2004/04/10 - Initial version.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer (Senior) Abstrakt Mekanik AB
Sweden Sweden
45 years old, married, three kids.

Started with computers more than 20 years ago on a CBM-64.

Read Theoretical Philosophy at the University of Lund.

Working as a C++ consultant developer.

Science-fiction freak. Enjoy vintage punkrock.

Comments and Discussions

 
Questioncan that be done for combo box ? Pin
Shay Harel20-Oct-06 10:31
Shay Harel20-Oct-06 10:31 
AnswerRe: can that be done for combo box ? Pin
Johan Rosengren20-Oct-06 19:55
Johan Rosengren20-Oct-06 19:55 
GeneralRe: can that be done for combo box ? Pin
Shay Harel23-Oct-06 3:15
Shay Harel23-Oct-06 3:15 
GeneralRe: can that be done for combo box ? Pin
Johan Rosengren23-Oct-06 9:01
Johan Rosengren23-Oct-06 9:01 
GeneralProblems with MSVC.NET compile Pin
amw157454-Mar-06 5:48
amw157454-Mar-06 5:48 
GeneralRe: Problems with MSVC.NET compile Pin
Johan Rosengren5-Mar-06 9:32
Johan Rosengren5-Mar-06 9:32 
QuestionHow to use in CFormView Pin
Pete Goodsall13-Jun-04 15:09
Pete Goodsall13-Jun-04 15:09 
In CFormView, when you press ENTER, the edit box doesn't clear and the text isn't added to the history list.

To solve this, look in "BufferedEdit.cpp" and find this function:
void CBufferedEdit::OnKeyDown( UINT nChar, UINT nRepCnt, UINT nFlags )

BEFORE the last line of the function, which is:
CEdit::OnKeyDown( nChar, nRepCnt, nFlags );

Add the following:

if ( nChar == VK_RETURN ) OnSetText( 0, 0 );

The class now seems to work perfectly.
Extremely useful it is too!Big Grin | :-D
AnswerRe: How to use in CFormView Pin
Johan Rosengren13-Jun-04 19:43
Johan Rosengren13-Jun-04 19:43 
Generalnice Pin
Monty222-Mar-04 18:46
Monty222-Mar-04 18:46 
GeneralAmazing - 5++ Pin
andyj11510-Mar-04 6:41
andyj11510-Mar-04 6:41 
GeneralRe: Amazing - 5++ Pin
Johan Rosengren10-Mar-04 8:11
Johan Rosengren10-Mar-04 8:11 
GeneralRe: Amazing - 5++ Pin
andyj11510-Mar-04 21:45
andyj11510-Mar-04 21:45 
GeneralRe: Amazing - 5++ Pin
Johan Rosengren10-Mar-04 22:18
Johan Rosengren10-Mar-04 22:18 
GeneralRe: Amazing - 5++ Pin
Johan Rosengren17-Mar-04 0:24
Johan Rosengren17-Mar-04 0: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.