Solved

Dynamically changing ListBox contents

Posted on 1999-01-06
7
244 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 Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

636 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