?
Solved

Changing CCheckListBox/CListBox text

Posted on 1998-04-06
4
Medium Priority
?
370 Views
Last Modified: 2013-11-20
I am trying to change the text in a CCheckListBox
depending on whether the box is checked or not.  I
have it working partially, but, there seems to be
a 'Reflection" problem.  To recreate the problem,
I made an MFC dialog app, inserted a listbox item,
gave it the following prperties:

Selection:Single
OwnerDraw:Fixed
HasStrings:TRUE
Sort:False

then in the dialog class I declared the control:

class CChecklistDlg : public CDialog
{
  CCheckListBox      m_ctlList;
  //etc...
};

Then in the implementation file added the following:

BEGIN_MESSAGE_MAP(CChecklistDlg, CDialog)
  ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
  // etc.
END_MESSAGE_MAP()

void CChecklistDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  DDX_Control(pDX, IDC_LIST1, m_ctlList);
}

BOOL CChecklistDlg::OnInitDialog()
{
  CDialog::OnInitDialog();
  // etc.
  CString str;
  int i;
 
  m_ctlList.SetCheckStyle(BS_AUTOCHECKBOX);
  m_ctlList.ResetContent();

  for (i=0; i < 10; i++)
  {
      str.Format("Line Number %d", i);
      m_ctlList.AddString(str);
  }
  UpdateData(FALSE);
      
  return TRUE;  
}

void CChecklistDlg::OnSelchangeList1()
{
  CString str;
  int cursel;

  UpdateData(TRUE);
  cursel = m_ctlList.GetCurSel();

  m_ctlList.GetText(cursel, str);
  m_ctlList.DeleteString(cursel);
  str.MakeReverse();
  m_ctlList.InsertString(cursel, str);
  m_ctlList.SetCheck(cursel, check);
  m_ctlList.SetCurSel(cursel);

  UpdateData(FALSE);
}

The above code works on Windows 95.  Well, unless you doubleclick a selection quickly, then the box checks and unchecks, but the text changes only once.  In WinCtrl3.cpp it says Click and DblClick should behave the same.

In Windows NT 3.51 and 4.0 checkboxes 5-9 work fine (except dbl clk problem above).  1-4 are intermittent for changing text, and item 0 causes the app to crash.

Any suggestions?

tom.
tom.fejes@sofkin.ca
0
Comment
Question by:fejes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Expert Comment

by:chacko
ID: 1317481
Could you try single/double click messages instead of selection change? You probably has to subclass the window procedure.
0
 

Author Comment

by:fejes
ID: 1317482
Thanks for your idea.

Listbox derived classes receive LB_ notification messages, not BN_ messagees.  That wouldn't solve the problem because I'd still be sending messages to the listbox and it would assert in the same place.

My current solution is to ResetContent the listbox and redraw all the list items.  Not very elegant, but good thing the list is small.  I get an assertion error in CCheckListBox:: PreDrawItem() because drawItem is -1 (MFC checks its status against NULL).  The assertion occurs just after the UpdateData(FALSE) in OnSelChange.
0
 
LVL 11

Accepted Solution

by:
mikeblas earned 50 total points
ID: 1317483

The ASSERT you get is because of a bug in Windows; the list box code in WinNT and Win9x doesn't behave the same way under the same circumstances. Sometimes, Windows NT passes a -1 with WM_DRAWITEM when Win9x wouldn't.

I worked around the issue in MFC, but I can't remember if the fix is in VC++ 5 or not. (It always helps to point out which version of the development environment you're using, as well as which version of Windows you're using to reproduce the problem.)

I thought there was a KB article on the bug, but can't find one. So maybe the fix is not available in a released version of the product. The fix appears to be in the platform update, and will certainly be in the next release of the product.

Anyway, you can work around the issue yourself by overriding OnChildNotify() and looking for the WM_DRAWITEM message.  If I remember correctly, if drawItem.itemData == 0 _or_ if drawItem.itemData == LB_ERR, you can return from the function without doing any work to avoid the error and still get correct functionality from the control.

.B ekiM

0
 

Author Comment

by:fejes
ID: 1317484
Thanks Mike.

I've decided to stick with my ResetContent solution.  It's the most readable solution that isn't version dependant.  I couldn't find any info in MSDN or the KB, only the old article you wrote last year on the MFC-L archives, basically saying "it's a bug".

BTW, I tried this on Win95 original with MSDEV 4.2, Win95 OSR 2 with MSDEV 5.0 (unpatched and patched), WinNT 3.51 original with MSDEV 4.2, WinNT 4.0 2 with MSDEV 5.0 (unpatched and patched).  Again, it worked on Win95, but neither NT, always reverting to
that -1 return problem.

0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question