Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to put checkboxes in listbox

512 views
Skip to first unread message

Brian Korinek

unread,
Aug 9, 1996, 3:00:00 AM8/9/96
to

Ideally I would like to place a check next to text for items a user selects in a listbox. Does
anyone know of any sample source that would show me how. BTW this code must be 32bit and run
under Win32s using Visual C++ 4.X. Someone said that it could be done without using owner draw
but I am skeptical.

Thanks for any help,

Brian Korinek
bri...@eurekasoft.com

Cecil A. Galbraith

unread,
Aug 9, 1996, 3:00:00 AM8/9/96
to Brian Korinek

See CCheckListBox in your documentation - it's pretty straightforward.

--
Cecil Galbraith
cgal...@concentric.net
CustomSoft

Dan Vallejo <Microsoft>

unread,
Aug 9, 1996, 3:00:00 AM8/9/96
to

CCheckListBox
--

Dan Vallejo <Dan...@Microsoft.com>
Software Development Engineer, Personal Finance, Money
The opinions expressed in this message are my own personal views and do not
reflect the official views of Microsoft Corporation.

Brian Korinek <bri...@eurekasoft.com> wrote in article
<320B63...@eurekasoft.com>...

John Grant

unread,
Aug 10, 1996, 3:00:00 AM8/10/96
to

In article <320B6F...@concentric.net> "Cecil A. Galbraith" <cgal...@concentric.net> writes:

>Brian Korinek wrote:
>>
>> Ideally I would like to place a check next to text for items a user selects in a listbox. Does
>> anyone know of any sample source that would show me how. BTW this code must be 32bit and run
>> under Win32s using Visual C++ 4.X. Someone said that it could be done without using owner draw
>> but I am skeptical.
>
>See CCheckListBox in your documentation - it's pretty straightforward.
What is the API equivalent of that? I've never run into it before.
Does it correspond to a Win32-specific feature of a listbox?
What does the MFC source do?

To the original poster... what's wrong with the usual selection
markings you get in a listbox (COLOR_HIGHLIGHT & COLOR_HIGHLIGHTTEXT)?
--
John A. Grant jag...@nrcan.gc.ca
Airborne Geophysics, Geological Survey of Canada, Ottawa
If you followup, please do NOT e-mail me a copy: I will read it here.

Cecil A. Galbraith

unread,
Aug 10, 1996, 3:00:00 AM8/10/96
to John Grant

There isn't an API equivalent of that. CCheckListBox is a class that is
derived from CListBox (which is where the API come in...)

I am including some source code that I came up with which you can play
with. However, I should warn you that the code will assert in debug mode
for a reason that I haven't discovered. It works fine in release mode...

