getting value from listbox

W

wallacej

I am trying to retrieve a selected value from a listbox and store it in
a string (char[]) variable. I am using the following code:

calibIndex=SendMessage(hWnd,(UINT)IDC_LIST_CALIBOPTS,(WPARAM)0,
LPARAM)0);
SendMessage(hWnd,(UINT)LB_GETTEXT,(WPARAM)calibIndex,(LPARAM)selectedCalib);

Where IDC_LIST_CALIBOPTS is the list and selectedCalib is the variable
where I want the value to go.

This does not work and when I look at the value of selectedCalib it is
"[rvЩ]" and not "Calibration1" as I expected.

Can anybody help?

Thank you
 
N

Nahappan SM

The first statement:
calibIndex=SendMessage(hWnd,(UINT)IDC_LIST_CALIBOPTS,(WPARAM)0,
LPARAM)0);

is incorrect.
To get the selected index, use:

calibIndex=SendMessage(hWnd,(UINT)LB_GETSEL,(WPARAM)0,
LPARAM)0);

hWnd is the handle to the Listbox control. It can be obtained from:
hWnd = GetDlgItem (hParentWnd, IDC_LIST_CALIBOPTS);
where hParentWnd is the handle of the dialog/window hosting the Listbox.

The second statement in your code is correct. Remember that hWnd is the
handle to the listbox control and not to the parent window/dialog.
 
W

wallacej

Thank You

That solved the problem. Was getting mixed up with handle to the
control and handle to the parent window.


One more, do you know why using the following to fill the list box will
make the first item in the list blank?

inputfile.getline(sectionName, 50);
SendMessage(hwndlist, LB_ADDSTRING, 0, (LPARAM) sectionName); //populate
 
H

Howard

Nahappan SM said:
The first statement:
calibIndex=SendMessage(hWnd,(UINT)IDC_LIST_CALIBOPTS,(WPARAM)0,
LPARAM)0);

is incorrect.
To get the selected index, use:

calibIndex=SendMessage(hWnd,(UINT)LB_GETSEL,(WPARAM)0,
LPARAM)0);

hWnd is the handle to the Listbox control. It can be obtained from:
hWnd = GetDlgItem (hParentWnd, IDC_LIST_CALIBOPTS);
where hParentWnd is the handle of the dialog/window hosting the Listbox.

The second statement in your code is correct. Remember that hWnd is the
handle to the listbox control and not to the parent window/dialog.

Please refrain from discussing MFC or other platform-specific code here.
Listboxes, dialogs, etc., are not part of the standard C++ language and
off-topic in this newsgroup. Questions regarding MFC should be directed to
microsoft.public.vc.mfc. Answering them here only encourages more off-topic
questions.

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top