Re: Stupid Question Time: Combo box items

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Never mind. I'd missed the ".Container" in the ScreenToClient changes. Works beautifully now. Thanks!

Final--working--code for any future readers of this thread is (watch out for wrapping):


Private Const cbGetItemHeight = &H154

Private Type POINTAPI
x As Long
y As Long
End Type

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hWnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal
nHeight As Long, ByVal bRepaint As Long) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As
Long, lParam As Any) As Long

Public Sub SetComboBoxRows(ByVal cmbToChange As VB.ComboBox, ByVal lngNumRows As Long)
Dim rc As RECT
Dim pt As POINTAPI
Dim lngCurHeight As Long
Dim lngNewHeight As Long
Dim lngScaleMode As Long

lngCurHeight = SendMessage(cmbToChange.hWnd, cbGetItemHeight, 0, ByVal 0)
lngNewHeight = lngCurHeight * (lngNumRows + 2)
GetWindowRect cmbToChange.hWnd, rc
pt.x = rc.Left
pt.y = rc.Top
ScreenToClient cmbToChange.Container.hWnd, pt
MoveWindow cmbToChange.hWnd, pt.x, pt.y, rc.Right - rc.Left, lngNewHeight, True
End Sub



Rob

"Robert Morley" <rmorley@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:ufghP5t$HHA.3940@xxxxxxxxxxxxxxxxxxxxxxx
Doesn't seem to have worked. Did I goof somewhere? Here's the code I have currently (with many of the variables renamed to
conform to my convention):

Public Sub SetComboBoxRows(ByVal cmbToChange As VB.ComboBox, ByVal lngNumRows As Long)
Dim rc As RECT
Dim pt As POINTAPI
Dim lngCurHeight As Long
Dim lngNewHeight As Long
Dim lngScaleMode As Long
Dim lngWidth As Long

lngWidth = cmbToChange.Width
lngCurHeight = SendMessage(cmbToChange.hWnd, cbGetItemHeight, 0, ByVal 0)
lngNewHeight = lngCurHeight * (lngNumRows + 2)
GetWindowRect cmbToChange.hWnd, rc
pt.x = rc.Left
pt.y = rc.Top
ScreenToClient cmbToChange.hWnd, pt
MoveWindow cmbToChange.hWnd, pt.x, pt.y, rc.Right - rc.Left, lngNewHeight, True
End Sub


Rob

"Mike Williams" <mike@xxxxxxxxxxxxxxxxx> wrote in message news:uSbxzyt$HHA.4612@xxxxxxxxxxxxxxxxxxxxxxx
"Robert Morley" <rmorley@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:OWEQ9Yt$HHA.5164@xxxxxxxxxxxxxxxxxxxxxxx

Just out of curiosity, could this be made to work without
the references to the form if I were to assume that the
form's ScaleMode is always in twips (which all my forms
are) and did the appropriate conversions?

Yes. You don't even need to assume anything about the ScaleMode because it will work whatever it happens to be. Just change the
following two lines (the comented out lines are the originals and the uncommented lines are their replacements:

' Call ScreenToClient(Form1.hWnd, pt)
Call ScreenToClient(Combo1.Container.hWnd, pt)

'Call MoveWindow(Combo1.hWnd, pt.x, pt.y, _
Combo1.Width, newHeight, True)
Call MoveWindow(Combo1.hWnd, pt.x, pt.y, _
rc.Right - rc.Left, newHeight, True)

You can now remove all references to Form1 in the example code. This will enable you to easily build your required function in
which you just need to pass the Combo Box and the required number of rows.

Mike









.



Relevant Pages

  • Re: Drag from FileListBox, drop on DirListBox?
    ... > (lpPoint As POINTAPI) As Long ... > Private Function GetRealFirstListIndexAs Long ... > Dim cnt As Long ... time) onto a directory shown on a DirListBox and have it move the files ...
    (microsoft.public.vb.enterprise)
  • Re: Drag from FileListBox, drop on DirListBox?
    ... > (lpPoint As POINTAPI) As Long ... > Private Function GetRealFirstListIndexAs Long ... > Dim cnt As Long ... time) onto a directory shown on a DirListBox and have it move the files ...
    (microsoft.public.vb.enterprise)
  • Re: Drag from FileListBox, drop on DirListBox?
    ... > (lpPoint As POINTAPI) As Long ... > Private Function GetRealFirstListIndexAs Long ... > Dim cnt As Long ... time) onto a directory shown on a DirListBox and have it move the files ...
    (microsoft.public.vb.general.discussion)
  • Re: VB automatic shutdown
    ... Private mdtLastKBorMouseEvent As Date ... Lib "user32" (lpPoint As POINTAPI) As Long ... Private Sub Form_Unload ... Dim i As Integer ...
    (microsoft.public.vb.general.discussion)
  • Re: Problem with execution order in print routine
    ... Private Const DM_ORIENTATION = &H1 ... Dim strPrinterDesignation As String ... Dim iRet As Long ... "Unexpectedly could not access printer property ...
    (microsoft.public.word.vba.general)