BOOL CTestDlg::OnInitDialog()
{

// in CTestDlg.h file - CCheckListBox * pList;
// in CTestDlg.cpp constructor - pList = new CCheckListBox;
// in CTestDlg.cpp destructor - delete pList;

// call base class first

CDialog::OnInitDialog();

// created using resource file a standard CListBox control with the
following attributes
// only the first two are required

// LBS_OWNERDRAWFIXED - omitting this one causes nothing to appear
inside listbox
// LBS_HASSTRINGS - omitting this one causes check boxes to appear,
but trash instead of strings
// LBS_NOINTEGRALHEIGHT
// LBS_DISABLENOSCROLL
// WS_VSCROLL
// WS_TABSTOP
// WS_EX_CLIENTEDGE

// CCheckListBox will use the LBS_OWNERDRAWFIXED by default if
// LBS_OWNERDRAWVARIABLE is not specified

pList->Create(NULL,(CRect)NULL,this,IDC_LISTTEST);

// subclassing is necessary because CCheckListBox is derived from
// CListBox which has it's own message map

pList->SubclassDlgItem(IDC_LISTTEST,this);

pList->SetCheckStyle(BS_AUTOCHECKBOX);

pList->AddString("test1");
pList->AddString("test2");
pList->AddString("test3");
pList->AddString("test4");
pList->AddString("test5");
pList->AddString("test6");
pList->AddString("test7");
pList->AddString("test8");
pList->AddString("test9");
pList->AddString("test10");
pList->AddString("test11");
pList->AddString("test12");
pList->AddString("test13");
pList->AddString("test14");
pList->AddString("test15");
pList->AddString("test16");
pList->AddString("test17");
pList->AddString("test18");


return TRUE;

Chris Marriott

unread,
Aug 10, 1996, 3:00:00 AM8/10/96
to

In article <DvwJC...@emr1.NRCan.gc.ca>, John Grant
<jag...@emr1.NRCan.gc.ca> writes

> What is the API equivalent of that? I've never run into it before.
> Does it correspond to a Win32-specific feature of a listbox?
> What does the MFC source do?

CCheckListBox is a very useful MFC class - it's a subclassed "owner
draw" listbox which provides a list of checked fields - ie it's
basically a list of checkboxes. Extremely useful.

Chris

--------------------------------------------------------------------------
Chris Marriott, Warrington, UK | Author of SkyMap v3 award-winning
ch...@chrism.demon.co.uk | shareware Win31/Win95 planetarium.
For full info, see http://www.execpc.com/~skymap
Author member of Association of Shareware Professionals (ASP)
--------------------------------------------------------------------------


Matt Arnold

unread,
Aug 11, 1996, 3:00:00 AM8/11/96
to

jag...@emr1.NRCan.gc.ca (John Grant) writes:

>In article <320B6F...@concentric.net> "Cecil A. Galbraith" <cgal...@concentric.net> writes:
>>Brian Korinek wrote:
>>>
>>> Ideally I would like to place a check next to text for items a user selects in a listbox. Does
>>> anyone know of any sample source that would show me how. BTW this code must be 32bit and run
>>> under Win32s using Visual C++ 4.X. Someone said that it could be done without using owner draw
>>> but I am skeptical.

I probably seems like *you* aren't using an owner-drawn listbox when you
let MFC do it for you! But, the stock Windows LISTBOX control does not
have a way to show checkboxes (or any other custom image), without it
being an owner-drawn one. So, that's what MFC must be doing.

>>See CCheckListBox in your documentation - it's pretty straightforward.

There you go, I bet the CCChekcListBox MFC class just creates an owner-
drawn listbox and takes care of showing the checkboxes for each item.

> What is the API equivalent of that? I've never run into it before.
> Does it correspond to a Win32-specific feature of a listbox?

I don't think so. There are no new styles for the LISTBOX control in
Win32 (actually, there are, LBS_NOSEL and LBS_NODATA, but they obviously
have nothing to do with checkboxes).

However, I suppose a TreeView showing only one level of nodes with no
lines, all of which have checkbox-like images next to them could appear
like a "check/listbox".

> What does the MFC source do?

It MUST either create an owner-drawn listbox, possibly use a TreeView
control masquerading as a listbox or it draws the whole thing (unlikely).

> To the original poster... what's wrong with the usual selection
> markings you get in a listbox (COLOR_HIGHLIGHT & COLOR_HIGHLIGHTTEXT)?

True, that was the way a LISTBOX was originally intended to show
multiple selections, but that's simply is not as intuitive as a list of
items with checkmarks next to them. I think a check-listbox, if you can
use one, is much better than a plain old listbox with the LBS_MULTISELECT
style.

Regards,
-------------------------------------------------------------------------
Matt Arnold | | ||| | |||| | | | || ||
mar...@netcom.com | | ||| | |||| | | | || ||
Boston, MA | 0 | ||| | |||| | | | || ||
617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
-------------------------------------------------------------------------

Cecil A. Galbraith

unread,
Aug 12, 1996, 3:00:00 AM8/12/96
to Will Flor

Will Flor wrote:

>
> Cecil A. Galbraith wrote:
> >
> > Brian Korinek wrote:
> > >
> > > Ideally I would like to place a check next to text for items a user selects in a listbox. Does
> > > anyone know of any sample source that would show me how. BTW this code must be 32bit and run
> > > under Win32s using Visual C++ 4.X. Someone said that it could be done without using owner draw
> > > but I am skeptical.
> > >
> > > Thanks for any help,
> > >
> > > Brian Korinek
> > > bri...@eurekasoft.com
> >
> > See CCheckListBox in your documentation - it's pretty straightforward.
> >
>
> Does CCheckListBox function correctly under Win32s, which Brian
> specifically asked about??
>
> I'm fairly sure that it is specific to the newer MFC, which
> does not run under Win32s.
>
> -Will Flor wi...@rrgroup.com

Man, I don't know... I thought that it was fairly straightforward until
I tried it myself (32 bit). I'd like to hear from anyone who has gotten
CCheckListBox to work without asserts in debug mode... I can get it to
work in release with no problem, but in assert I believe that there is a
problem with calling create on the CCheckListBox object which instantly
calls create on the base class CListBox. Trouble is, a list box control
has already been created using the same ID before that happens! There is
something that I'm missing, here...

Mike Blaszczak

unread,
Aug 12, 1996, 3:00:00 AM8/12/96
to

In article <320F77...@rrgroup.com>, wi...@rrgroup.com says...

>Does CCheckListBox function correctly under Win32s, which Brian
>specifically asked about??

I haven't heard any problems about CCheckListBox with Win32s.

>I'm fairly sure that it is specific to the newer MFC, which
>does not run under Win32s.

CCheckListBox was in MFC 4.0 and MFC 4.1, and is in MFC 4.2.
The only one of these that won't run under Win32s is MFC 4.2.

.B ekiM
--
http://www.nwlink.com/~mikeblas/

Ron Forrester

unread,
Aug 12, 1996, 3:00:00 AM8/12/96
to Cecil A. Galbraith

"Cecil A. Galbraith" <cgal...@concentric.net> writes:

> Man, I don't know... I thought that it was fairly straightforward until
> I tried it myself (32 bit). I'd like to hear from anyone who has gotten
> CCheckListBox to work without asserts in debug mode... I can get it to
> work in release with no problem, but in assert I believe that there is a
> problem with calling create on the CCheckListBox object which instantly
> calls create on the base class CListBox. Trouble is, a list box control
> has already been created using the same ID before that happens! There is
> something that I'm missing, here...

You have to turn on a specific flag in the resource for the listbox. I
don't remember which one, but here are the ones I have in one that
works for me:

LBS_OWNERDRAWFIXED | LBS_HASSTRINGS | LBS_USETABSTOPS |
LBS_EXTENDEDSEL | LBS_DISABLENOSCROLL | WS_VSCROLL |
WS_GROUP | WS_TABSTOP


I think its either LBS_OWNERDRAWFIXED or LBS_HASSTRINGS...

rjf

--
"...fame is a cancer and ego its seed...",Toad the Wet Sprocket
For pgp key, finger r...@infograph.com

Mike Blaszczak

unread,
Aug 12, 1996, 3:00:00 AM8/12/96
to

In article <320F93...@concentric.net>, cgal...@concentric.net says...

>Man, I don't know... I thought that it was fairly straightforward until
>I tried it myself (32 bit). I'd like to hear from anyone who has gotten
>CCheckListBox to work without asserts in debug mode.

CCheckListBox is easy to use, if you get the right styles on the
resource. My book outlines most of the rules. I informed the
documentation team about the docs not mentioning which styles should
be used when, and I think they got the fixes into the 4.2 fixes.

What assertions are you getting, exactly? What line and file are
they on? Which vesion of MFC are you using?

>I can get it to
>work in release with no problem, but in assert I believe that there is a
>problem with calling create on the CCheckListBox object which instantly
>calls create on the base class CListBox. Trouble is, a list box control
>has already been created using the same ID before that happens! There is
>something that I'm missing, here...

Yes, there is. You should _subclass_ the existing list box control.
Since the control window already exists (because it was created by
the Windows dialog manager), there's no reason for you to call
Create() against CCheckListBox() unless you want to create a brand-new
control.

So, if you have some list control on your dialog named IDC_LIST1,
you should add this code to your OnInitDialog():

m_wndCheckedBox.SubclassDlgItem(IDC_LIST1, this);

and add a "CCheckListBox m_wndCheckedBox;" member to your CDialog
class. If you use SubclassDlgItem() in this way, _DO NOT_ use
a DDX_Control() call against m_wndCheckedBox.

Cecil A. Galbraith

unread,
Aug 13, 1996, 3:00:00 AM8/13/96
to Mike Blaszczak

Aha! That solves the assert problem!

<<If you use SubclassDlgItem() in this way, _DO NOT_ use
> a DDX_Control() call against m_wndCheckedBox.>>

That solved the problem - thanks. Incidentally, I have no immediate use
for this class - what started this with me was a the same question from
two people in this newsgroup. I fell into the same trap that they did
from the documentation that says to create a listbox from either
resources oor in your code and say in BOTH cases call the constructor
and then the Create member for CCheckListBox.

Had I gone with what I knew instead of blindly following instructions,
this wouldn't have happened....

Well, thanks again ...

Charlie Poole

unread,
Aug 13, 1996, 3:00:00 AM8/13/96
to

"Cecil A. Galbraith" <cgal...@concentric.net> wrote:

>Will Flor wrote:
>>
>> Cecil A. Galbraith wrote:
>> >
>> > Brian Korinek wrote:
>> > >
>> > > Ideally I would like to place a check next to text for items a user selects in a listbox. Does
>> > > anyone know of any sample source that would show me how. BTW this code must be 32bit and run
>> > > under Win32s using Visual C++ 4.X. Someone said that it could be done without using owner draw
>> > > but I am skeptical.
>> > >
>> > > Thanks for any help,
>> > >
>> > > Brian Korinek
>> > > bri...@eurekasoft.com
>> >
>> > See CCheckListBox in your documentation - it's pretty straightforward.
>> >
>>

>> Does CCheckListBox function correctly under Win32s, which Brian
>> specifically asked about??
>>

>> I'm fairly sure that it is specific to the newer MFC, which
>> does not run under Win32s.
>>

>> -Will Flor wi...@rrgroup.com


>
>Man, I don't know... I thought that it was fairly straightforward until
>I tried it myself (32 bit). I'd like to hear from anyone who has gotten

>CCheckListBox to work without asserts in debug mode... I can get it to

>work in release with no problem, but in assert I believe that there is a
>problem with calling create on the CCheckListBox object which instantly
>calls create on the base class CListBox. Trouble is, a list box control
>has already been created using the same ID before that happens! There is
>something that I'm missing, here...
>
>

>--
>Cecil Galbraith
>cgal...@concentric.net
>CustomSoft

Presumably, it already exists because you have created it in your
dialog template. In that case, you don't want to create a new one.
Rather you want to subclass the existing control. You can either do
that "manually" using CWnd::SubclassDlgItem, or let the ClassWizard do
it for you by associating a CCheckListBox variable with the item.
---------------------------------------------------------------
Charlie Poole cpo...@kendaco.telebyte.com
Poulsbo, Washington USA
---------------------------------------------------------------

Mathew Dredge

unread,
Aug 14, 1996, 3:00:00 AM8/14/96
to

In article <320F93...@concentric.net>, "Cecil A. Galbraith" <cgal...@concentric.net> wrote:
>Will Flor wrote:
>>
>> Cecil A. Galbraith wrote:
>> >
>> > Brian Korinek wrote:
>> > >
>> > > Ideally I would like to place a check next to text for items a user
> selects in a listbox. Does
>> > > anyone know of any sample source that would show me how. BTW this code
> must be 32bit and run
>> > > under Win32s using Visual C++ 4.X. Someone said that it could be done
> without using owner draw
>> > > but I am skeptical.

Its easily done, look on the web for Mark G's Win32 Programming page, but you
might have problems understanding it as its written in plain C.. Now I did it
and a editable list box class on OO but that was using ObjectWindows, the real
OO for windows..

Never Mind..


Mathew Dredge
CSE (NZ) Ltd.
Software Developer
---------------------------------------------
E-Mail: mat...@wave.co.nz
URL: http://www.wave.co.nz/pages/mdredge
Phone: +64-7-838 2010
Fax: +64-7-838 0977
---------------------------------------------

David Connet

unread,
Aug 21, 1996, 3:00:00 AM8/21/96
to

In article <4uo42l$2...@news.microsoft.com>,

mike...@microsoft.com (Mike Blaszczak) wrote:
>In article <320F77...@rrgroup.com>, wi...@rrgroup.com says...
>
>>Does CCheckListBox function correctly under Win32s, which Brian
>>specifically asked about??
>
>I haven't heard any problems about CCheckListBox with Win32s.

I don't know if it's been fixed yet (I can't upgrade to 4.2 due to win32s
support), but in 4.1 there was a bug (which I reported - all I heard back was
the automated mail reply) in the ownerdrawvariable variant in the
OnLButtonDown callback.

But this was not specific to win32s, it affected all systems. The behavior
was that if you click lower than the last entry in the list, the for loop in
the lbuttondown handler went into an infinite loop. The test in the for loop
should be an AND, not an OR (which also requires initializing the CRect then).
What happens is that the bottom of the last item is always less the the
clicked point. Since it's an OR, the index just keeps incrementing beyond
GetCount().

--------------------
Dave Connet
Dave_...@krinfo.com

My opinions are my own. I think...

0 new messages