Solved

Windows Messages

Posted on 1998-04-22
9
393 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 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