Solved

Dynamically changing ListBox contents

Posted on 1999-01-06
7
236 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

776 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