Results 1 to 10 of 10

Thread: Problem with a listbox on a dialog box!

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Problem with a listbox on a dialog box!

    I have a listbox on a dialog box. I want to change the text of a static control to the selected text of the listbox whenever the selection is changed. The listbox is a "single selection" listbox and it has "LBS_NOTIFY" style. I am using the following code but nothing happens:

    Code:
    case ID_LIST:
    if(HIWORD(wparam) == LBN_SELCHANGE)
    {
    selindex = SendMessage(GetDlgItem(hwnd, ID_LIST),
    LB_GETCURSEL,0,0);
    sellen = SendMessage(GetDlgItem(hwnd, ID_LIST), LB_GETTEXTLEN,selindex,0);
    seltext = (char*)GlobalAlloc(GPTR, sellen + 1);
    SendMessage(GetDlgItem(hwnd, ID_LIST), LB_GETTEXT,selindex, (LPARAM)seltext);
    SetDlgItemText(hwnd, ID_SELECTION, "seltext");
    GlobalFree(seltext);
    }

    Can you check the error in there please?
    Baaaaaaaaah

  2. #2
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    you need to capture the LBN_SELCHANGE message under the WM_COMMAND -> HIWORD(wParam) as show below sample code:

    PHP Code:
    LRESULT CALLBACK MyDlg(HWND hDlgUINT messageWPARAM wParamLPARAM lParam)
    {
        
    int        idx =0sz=0;
        
    char    *str;

        switch (
    message)
        {
        case 
    WM_INITDIALOG:
            return 
    TRUE:
        case 
    WM_PAINT:
            break;
        case 
    WM_COMMAND:
            switch (
    HIWORD(wParam))
            {
            case 
    LBN_SELCHANGE:
                
    idx SendMessage((HWND)lParamLB_GETCURSEL00);
                if (
    idx != LB_ERR)
                {
                    
    sz SendMessage((HWND)lParamLB_GETTEXTLEN, (WPARAM)idx0); 
                    if (
    sz 0)
                    {
                        
    str = new char[sz+1];
                        
    memset(str0sz+1);
                        
    SendMessage((HWND)lParamLB_GETTEXTidx, (LPARAM)str);
                        
    SetDlgItemText(hDlgIDC_EDIT1str);
                        
    delete [] str;
                    }
                }
                break;
            default:
                break;
        }
        return 
    FALSE;


  3. #3

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    It does not work at all now. I think there is some problem in the returning value. Can you please find it!
    Baaaaaaaaah

  4. #4
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Can you post the entire programs code?
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  5. #5

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    ok

    Here is my C++ source file:

    Code:
    #include <windows.h>
    #include "resource.h"
    
    BOOL CALLBACK MyDlgProc(HWND, UINT, WPARAM, LPARAM);
    char *buffer, *seltext;
    int x,y,len, sellen, selindex;
    HRGN hrgn;
    HINSTANCE hinst;
    int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int nshowcmd)
    
    {
    	hinst = hinstance;
    	MSG msg;
    
    	DialogBox(hinstance, MAKEINTRESOURCE(MYDLG), NULL, MyDlgProc);
    
    	while(GetMessage(&msg, NULL, 0,0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return msg.wParam;
    }
    
    
    BOOL CALLBACK MyDlgProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
    {
    	hrgn = CreateEllipticRgn(10,10,330,330);
    
    	switch(msg)
    	{
    	case WM_INITDIALOG:
    
    		SetWindowRgn(hwnd, hrgn, FALSE);
    		return TRUE;
    	case WM_SHOWWINDOW:
    		SetFocus(GetDlgItem(hwnd, ID_TEXT));
    		return TRUE;
    	case WM_CLOSE:
    
    		EndDialog(hwnd, NULL);
    		PostQuitMessage(0);
    		return TRUE;
    
    	case WM_COMMAND:
    
    				switch (HIWORD(wparam))
    		{
    				case LBN_SELCHANGE:
    				selindex = SendMessage(GetDlgItem(hwnd, ID_LIST), LB_GETCURSEL,0,0);
    				sellen = SendMessage(GetDlgItem(hwnd, ID_LIST), LB_GETTEXTLEN,selindex,0);
    				seltext = (char*)GlobalAlloc(GPTR, sellen + 1);
    				SendMessage(GetDlgItem(hwnd, ID_LIST), LB_GETTEXT,selindex, (LPARAM)seltext);
    				SetDlgItemText(hwnd, ID_SELECTION, "seltext");
    				GlobalFree(seltext);
    				break;
    				default:
    					break;
    		}
    				break;
    
    		switch(LOWORD(wparam))
    		{
    		case ID_ADD:
    			len = GetWindowTextLength(GetDlgItem(hwnd, ID_TEXT));
    			if(len != NULL)
    			{
    			buffer = (char*)GlobalAlloc(GPTR, len + 1);
    
    			GetDlgItemText(hwnd, ID_TEXT, buffer, len + 1);
    
    			SendMessage(GetDlgItem(hwnd,ID_LIST), LB_ADDSTRING, 0, (LPARAM) buffer);
    			SetDlgItemText(hwnd, ID_TEXT, NULL);
    			SetFocus(GetDlgItem(hwnd, ID_TEXT));
    			
    			GlobalFree(buffer);
    			}
    			return TRUE;
    		case ID_CLEAR:
    SendMessage(GetDlgItem(hwnd, ID_LIST), LB_RESETCONTENT, 0,0);
    return TRUE;
    case IDOK:
     EndDialog(hwnd, IDOK);
     PostQuitMessage(0);
     return TRUE;
    		}
    		break;
    	}
    	return FALSE;
    }
    Baaaaaaaaah

  6. #6

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Never mind!
    It is working now. I just tried to change the text of another child window (not ID_SELECTION) and it works fine.
    Baaaaaaaaah

  7. #7
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Ok cool. I was going to have to ask you for the resource file because I was having a problem getting mine to work like I am sure it was supposed to.

    PS. Nice dialog affect
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  8. #8

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Thanks

    I also had problem naming the controls and menus. It my "text editor" program, I named my menus like this:

    ID_FILE_OPEN
    ID_FILE_SAVEAS
    ID_FILESAVE


    Now I perform certain function when a certain menu is clicked. But it does do any thing when "ID_FILE_OPEN" is clicked. When I changed its name to "ID_OPEN" it worked but and when I change it back to "ID_FILE_OPEN" , it does not work.

    The same thing is with the "ID_SELECTION" static control. I can set its text when I change its name to "ID_SEL" but when I change it back to "ID_SELECTION", I cannot change the text.

    I dont know if there is problem with the compiler (VC++) or something is rong sometime.
    Baaaaaaaaah

  9. #9
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Try doing a clean, then rebuild all. See if that cleans up your problem.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  10. #10

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    I is working now but i got another problem:

    When I run my program, and select(set the focus) to the "ID_ADD" (which is default button) -> and then I press any key, it unloads the dialog box. I dont know why its doing that because I have not put any code that unloads the dialog box when "ID_ADD" button is clicked.
    Baaaaaaaaah

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width