Thread: Shuffling Through Listbox

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    7

    Shuffling Through Listbox

    Is there a better way to Shuffle through a listbox then depending on SendMessage? Like getting the Count of items....Getting text.... removing items...

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by xJJx View Post
    Is there a better way to Shuffle through a listbox then depending on SendMessage? Like getting the Count of items....Getting text.... removing items...
    That's simply how you do any of those things with a listbox, you send it Windows messages. You can make your own nice little wrapper function sthat do the above things for you, or use a library that provides such wrapper functions, but there isn't any other way to do those things besides sending Windows messages to the control.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    7
    Ok thats kinda what i figured. One problem I'm having shuffling threw my listbox is sometimes it doesnt return a value. Do you think u could take a look?

    Code:
    char buf[300]; std::string tmp = "";
    memset(buf,0,sizeof(buf));
    HWND active = GetDlgItem(ListHwnd,ID_LBACTIVE);
    HWND disabled = GetDlgItem(ListHwnd, ID_LBDISABLED);
    GetWindowText(GetDlgItem(SettingHwnd,ID_REACTEDIT),buf,300);
    	while(tmp == "")
    	{
    		if(SendMessage(active,LB_GETCOUNT,0,0) <= atoi(buf))
    		{
    			memset(buf,0,sizeof(buf));
    			while(SendMessage(disabled,LB_GETCOUNT,0,0) > 0)
    			{
    				SendMessage(disabled,LB_GETTEXT,0,(LPARAM)buf);
    				SendMessage(active,LB_ADDSTRING,0,(LPARAM)buf);
    				SendMessage(disabled,LB_DELETESTRING,0,0);
    			}
    			ProxyCount = 1;
    			SendMessage(active,LB_GETTEXT,0, (LPARAM)buf);
    			tmp = buf;
    		}else{
    			int count = SendMessage(active,LB_GETCOUNT,0,0);
    			memset(buf,0,sizeof(buf));
    			if(ProxyCount == ( count - 1))
    			{
    				ProxyCount = 0;
    				SendMessage(active,LB_GETTEXT,count - 1,(LPARAM)buf);
    				tmp = buf;
    			}else{
    				ProxyCount += 1;
    				SendMessage(active,LB_GETTEXT,ProxyCount - 1, (LPARAM)buf);
    				tmp = buf;
    			}
    		}
    	}
    return tmp;
    This functions job is to return a proxy from the active listbox and if the active listbox's coutn is less then the value in my edit box, return all values from the disabled listbox to the active list box and return the next in line proxy.

    Edit: I added the while loop to prevent it from returning "" until ii could find the problem

  4. #4
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    are you using MSVC++?
    then, why not trying MFC...?

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    7
    Yes i am using VC++ and it is a dialog based application. This is the code from my GetNewProxy function

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Add some error checking, windows is a fickle beast and likes to throw random errors, just to keep you on your toes...

    GetLastError() if a WIN API call fails, vey useful for debugging

    For example, if LB_GETCOUNT fails it returns LB_ERR, which is defined as -1 in my SDK. That would have a negative impact on your app....
    [atoi() returns zero if it can't process the text, so your loop would be 'if(-1 < 0 )']

    LB_GETTEXT returns the number of char copied to the buf. You should check this to make sure you want to add the string, it may still hold the last value.

    Code:
    //this has multiple points of failure 
    //and no way to know if that fail will cause issues some where else
    GetWindowText(GetDlgItem(SettingHwnd,ID_REACTEDIT),buf,300);
    
    //I suggest something more like
    int iSuccess=0,iCount=-1;
    char szBuf[MAX_PATH]={0};
    
    iCount=GetDlgItemInt (SettingHwnd,ID_REACTEDIT,&iSuccess,FALSE);
    //test for success as zero is a vaild count && the return for a fail
    if(!iSuccess)
    {
    
    //first thing find out why it failed iSuccess=GetLastError(); //log it failed _snprinf(szBuf,MAX_PATH-1,"Get count in REACT edit failed with error#%d.",iSuccess); //msgbox or file MessageBox(NULL,szBuf,"My App Error",MB_ICONERROR|MB_OK); //handle fail return -1;
    } //we got a count, but are there any items? if(iCount) {
    I also set the edit to ES_NUMBER to restrict user input (but your looks app set).
    Last edited by novacain; 01-23-2009 at 09:53 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deal or No Deal listbox prob
    By kryptkat in forum Windows Programming
    Replies: 5
    Last Post: 03-30-2009, 06:53 PM
  2. Listbox search
    By Elsindes in forum Windows Programming
    Replies: 4
    Last Post: 04-09-2007, 03:47 PM
  3. How to cast a ListBox item to an int for a switch statment?
    By Swaine777 in forum C++ Programming
    Replies: 8
    Last Post: 09-26-2004, 08:52 PM
  4. Listbox stealing focus
    By Calthun in forum Windows Programming
    Replies: 3
    Last Post: 09-12-2004, 04:36 PM
  5. Getting FULL filename from listbox
    By Garfield in forum Windows Programming
    Replies: 8
    Last Post: 01-27-2002, 08:28 AM