Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data combo

Status
Not open for further replies.

YanaD

Programmer
Mar 22, 2001
38
0
0
US
I have a questiong about the data combo, how to set the ListWidth. I have data in the datacombo, but the data is long and when I click on drop down i can see the data, but some of the data is long and I can't see the whole think, so how to set the listwidth of the data combo.
 
heres your declaration:

Option Explicit

Private Const CB_SETDROPPEDWIDTH = &H160
Private Const CB_GETDROPPEDWIDTH = &H15F
Private Const DT_CALCRECT = &H400

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

Private Declare Function SendMessageLong Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long

Private Declare Function DrawText Lib "user32" Alias _
"DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, _
ByVal nCount As Long, lpRect As RECT, ByVal wFormat _
As Long) As Long


HERES YOUR FUNCTION:

Public Function AutoSizeDropDownWidth(Combo As Object) As Boolean
'**************************************************************
'PURPOSE: Automatically size the combo box drop down width
' based on the width of the longest item in the combo box

'PARAMETERS: Combo - ComboBox to size

'RETURNS: True if successful, false otherwise

'ASSUMPTIONS: 1. Form's Scale Mode is vbTwips, which is why
' conversion from twips to pixels are made.
' API functions require units in pixels
'
' 2. Combo Box's parent is a form or other
' container that support the hDC property

'EXAMPLE: AutoSizeDropDownWidth Combo1
'****************************************************************
Dim lRet As Long
Dim bAns As Boolean
Dim lCurrentWidth As Single
Dim rectCboText As RECT
Dim lParentHDC As Long
Dim lListCount As Long
Dim lCtr As Long
Dim lTempWidth As Long
Dim lWidth As Long
Dim sSavedFont As String
Dim sngSavedSize As Single
Dim bSavedBold As Boolean
Dim bSavedItalic As Boolean
Dim bSavedUnderline As Boolean
Dim bFontSaved As Boolean

On Error GoTo ErrorHandler

If Not TypeOf Combo Is ComboBox Then Exit Function
lParentHDC = Combo.Parent.hdc
If lParentHDC = 0 Then Exit Function
lListCount = Combo.ListCount
If lListCount = 0 Then Exit Function


'Change font of parent to combo box's font
'Save first so it can be reverted when finished
'this is necessary for drawtext API Function
'which is used to determine longest string in combo box
With Combo.Parent

sSavedFont = .FontName
sngSavedSize = .FontSize
bSavedBold = .FontBold
bSavedItalic = .FontItalic
bSavedUnderline = .FontUnderline

.FontName = Combo.FontName
.FontSize = Combo.FontSize
.FontBold = Combo.FontBold
.FontItalic = Combo.FontItalic
.FontUnderline = Combo.FontItalic

End With

bFontSaved = True

'Get the width of the largest item
For lCtr = 0 To lListCount
DrawText lParentHDC, Combo.List(lCtr), -1, rectCboText, _
DT_CALCRECT
'adjust the number added (20 in this case to
'achieve desired right margin
lTempWidth = rectCboText.Right - rectCboText.Left + 20

If (lTempWidth > lWidth) Then
lWidth = lTempWidth
End If
Next

lCurrentWidth = SendMessageLong(Combo.hwnd, CB_GETDROPPEDWIDTH, _
0, 0)

If lCurrentWidth > lWidth Then 'current drop-down width is
' sufficient

AutoSizeDropDownWidth = True
GoTo ErrorHandler
Exit Function
End If

'don't allow drop-down width to
'exceed screen.width

If lWidth > Screen.Width \ Screen.TwipsPerPixelX - 20 Then _
lWidth = Screen.Width \ Screen.TwipsPerPixelX - 20

lRet = SendMessageLong(Combo.hwnd, CB_SETDROPPEDWIDTH, lWidth, 0)

AutoSizeDropDownWidth = lRet > 0
ErrorHandler:
On Error Resume Next
If bFontSaved Then
'restore parent's font settings
With Combo.Parent
.FontName = sSavedFont
.FontSize = sngSavedSize
.FontUnderline = bSavedUnderline
.FontBold = bSavedBold
.FontItalic = bSavedItalic
End With
End If
End Function
Todd Norris
Hope this helps !
 
It is perfectly working with simple combo box, but I am using DATACOMBO, which is not working with your codes. I tried to replace some codes from combobox to datacombo, but it is not working because plain combo box has different methods and properties than datacombo, that's why it is not working. I don't know what should I do now, if u know how to do it with datacombo, I will appreciate your help. Thank u anyway, waiting for response.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top