Solved

Dynamically changing ListBox contents

Posted on 1999-01-06
7
240 Views
Last Modified: 2013-12-03
I have a SORTED List Box and I wish to maintain the currently highlighted selection when strings are added to or taken  away from the total list by events that are external to this subroutine

What happens in my sorted listbox is, if I have say:

string 0 = AAAA
string 1 = CCCC
string 2 = DDDD

and string CCCC is currently highlighted and I save it's index  (icaret = 1)
and a new entry comes along, this time round that is BBBB
so the saved index is out by 1, in other words when I finally do LB_SETCURSEL it highlights BBBB instead of CCCC

so do I have to do a string compare on the new entry/s (it can be multiple entries or deletions) and adjust the saved index on whether the new string/s are inserted or deleted before or after the highlighted string, or is there a simpler way to do this ????????????

// Code snippet of how the listbox gets updated

int icaret; j, iCount;
char szString [19]; // 20 possible listbox entries

// Get current selection index
icaret = SendDlgItemMessage(hdlg,
IDC_USER_LIST,
LB_GETCARETINDEX,
0,
0);

// Clear Listbox for new update
SendDlgItemMessage(hdlg,
IDC_USER_LIST,
LB_RESETCONTENT,
0,
0);
      
// There is an ORDERED array of strings that get entries  added to or subtracted from triggered by external events
// The current count of strings is iCount and varies from time to time

for(j = 0; j < iCount; j++) {
SendDlgItemMessage(hdlg,
IDC_USER_LIST,
LB_ADDSTRING,
(WPARAM) 0,
(LPARAM) szString[j]);
}

// Reset current selection highlight
SendDlgItemMessage(hdlg,
IDC_USER_LIST,
LB_SETCURSEL,
icaret,
(LPARAM) 0);
0
Comment
Question by:putu
[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
  • 4
  • 3
7 Comments
 

Author Comment

by:putu
ID: 1418138
// Get current selection index
icaret = SendDlgItemMessage(hdlg,
IDC_USER_LIST,
LB_GETCARETINDEX,
0,
0);

//  Probably should be
 
// Get current selection index
icaret = SendDlgItemMessage(hdlg,
IDC_USER_LIST,
LB_GETCURSEL,
0,
0);
0
 
LVL 12

Expert Comment

by:migel
ID: 1418139
Hi!
I see two way to solve
1.
You can add a order data to the ListBox item (By LB_SETITEMDATA message)
befor Adding new srtring you can get order data from the currently selected item and after adding you can loop through all items and find order data and select corresponding item.
2. AddString return an index of the inserted element. You can compare it with the saved (selected item before adding) one and set selection depends on the comparation. That is:
 if( nAddedIndex > nSaved)
  nSelIndex = nSaved
else
 nSelIndex = nSaved+1;
0
 

Author Comment

by:putu
ID: 1418140
migel

I like both of your suggestions and see the concepts.

can you give me some demonstration code ??

I have increased the points and will gladly give you excellent credits if you can.

thanks,
putu

0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:putu
ID: 1418141
Re: solution 2 Addstring, what if an item is removed before (sort wise) from the selected item, so effectively it's index becomes 1 less and the highlight will be the one after the actual item ??

I like solution 1 where i can associate a unique identifier with the selected item and search for it

what do you think??


0
 
LVL 12

Expert Comment

by:migel
ID: 1418142
Ok!
Way 1.)
//step 1 adding string to the listbox
UINT uListIndex - 0;
for(j = 0; j < iCount; j++)
{
uListIndex =
   SendDlgItemMessage(hdlg,     IDC_USER_LIST,     LB_ADDSTRING,    (WPARAM) 0,
    (LPARAM) szString[j]);
   SendDlgItemMessage(hdlg,     IDC_USER_LIST,     LB_SETITEMDATA,   uListIndex, j);
    }

//Step 2...adding extra data
UINT uLastIndex =    SendDlgItemMessage(hdlg,     IDC_USER_LIST,     LB_GETCOUNT,    0, 0);
UINT uCurSel = SendDlgItemMessage(hdlg,     IDC_USER_LIST,     LB_GETCURSEL,    0, 0);
uCurSel = (UINT)SendDlgItemMessage(hdlg,     IDC_USER_LIST,     LB_GETITEMDATA,    uCurSel, 0);
uListIndex =
   SendDlgItemMessage(hdlg,     IDC_USER_LIST,     LB_ADDSTRING,    (WPARAM) 0,
    (LPARAM) strToAdd);
   SendDlgItemMessage(hdlg,     IDC_USER_LIST,     LB_SETITEMDATA,   uListIndex, uLastIndex);
//Step 3 change selection
for (i = 0; i < uLastIndex+1; i++)
{
if (uCurSel == (UINT)SendDlgItemMessage(hdlg,     IDC_USER_LIST,     LB_GETITEMDATA,    i, 0))
{
SendDlgItemMessage(hdlg,     IDC_USER_LIST,     LB_SETCURSEL,    i, 0);
break;
}
}

Way 2.)
// Step 1 is same ...
// Step 2
UINT uCurSel = SendDlgItemMessage(hdlg,     IDC_USER_LIST,     LB_GETCURSEL,    0, 0);
uListIndex =
   SendDlgItemMessage(hdlg,     IDC_USER_LIST,     LB_ADDSTRING,    (WPARAM) 0,
    (LPARAM) strToAdd);
// step 3.
if (uListIndex <= uCurSel)
 uCurSel++;
SendDlgItemMessage(hdlg,     IDC_USER_LIST,     LB_SETCURSEL, uCurSel, 0);

0
 

Author Comment

by:putu
ID: 1418143
Excellent answer migel, so can you make your comment added the answer and I will award you the points,
thanks,
putu
0
 
LVL 12

Accepted Solution

by:
migel earned 130 total points
ID: 1418144
Good luck!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

726 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