LB_ADDSTRING,LB_GETCURSEL - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

LB_ADDSTRING,LB_GETCURSEL

while i'm retrieve the item from the listbox; it always returns zero(0)how can i solve out!

here my code[code]
#define WIN32_LEAN_AND_MEAN
#include
#include
#include
#include


#define IDB_REMOVE 1000
#define IDB_ADD 1001
#define IDE_ITEMTEXT 2000
#define IDL_LISTBOX 3000


char szClassName[] = "Windows App w/controls";
char text[] = "rbh";

HINSTANCE hApplicationInstance = NULL;
HWND hListbox = NULL;
HWND hMainWindow = NULL;
HWND hRemove = NULL;
HWND hEdit = NULL;
HWND hAdd = NULL;









LRESULT AddItem(HWND hwnd, LPSTR lpstr);
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreviousInstance, LPSTR lpCommandLine, int nShowCommand) {
MSG messages;
WNDCLASSEX wcex;

hApplicationInstance = hInstance;

wcex.cbClsExtra = 0;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.cbWndExtra = 0;
wcex.hbrBackground = (HBRUSH)(COLOR_3DFACE+2);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wcex.hInstance = hInstance;
wcex.lpfnWndProc = WindowProcedure;
wcex.lpszClassName = szClassName;
wcex.lpszMenuName = NULL;
wcex.style = CS_HREDRAW | CS_VREDRAW;

if (!RegisterClassEx(&wcex))
return 0;

hMainWindow = CreateWindowEx(0, szClassName, "Win32 Controls", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_DESKTOP, NULL, hInstance, NULL);

ShowWindow(hMainWindow, nShowCommand);
while(GetMessage(&messages, NULL, 0, 0)) {
if (!IsDialogMessage(hMainWindow, &messages)) { // <-- Added
TranslateMessage(&messages);
DispatchMessage(&messages);
} // <-- Added
}

return (int)messages.wParam;
}


LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
HGDIOBJ hDefaultFont = NULL;
HWND hwndList,hButton;
char Buffer[126]="";

switch(uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
break;

case WM_CREATE: {


hEdit = CreateWindowEx(WS_EX_CLIENTEDGE,
"Edit",
"",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
110, 35, 50, 20,
hwnd,
(HMENU)IDE_ITEMTEXT,
hApplicationInstance,
NULL);


hButton = CreateWindowEx(WS_EX_CLIENTEDGE,
"BUTTON",
"Remove",
WS_CHILD | WS_VISIBLE ,
110, 280, 50, 20,
hwnd,
(HMENU)IDB_REMOVE,
hApplicationInstance,
NULL);

hButton = CreateWindowEx(WS_EX_CLIENTEDGE,
"BUTTON",
"Add",
WS_CHILD | WS_VISIBLE ,
110, 300, 50, 20,
hwnd,
(HMENU)IDB_ADD,
hApplicationInstance,
NULL);



hwndList = CreateWindow (TEXT ("listbox"), NULL,
WS_CHILD | WS_VISIBLE | LBS_STANDARD|LBS_SORT,
120, 140,
240,
145,
hwnd, (HMENU) IDL_LISTBOX,
(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
NULL) ;


SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)"First");
SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)"Sec");
SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)"Third");
SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)"Four");
SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)"Five");
SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)"Six");





}



case WM_COMMAND: {


switch(LOWORD(wParam)){

case IDB_REMOVE: {
if (HIWORD(wParam) == BN_CLICKED) {
MessageBox(hwnd,"IDB_REMOVE Clicked","",MB_OK);
int iCurSel = (int)SendMessage(hwndList, LB_GETCURSEL, 0, 0);
sprintf(Buffer,"The cur sel val is %d",iCurSel);
MessageBox(hwnd,Buffer,"",MB_OK);
if (iCurSel == -1) {
MessageBox(hwnd,"nothing selected","",MB_OK);
}break;
MessageBox(hwnd,"selection success","",MB_OK);
SendMessage(hwndList, LB_DELETESTRING, (WPARAM)iCurSel, 0);
}
}break;

case IDL_LISTBOX: {
if (HIWORD(wParam) == LBN_SELCHANGE) {
char *Item;
int iCurSel = (int)SendMessage(hwndList, LB_GETCURSEL, 0, 0);
int nItemLen = (int)SendMessage(hwndList, LB_GETTEXTLEN, iCurSel, 0);
if (nItemLen > 0) {
Item = (char*)malloc(nItemLen);
SendMessage(hwndList, LB_GETTEXT, iCurSel, (LPARAM)Item);
SetWindowText(hwnd, Item);
}
}

}break;

case IDB_ADD: {
if (HIWORD(wParam) == BN_CLICKED) {
MessageBox(hwnd,"IDB_ADD selected","",MB_OK);
//char *NewItem;
//int nTextLen = (int)SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
//if (nTextLen > 0) {
//NewItem = (char*)malloc(nTextLen);
//SendMessage(hEdit, WM_GETTEXT, (nTextLen + 1), (LPARAM)NewItem);
//MessageBox(hwnd,NewItem,"item",MB_OK);
//AddItem(hwndList, NewItem);
//}

}
}break;
}
}

/*if (LOWORD(wParam) == 1 &&
HIWORD(wParam) == BN_CLICKED &&
(HWND) lParam == hwndButton)ss
}*/

default: return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

return 0;
}

/*
This is a quick and easy function to add items to a listbox for you
*/
//LRESULT AddItem(HWND hwndList, LPSTR lpstr) {
// MessageBox(NULL,lpstr,"item @ AddItem routine",MB_OK);
// return SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)lpstr);
//}




[/code]

Comments

  • misfit5000misfit5000 Posts: 4Member
    use this instead

    INT iCurSel = SendDlgItemMessage(hwnd,IDL_LISTBOX,LB_GETCURSEL,0,0);

    also, the window style (when creating the list box), take out LBS_SORT, just make it WS_CHILD | WS_VISIBLE and nothing else. Take out the other LBS too because it has LBS_SORT in it. Just WS_CHILD | WS_VISIBLE is all you need.
  • misfit5000misfit5000 Posts: 4Member
    If you have anymore questions about listboxes and buttons ask me, I know how to color them and even make them into pictures.
Sign In or Register to comment.