Solved

Changing CCheckListBox/CListBox text

Posted on 1998-04-06
4
323 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
  • 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now