Home | Index | Dotnet4all Snippets | Submit resources
About | Mail us 





ComboBox class, CB_INSERTSTRING , VB.NET and marshalling... (Saturday, August 28, 2004)



Found the following interesting discussion in the Newsgroups:

ComboBox class, CB_INSERTSTRING , VB.NET and marshalling...
by:GianPiero Andreis

Hello All,
let me pose a simple question about combobox and the CB_INSERTSTRING message.
Suppose for instance that I already have a combobox handle, how can I declare and use the SendMessage function just for insert a new item into the combobox ?
Of course, the combo box DO NOT belong to my process, so I can't use the standard properties of that class. In fact, I need to "put" a new entry in the combo box of IExplorer. EnumWindows get me the handle of the IExplorer main window, with EnumChildWindows I can obtain the handle of the "www address" combo. At this point, I would like to put into the combo an "www address" of choice.
I tried ALL the parameters of the MarshalAs attribute related to the string, (with the UnmanagedType enum) but never seems to works, sometime IE crashes.. :-)

Please, don't tell to me to use SendKeys class. .NET SendKeys class works well (do not alter, no more, the numlock and/or capslock status of my keyboard, like the older sendkeys in VB) but I need to use SendMessage because I want to unterstand hot to marshalling correctly the parameters. And last, but not least, I LIKE API's...

Thank You Gentleman for Your precious help.
Best regards
Ing. GianPiero Andreis


 Reply:
by:CJ Taylor

 
Whats your code look like for the SendMessage?
And what part are you marshalling? individual variables or an entire structure?


 Reply:
by:GianPiero Andreis

 Ok,
a piece of code...
This is the function I use for looping over the child windows in IE.
Everything works well except the SendMessageA function


Public Function EnumChildWindowsSub(ByVal _
argl_hChildWnd As IntPtr, ByVal argl_Param _
As Integer) As Boolean
Dim lsb_ClassName As New StringBuilder(256)
Call GetClassNameA(argl_hChildWnd, lsb_ClassName, _
lsb_ClassName.Capacity)
If (lsb_ClassName.ToString = "ComboBox") Then
SendMessageA(argl_hChildWnd.ToInt32, _
CB_INSERTSTRING, 0, "www.thatsite.com")
'This just for see with Spy that the combo handle is correct
Me.Text = "I find combo with handle " & _
Hex(argl_hChildWnd.ToInt32).ToString
Return False
Else
Return True
End If
End Function


And this is the prototype for the function.


Private Declare Auto Function SendMessageA _
Lib "user32" (ByVal hwnd As Long,
ByVal wMsg As Long, ByVal wParam As Long _
, <MarshalAs(UnmanagedType.LPStr)>
ByVal lParam As String) As Long


There is no need to use System.IntPtr, I don't look for system portability at this time.

Regarding to formatted classes, at this time I don't use it because I see Win32 APi's are working as well without the use of that encapsulation. Maybe this is the cause of the problem, but consider that I use API very very often in .NET (because I wrote automation programs, macro player and so on), and everything works well without formatted classes. I tried also the use of StringBuilder class as fouth parameter in the SendMessageA function, but IE crashes.
If You need other informations, I would be glad to write for You forward. Please excuse my english, is very poor, I know.

Thank You in advance, Sir.

Ing. GianPiero Andreis


 Reply:
by:GianPiero Andreis

 Hey All,
finally this code works. I worked hard over the CB_*** messages, but there are no way (it seems to me) to set an item into the ComboBox, like the old VB instead allow with API.

The only solution I find is to send a message with the edit portion of the combo (i.e. is a child window that belongs to the combo). So You need, once You have the combo handle, to enumerate all the child windows in the combo, and when the class name You find is "Edit", You can send the message WM_SETTEXT with the string You wanted. Below You find the declaration of SendMessage and the line of code that send the string into the combo.

Hope this helps someone.

Best regards


'Declaration
Private Declare Auto Function SendMessageA _
Lib "user32" (ByVal hwnd As
IntPtr, ByVal wMsg As IntPtr, ByVal wParam As IntPtr,
<MarshalAs(UnmanagedType.AnsiBStr)> ByVal _
lParam As String) As IntPtr
'Code (argl_hChildWnd is the handle of the Edit _
window into the combo)
SendMessageA(argl_hChildWnd, New _
IntPtr(WM_SETTEXT), IntPtr.Zero, "www.thatsite.com")


 Reply:
by:M. Angelo

 If you use sendmessage to set the strings in a list control you will not be able to use the framework functions to retrieve it because it deals with objects instead of strings.

The ListBox and the TextBox are not extremely difficult to subclass but I found the ComboBox subclassing not pratical.

I would like to read further from your experience.

Regards



Posted by Xander Zelders



0 Comments:

Post a Comment

<< Home

 
Previous Posts
    - How to determine the size of a file in VB.NET
    - HttpWebRequest & Credentials
    - Synclock clarification
    - How to reference an embedded icon file
    - Returning Integer from String
    - Expiring the Page To discourage Browser BACK butto...
    - How to use a treeview control
    - Clipboard in console application
    - How to manage a listbox from javascript
    - Deploying MSDE with VB.net



Disclaimer & Terms of Use | DotNet4All.Com concept & © 2004 - 2007 by Zelders² - Holland