Combobox SendMessage CB_SETDROPPEDWIDTH

W

Wapiti

I'm trying to use SendMessage to control the combobox's dropdown area's
width. This was a no brainer in VB6, and seems very comparible in vb.net.
But for some reason, the list area isn't expanding to the number of pixels I
specify. It appears that others have made this work, so I must be doing
something wrong.

SendMessage continually returns the width of the control as it resides on
the form, rather than the value I send it.

Public Class Form1
Inherits System.Windows.Forms.Form
Friend WithEvents cmbTEST As System.Windows.Forms.ComboBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu

Protected Overrides Sub OnLoad(ByVal e As EventArgs)
MyBase.OnLoad(e)
setComboboxWidth(cmbTEST, 400)
End Sub 'OnLoad

'For ComboBox width (see setComboboxWidth below)
Declare Function SendMessage Lib "coreDll.dll" (ByVal hwnd As IntPtr, ByVal
wMsg As Int32, ByVal wParam As Boolean, Optional ByVal lParam As Integer =
0) As Integer
Declare Function GetCapture Lib "coredll.dll" () As IntPtr

#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Private Sub InitializeComponent()
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.cmbTEST = New System.Windows.Forms.ComboBox
Me.Button1 = New System.Windows.Forms.Button
'
'cmbTEST
'
Me.cmbTEST.Items.Add("this is a test")
Me.cmbTEST.Items.Add("this is a nother test")
Me.cmbTEST.Items.Add("this is another another test")
Me.cmbTEST.Location = New System.Drawing.Point(32, 112)
Me.cmbTEST.Size = New System.Drawing.Size(64, 22)
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(72, 184)
Me.Button1.Text = "Button1"
'
'Form1
'
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.cmbTEST)
Me.Menu = Me.MainMenu1
Me.Text = "Form1"
End Sub
#End Region


'I've tried LONG and INT, both give the same results - no change to the
width of the list area. ??
Public Sub setComboboxWidth(ByVal cmb As ComboBox, ByVal pxWidth As Long)
Const CB_SETDROPPEDWIDTH = &H160
cmb.Capture = True
Dim hwnd As IntPtr = GetCapture()
cmb.Capture = False
Dim rtn As Integer
rtn = SendMessage(hwnd, CB_SETDROPPEDWIDTH, pxWidth, 0)
If (rtn = -1) Then
MsgBox("An error occurred in sendmessage! (" & rtn & ")")
Else
MsgBox("SendMessage returned (" & rtn & ")")
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Close()
End Sub

End Class
 
C

Chris Tacke, eMVP

I notice you have SendMessage defined with WParam as Boolean instead of
Int32 - any reason why?

-Chris
 
C

Chris Tacke, eMVP

I also notice in the header that CB_SETDROPPEDWIDTH is only defined when
WINVER > 0400....

-Chris
 
W

Wapiti

holy crap, i have no idea how that got set that way... Thanks for finding
that Chris... I'm baffled.
 
W

Wapiti

Chris Tacke said:
I notice you have SendMessage defined with WParam as Boolean instead of
Int32 - any reason why?

Thanks Chris, that was it. I must have nabbed that definition
(declaration?) from somewhere on the web - not a very good place to copy
code, it appears.
 
W

Wapiti

Not finding what you are referring to here.

Chris Tacke said:
I also notice in the header that CB_SETDROPPEDWIDTH is only defined when
WINVER > 0400....

-Chris


pixels
 
C

Chris Tacke, eMVP

Yeah, I've got versions with uints, rects, regions, and all sorts of others.
I *always* alias to to something like SendMessageRECT or SendMessageLONG to
prevent me from mangling my own stuff.

-Chris
 
W

Wapiti

Good idea, appreciate that. Thanks. -Mike

(I know its bad news'etiquette to reply with simple thanks, but you saved me
much time/frustration on this one - It was a very helpful find. Thanks.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top