Click here to Skip to main content
15,888,816 members
Articles / Desktop Programming / MFC
Article

Editable Table Control

Rate me:
Please Sign up or sign in to vote.
4.84/5 (26 votes)
19 Aug 2001 204.1K   8.4K   64   28
An editable combo list control

Sample Image - editable_list.jpg

Introduction

I had to implement some kind of list representation of a certain product-sell table (Product | Price | ProductID | Amount ). So, I decided to use ListCtrl for that matter. Basically, ListCtrl is fun to play with. One of the styles supported by ListCtrl is called report-view, which is used here. ListCtrl also allow editable entries as one of its styles. Unfortunately, when dealing with report-view, editing is applied only to the first column‘s elements.

My Solution

The solution was to create an Edit window on-demand, on top of the according cell-entry. Then, when done editing, a notification message is sent to parent ListCtrl. Thinking this idea is simple enough, I have also perfected it to create ComboBox on demand for certain cells to narrow the input options ( Such as Product field, which is out of the predefined list of elements ).

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
Israel Israel
Nir Shilon -
B.Sc. in Mathematics & Computer Science of Tel-Aviv University.
I have worked for the past 2 years in DSPG as a R&D software engineer.
As part of Debugger/Simulator team ( Myself, Anna & Yair).
I basically taught myself MFC and other Win32 programming stuff.

Comments and Discussions

 
GeneralMy vote of 2 Pin
China_NewComer18-Nov-13 5:49
China_NewComer18-Nov-13 5:49 
QuestionScroll bar is black before clicking it Pin
bfm8923-Jun-13 16:28
bfm8923-Jun-13 16:28 
AnswerRe: Scroll bar is black before clicking it Pin
VincyChan25-May-15 15:07
professionalVincyChan25-May-15 15:07 
Questionedit other columns Pin
peoria12330-Apr-13 5:38
peoria12330-Apr-13 5:38 
AnswerRe: edit other columns Pin
wy73722-May-13 12:22
wy73722-May-13 12:22 
GeneralMy vote of 5 Pin
frankagra29-Nov-11 11:29
frankagra29-Nov-11 11:29 
QuestionHow to do sorting Pin
chevu6-Jul-09 19:21
chevu6-Jul-09 19:21 
QuestionCBS_DROPDOWN err Pin
bala_potty25-Apr-07 4:30
bala_potty25-Apr-07 4:30 
AnswerRe: CBS_DROPDOWN err Pin
Rolf Kristensen9-Sep-08 0:34
Rolf Kristensen9-Sep-08 0:34 
GeneralRe: CBS_DROPDOWN err Pin
bala_potty15-Sep-08 20:17
bala_potty15-Sep-08 20:17 
GeneralThank you very much!!!! Pin
luoky20-Nov-06 23:23
luoky20-Nov-06 23:23 
GeneralI found a bug. Pin
rotaerc4-Jan-05 15:32
rotaerc4-Jan-05 15:32 
GeneralShort lists, no scoll bars, autoset drop down height Pin
Jordan Walters21-Sep-04 10:59
Jordan Walters21-Sep-04 10:59 
If you have a very short list to display in the drop - say only 2 items - then the presence of the horizontal scroll at the bottom tends to obscure much of the fist item and all of the second item. Of course one could use the vertical scroller to scroll up and down between the items, but have you seen the size of the scroll buttons?

My proposal is that if there are not so many items in, say less than 10, then set the style as....

<code>
if(lstItems.GetCount() < 10)
dwStyle = WS_BORDER|WS_CHILD|WS_VISIBLE |CBS_DROPDOWNLIST|CBS_DISABLENOSCROLL;
else
dwStyle = WS_BORDER|WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_HSCROLL |CBS_DROPDOWNLIST|CBS_DISABLENOSCROLL;
</code>

And then one can use the autosizing function (which must be provided by oneself) to adjust the heoight of the combobox to show all items in the list....
<code> if(lstItems.GetCount() < 10)
CDialogHelpers::SetComboBoxDropDownHeight(pList);</code>

where the function <code>CDialogHelpers::SetComboBoxDropDownHeight()</code> is implemented as follows.

<code>
void CDialogHelpers::SetComboBoxDropDownHeight(CComboBox *box, UINT nLinesToDisplay /* = -1 */)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());

ASSERT(IsWindow(*box)); // Window must exist or SetWindowPos won't work

if(nLinesToDisplay == -1)
{
nLinesToDisplay = box->GetCount();
}

CRect cbSize; // current size of combo box
int Height; // new height for drop-down portion of combo box

box->GetClientRect(cbSize);
Height = box->GetItemHeight(-1); // start with size of the edit-box portion
Height += box->GetItemHeight(0) * (nLinesToDisplay + 1); // add height of lines of text

// Note: The use of SM_CYEDGE assumes that we're using Windows '95
// Now add on the height of the border of the edit box
Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges

// The height of the border of the drop-down box
Height += GetSystemMetrics(SM_CYEDGE) * 2; // top & bottom edges

// now set the size of the window
box->SetWindowPos(NULL, // not relative to any other windows
0, 0, // TopLeft corner doesn't change
cbSize.right, Height, // existing width, new height
SWP_NOMOVE | SWP_NOZORDER // don't move box or change z-ordering.
);
}
</code>

Excuse the use of the magic number 10 above, this would be replaced with a <code>#define</code> or <code>const</code> of course.

Hope this minor improvements helps any others out there who are frustrated by short lists appearing too small.

Jordan
Generalquestion about ListCtrl EditBox and ComboBox Pin
alla_hpl27-Jan-04 3:22
alla_hpl27-Jan-04 3:22 
QuestionHow to use Editable List Control in CFrameView WIndow Pin
T R Raghavendra6-Dec-03 2:28
sussT R Raghavendra6-Dec-03 2:28 
GeneralProblem with scrolling Pin
Cabbi21-Apr-03 21:08
Cabbi21-Apr-03 21:08 
GeneralRe: Problem with scrolling Pin
raba1621-Oct-03 5:53
raba1621-Oct-03 5:53 
GeneralRe: Problem with scrolling Pin
tom.schultz29-Mar-05 19:15
tom.schultz29-Mar-05 19:15 
GeneralRe: Problem with scrolling Pin
wireman24-Jun-05 12:14
wireman24-Jun-05 12:14 
QuestionHow to suppress a combo box when uses Checboxes? Pin
yummi28-Jan-03 7:43
yummi28-Jan-03 7:43 
GeneralAuthor's response to supid Pin
shilonir24-Aug-01 1:41
shilonir24-Aug-01 1:41 
GeneralWhen in Dialog the easier way Pin
22-Aug-01 2:49
suss22-Aug-01 2:49 

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.