Solved

Changing CCheckListBox/CListBox text

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
repeateFront java challenge 31 87
how to monitor remote shell execution on linux 9 92
maxBlock challenge 30 99
changeXy challenge 13 57
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: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

17 Experts available now in Live!

Get 1:1 Help Now