Click to See Complete Forum and Search --> : need help on my listbox


jesschen
November 14th, 2007, 04:34 PM
i attached file in this thread.

i wish it can have the function as:
(1)i select the item in listbox
(2)i click display
(3)i got an MessageBox to tell me which item i selected.

Marc G
November 15th, 2007, 08:57 AM
I didn't yet check the code, but use LB_GETCURSEL to get the current selected item. LB_GETCURSEL returns the index of the item and then use LB_GETTEXT to get the text of that item.

jesschen
November 16th, 2007, 04:18 PM
thanks marc..
but do u have any sample code?
i cannot use MFC.
so very few samples i can find online

Marc G
November 17th, 2007, 04:35 AM
Your listbox.zip file only contains 2 RC files and a .H file. Please post the complete project if you want us to take a look at it.
However, do you know how to work with SendMessage? If so, use SendMessage to send LB_GETCURSEL and LB_GETTEXT to your listbox. See MSDN for the exact parameters required for those 2 messages.

JohnCz
November 18th, 2007, 04:44 PM
LOL.
That is what I have indicated in other (completely unrelated) thread (http://www.codeguru.com/forum/showthread.php?p=1650894#post1650894).

jesschen
November 19th, 2007, 03:18 PM
the code with its executable file and project file *.dev are here.

JohnCz
November 19th, 2007, 10:49 PM
You can achieve this in several ways. One of them (see WM_COMMAND):

BOOL CALLBACK DlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
InitTabControl(hwnd, lParam);
break;

case WM_NOTIFY:
{
NMHDR *hdr;
hdr = (LPNMHDR)lParam;
if (hdr->code == TCN_SELCHANGING || hdr->code == TCN_SELCHANGE)
{
int index;
index = TabCtrl_GetCurSel(hdr->hwndFrom);
if (index >= 0 && index <= 4) ShowWindow(hDlg[index], (hdr->code == TCN_SELCHANGE) ? SW_SHOW : SW_HIDE);
}
break;
}

case WM_COMMAND:
{
WORD wCmdID = LOWORD(wParam);
WORD wCode = HIWORD(wParam);

switch(wCmdID)
{
case IDOK:
case IDCANCEL:
if(BN_CLICKED == wCode)
{
EndDialog(hwnd, 0);
}
break;
case IDC_DISPLAY:
if(BN_CLICKED == wCode)
{
char *pBuf = NULL;
char szFormat[] = "Selected item %d: %s";
char szMessage[200] = {0};

int iSel = (int)::SendMessage(hListbox, LB_GETCURSEL, 0, 0);
int iLen = 1 + (int)::SendMessage(hListbox, LB_GETTEXTLEN, iSel, 0);

pBuf = (char*)malloc(iLen);

SendMessage(hListbox, LB_GETTEXT, iSel, (LPARAM)pBuf);

sprintf(szMessage, szFormat, iSel, pBuf);
MessageBox(hwnd, szMessage, "Box Selection", MB_OK);

free(pBuf);
}
}
break;
}

case WM_CLOSE:
EndDialog(hwnd, 0);
break;

case WM_DESTROY:
DeleteObject(g_hbrBackground);
break;
default:
return FALSE;
}

return TRUE;
}


By the way DlgProc1 and DlgProc2 should look as DlgProc. Return TRUE if message is handled and you do not want dialog box window procedure to further handle it; return FALSE otherwise.

[EDIT] TRUE changed to FALSE; thanks Marc G!

Marc G
November 20th, 2007, 04:23 AM
By the way DlgProc1 and DlgProc2 should look as DlgProc. Return TRUE if message is handled and you do not want dialog box window procedure to further handle it; return TRUE otherwise.
You mean "return FALSE otherwise" ;)

JohnCz
November 20th, 2007, 06:07 AM
But of course! Edited, thanks.

jesschen
November 20th, 2007, 09:16 AM
hey...guys, thanks so much for your code and discussion!