PDA

Click to See Complete Forum and Search --> : Problem with a listbox on a dialog box!


abdul
Jul 19th, 2001, 11:41 PM
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:


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?

Chris
Jul 20th, 2001, 01:59 AM
you need to capture the LBN_SELCHANGE message under the WM_COMMAND -> HIWORD(wParam) as show below sample code:


LRESULT CALLBACK MyDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
int idx =0, sz=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)lParam, LB_GETCURSEL, 0, 0);
if (idx != LB_ERR)
{
sz = SendMessage((HWND)lParam, LB_GETTEXTLEN, (WPARAM)idx, 0);
if (sz > 0)
{
str = new char[sz+1];
memset(str, 0, sz+1);
SendMessage((HWND)lParam, LB_GETTEXT, idx, (LPARAM)str);
SetDlgItemText(hDlg, IDC_EDIT1, str);
delete [] str;
}
}
break;
default:
break;
}
return FALSE;
}

abdul
Jul 20th, 2001, 09:47 AM
It does not work at all now. I think there is some problem in the returning value. Can you please find it!

Technocrat
Jul 20th, 2001, 09:58 AM
Can you post the entire programs code?

abdul
Jul 20th, 2001, 10:43 AM
Here is my C++ source file:


#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;
}

abdul
Jul 20th, 2001, 10:57 AM
Never mind!
It is working now. I just tried to change the text of another child window (not ID_SELECTION) and it works fine.

Technocrat
Jul 20th, 2001, 11:16 AM
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

abdul
Jul 20th, 2001, 11:30 AM
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.

Technocrat
Jul 20th, 2001, 11:33 AM
Try doing a clean, then rebuild all. See if that cleans up your problem.

abdul
Jul 20th, 2001, 12:33 PM
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.