Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamically changing ListBox contents

Posted on 1999-01-06
7
Medium Priority
?
245 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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 520 total points
ID: 1418144
Good luck!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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…
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 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: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

721 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