Thread: C - Win32 problem writing in ListBox

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    9

    C - Win32 problem writing in ListBox

    Hi all,


    I have a generic WndProc() func running and I have a Dialog Box with a ListBox in it.
    I enter a Menu Item to open the Dialog Box and enter a specific function I created to fill the ListBox
    Code:
            BOOL CALLBACK FillListBox(HWND hWnd, UINT messg, WPARAM wParam, LPARAM lParam);
    The problem i'm facing, is the fact that while I have the Dialog Box opened, it keeps revisiting the WndProc() function and coming back to the FillListBox() in an infinit cycle, causing it to fill the ListBox numerous times.

    Is there a better way to treat the ListBox in the Dialog Box, or a way to only enter the FillListBox() function once?

    Thanks in advance,

    keyboarder
    P.S. I tried creating a modal and non-modal Dialog Box and it still cycles.
    Last edited by keyboarder; 08-30-2010 at 11:02 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Can you show us more than just the function prototype?
    There's not enough to go on...

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    9
    Here's how I open the DialogBox:

    Code:
    hDialog = CreateDialog(hInstance, MAKEINTRESOURCE(ID_DIALOG_TOP10), hWnd, (DLGPROC) Top10 );
    					ShowWindow(hDialog,SW_SHOW);

    This is the funcion. when the dialog box is opened, it keeps writing into the ListBox

    Code:
    BOOL CALLBACK Top10(HWND hWnd, UINT messg, WPARAM wParam, LPARAM lParam){
    	int i = 0;
    
    	for(i=0;i<10;i++){
    		SendDlgItemMessage(hWnd, IDC_LIST1,LB_ADDSTRING,i,names[i]);
    	}
    
    	switch (messg) 
    	{
    		case WM_COMMAND:
    			if(LOWORD(wParam) == IDSAIR){
    				EndDialog(hWnd,0);
    				DestroyWindow(hWnd);
    			}
    			break;
    
    		case WM_CLOSE:
    			EndDialog(hWnd,0);
    			DestroyWindow(hWnd);
    			break;
    		default:
    			break;
    	}
    	return 0;
    }
    The names I used previously were generic names. These are the actual ones

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by keyboarder View Post
    Here's how I open the DialogBox:

    Code:
    hDialog = CreateDialog(hInstance, MAKEINTRESOURCE(ID_DIALOG_TOP10), hWnd, (DLGPROC) Top10 );
                        ShowWindow(hDialog,SW_SHOW);
    This is the funcion. when the dialog box is opened, it keeps writing into the ListBox

    Code:
    BOOL CALLBACK Top10(HWND hWnd, UINT messg, WPARAM wParam, LPARAM lParam){
        int i = 0;
    
        for(i=0;i<10;i++){
            SendDlgItemMessage(hWnd, IDC_LIST1,LB_ADDSTRING,i,names[i]);
        }
    
        switch (messg) 
        {
            case WM_COMMAND:
                if(LOWORD(wParam) == IDSAIR){
                    EndDialog(hWnd,0);
                    DestroyWindow(hWnd);
                }
                break;
    
            case WM_CLOSE:
                EndDialog(hWnd,0);
                DestroyWindow(hWnd);
                break;
            default:
                break;
        }
        return 0;
    }
    The names I used previously were generic names. These are the actual ones
    Well, your dialog procedure sends 10 messages to itself, which in turn generates 10 more messages, ad infinitum. Hint: You can control the update of the list box by monitoring the messages (ie: WM_XXX) sent to your callback function.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    9
    Got it Thank you very much!

    And what is the tipe of message? What do I put in the XXXX part of WM_XXXX to treat these messages?

    Thanks again for the help

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by keyboarder View Post
    And what is the tipe of message?
    Think about it. What message is sent to a dialog window when it is created?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    9
    I guess WM_CREATE
    ok gonna try it thanks for the teaching method

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by keyboarder View Post
    I guess WM_CREATE
    ok gonna try it
    Nope. Dialog boxes don't process that one. Try again!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    9
    WM_INITDIALOG

    thanks google

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by keyboarder View Post
    Here's how I open the DialogBox:
    This is the funcion. when the dialog box is opened, it keeps writing into the ListBox
    Because it's right in the callback and is executed everytime windows stends a message.


    Code:
    BOOL CALLBACK Top10(HWND hWnd, UINT messg, WPARAM wParam, LPARAM lParam){
    	switch (messg) 
    	{
     
    // see here
                      case WM_INITDIALOG :
                                for(int i=0;i<10;i++)
                                    SendDlgItemMessage(hWnd, IDC_LIST1,LB_ADDSTRING,i,names[i]);
                                    return TRUE;
    
    
    
    		case WM_COMMAND:
    			if(LOWORD(wParam) == IDSAIR){
    				EndDialog(hWnd,0);
    				DestroyWindow(hWnd);
    			}
    			break;
    
    		case WM_CLOSE:
    			EndDialog(hWnd,0);
    			DestroyWindow(hWnd);
    			break;
    		default:
    			break;
    	}
    	return 0;
    }

  11. #11
    Registered User
    Join Date
    Aug 2010
    Posts
    9

    Lightbulb

    I don't get it... something must be missing :S

    I have to char arrays:

    Code:
    char names[10][50] = { "Julieta", "Martin", "Samanta", "Ruben", "Franco", "Sandra", "Marta", "Ofélia", "Carlos", "Fabiana" };
    static char rank[10][3] = {"1º","2º","3º","4º","5º","6º","7º","8º","9º","10º"};
    And I have 2 Listboxes in the DialogBox. I'm writing one of the arrays in each box with the following:

    Code:
    case WM_INITDIALOG:
    			for( i=0 ; i<10 ; i++){
    				SendDlgItemMessage(hWnd, IDC_LIST1,LB_ADDSTRING,NULL,names[i]);
    				SendDlgItemMessage(hWnd, IDC_LIST1,LB_ADDSTRING,NULL,rank[i]);
                            }
    			break;
    The first array it gets it right, but the second one it writes:
    ___________
    10º









    ____________
    Starts on the last position, and only then starts normally.... what the hell?

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by keyboarder View Post
    I don't get it... something must be missing :S

    I have to char arrays:

    Code:
    case WM_INITDIALOG:
    			for( i=0 ; i<10 ; i++){
    				SendDlgItemMessage(hWnd, IDC_LIST1,LB_ADDSTRING,NULL,names[i]);
    				SendDlgItemMessage(hWnd, IDC_LIST1,LB_ADDSTRING,NULL,rank[i]);
                            }
    			break;
    The first array it gets it right, but the second one it writes:
    Look closely... you are sending two different strings to the same listbox index... the second overwrites the first.
    Also in your dialog box turn off the LBS_SORT parameter.
    Last edited by CommonTater; 08-31-2010 at 06:55 AM.

  13. #13
    Registered User
    Join Date
    Aug 2010
    Posts
    9
    lool sorry, I must have changed it before posting it. In the code I'm using, it's correct:

    Code:
    case WM_INITDIALOG:
    			for( i=0 ; i<10 ; i++)
    			{
    				SendDlgItemMessage(hWnd, IDC_LIST1,LB_ADDSTRING,NULL,names[i]);
    				SendDlgItemMessage(hWnd, IDC_LIST2,LB_ADDSTRING,NULL,rank[i]);
    			}
    			break;
    this looks more like a memory access problem. If I change the

    Code:
    char rank[10][3] = {"1º","2º","3º","4º","5º","6º","7º","8º","9º","10º"};
    to

    Code:
    char rank[11][3] = {"1º","2º","3º","4º","5º","6º","7º","8º","9º","10º","\0"};


    Thanks all!!! I FIGURED IT OUT. It was a dumb propriety "Sort". just changed it to false. Thank you for all the intel
    Last edited by keyboarder; 08-31-2010 at 08:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. listbox Problem
    By emtec in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2010, 12:59 AM
  2. Radiobuttons and listbox problem
    By osal in forum Windows Programming
    Replies: 2
    Last Post: 08-02-2004, 11:43 AM
  3. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 02:39 PM
  4. win32 Winsock problem
    By spoon_ in forum Windows Programming
    Replies: 3
    Last Post: 07-19-2002, 01:19 AM
  5. Problem with file listbox
    By Garfield in forum Windows Programming
    Replies: 6
    Last Post: 02-04-2002, 03:41 PM