Tuesday, July 12, 2022

[FIXED] How to send a CBN_SELCHANGE message when using CB_SETCURSEL?

Issue

When using the CB_SETCURSEL message, the CBN_SELCHANGE message is not sent.

How to notify a control that the selection was changed ?

P.S.

I found on the Sexchange site, a very ugly hack :

SendMessage( hwnd, 0x014F/*CB_SHOWDROPDOWN*/, 1, 0 );
SendMessage( hwnd, 0x014E/*CB_SETCURSEL*/, ItemIndex, 0 );
SendMessage( hwnd, 0x0201/*WM_LBUTTONDOWN*/, 0, -1 );
SendMessage( hwnd, 0x0202/*WM_LBUTTONUP*/, 0, -1 );

Will do for now... Not really.

P.S.2

For resolving my problem, I'll follow Ken's suggestion in the comments.


Solution

You're not supposed to use CBN_SELCHANGE unless the change in selection was made by the user.

You don't indicate what language you're using; it would make it easier to provide you with a workaround if you did so.

In Delphi, where an OnChange() would be associated with the combobox, you just call the event method directly:

// Send the CB_SETCURSEL message to the combobox
PostMessage(ComboBox1.Handle, CB_SETCURSEL, Whatever, WhateverElse);

// Directly call the OnChange() handler, which is the equivalent to CBN_SELCHANGE
ComboBox1Change(nil);


Answered By - Ken White
Answer Checked By - Cary Denson (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.