Solved

Dynamically changing ListBox contents

Posted on 1999-01-06
7
231 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
  • 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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.  …
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

758 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

18 Experts available now in Live!

Get 1:1 Help Now