I'm currently in the process of dusting off the bugs from a tool
written back in 2011. One of the bugs involves an editable
ComboBox control paired with a separate
Button control. When
the button is clicked a popup menu is shown, and when an item in
this menu is clicked, the text of the
ComboBox is updated.
The problem with this scenario is by default, as soon as the
ComboBox loses focus, the
SelectedText properties are reset, thus preventing my
little menu from replacing the selected text. While this article
doesn't solve this problem (I'll save that for the next
article!), it does describe how you can get the
window handle (in .NET terms the
Handle) of the edit
component, thus opening the door for a little Win32 API fun.
There are various approaches to doing this - you could use
FindWindow and search for a child window with a class of
edit, or use
SendMessage with the
message. Or, easiest of all, we can use the
Using this API is very simple, as there's one method and two simple structs.
There is one (well, many, but lets start simple!) caveat which
you may fall foul of if you are new to Win32 API programming -
often the contents of structs need to be initialized with their
size first. But how to you know how big a structure is? Lucky
for you, you don't need to calculate it manually - the
Marshal.SizeOf function will handle this for you.
If you forget to set the size, then the structure simply won't be populated.
With the above in place, then getting the handle of the edit component is very straightforward.
Bear in mind that the
edit control is a Win32 control - not a
managed .NET control. In order to do anything with this
therefore, you need to use additional Win32 API methods, or
perhaps bind it to a
NativeWindow class for easy subclassing.
I'll briefly cover some of this in a future article.
COMBOBOXINFO structure has other information, such as the
handle of the
list control, which you see if you set the
DropDownStyle property of the
Simple and the
state of the dropdown button. You can view the MSDN
Documentation to learn more about the structure.
- 2013-09-29 - First published
- 2020-11-21 - Updated formatting
Like what you're reading? Perhaps you like to buy us a coffee?