Solved

Windows Messages

Posted on 1998-04-22
9
394 Views
Last Modified: 2013-12-03
I'm having nothing but bad luck from Windows Messages.  Specifically, I am trying to add items to a combo box.  Here is the line I am using to add the item:

long err = SendMessage(hWnd, CB_ADDSTRING, MAKEWPARAM(0, IDC_COMBO), (LPARAM)Item);

I immediatly call GetLastError after this line, and get a value of 0x58d (Control ID not found.)  I have specified the Control ID in the Hiword of the wParam, so I don't understand why I get this error.  And yes, I did specify the same control ID when creating the window.  I have also tried the line like this:

long err = SendMessage(hWnd, CB_ADDSTRING, 0, (LPARAM)Item);

and get the same result.
0
Comment
Question by:dirtdart
  • 4
  • 4
9 Comments
 
LVL 10

Accepted Solution

by:
RONSLOW earned 50 total points
ID: 1334375
You need hWnd to be the HWND of the combo control itself

Note that the CB_ADDSTRING docco says

CB_ADDSTRING
wParam = 0;                         // not used; must be zero
lParam = (LPARAM) (LPCTSTR) lpsz;    // address of string to add
 
Also, I would use ComboBox_AddString or ComboBox_AddItemData instead (it is a little nice to code that way)

0
 
LVL 11

Expert Comment

by:alexo
ID: 1334376
#include <windowsx.h> to do what Roger suggested.
0
 
LVL 5

Author Comment

by:dirtdart
ID: 1334377
I'm going to go ahead and grade your answer because you put forth the effort to answer.  However, that wasn't the problem.  hWnd is the hWnd of the control.  I realized the problem right after I left work.  I had created the control with the wrong control class specified (that's what happens sometimes when you cut and paste code).  If I change it to the correct class, I believe it will work as it should.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:RONSLOW
ID: 1334378
Well, at least you've got it fixed.  And you know that you don't need to include the control id in the message.  And you know that you can use ComboBox_AddItemData or AddString instead to make your code look neater (it worries about the SendMessage syntax for you).

0
 
LVL 5

Author Comment

by:dirtdart
ID: 1334379
Actually, I'm creating an AddString method for my combobox.  I'm trying to stay completly away from MFC, because i hate using it.  But it only makes sense to wrap objects you are going to use more than once (combobox, textbox, etc).  I had figured that the control ID didn't need to be included in the SendMessage, but since it had said Control ID not found, I thought I'd give it a shot like that.  It wouldn't be the first time an API was obscure.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1334380
ComboBox_AddString is NOTHING to do with MFC .. it is part of the API - its is implemented as a Macro in <windowsx.h>.

Most standard SendMessage calls have API macro wrappers that make them easier to call.  That way you don't need to remember what lparam and wparam etc to use .. the macro builds them approriately for each message.

If there is such a macro for a particular message, then it is ALWAYS best to use the macro instead of the direct SendMessage because:
* you are less likely to make mistakes
* your code will still work even if the way a particular message is send is altered in a later version of Win32
* it is much easier to read.


0
 
LVL 5

Author Comment

by:dirtdart
ID: 1334381
Ok.  I'll buy that.  Any chance you can tell me what the macro is to change the height of the stupid thing?  According to my error messages, everything worked beautifully, but I wouldn't know, because when I drop it down, it's only about one pixel high. AAARRRRGGGGGGGG!!!!!
0
 
LVL 5

Author Comment

by:dirtdart
ID: 1334382
Never mind.  Just found it.  Duh!

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1334383
Are you using the dialog resource editor in VC?

If so, click on the drop down arrow of the combo box from within the editor. That will show you the size of the dropdown part.  Drag it to the deried size.

You can change the size (I think) with message, but getting it right in the dialog template is better.

Is this another question?  If so, then you might be better off posting another question (with whatever points you feel is suitable) so that other experts can get a look in, people looking at the PAQ can find this answer, and (coincidentally) I can get a couple more points :-)


0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question