Solved

Changing CCheckListBox/CListBox text

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 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