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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Complete string is not displaying in combo Box 3

Status
Not open for further replies.

PankajAgr

Technical User
Jul 30, 2003
52
IN
Hi Guys,

I added some items in Combo box, that text width is greater than the combo box width. When I selecting the combox box, it is showing the truncated text.

Ex: I added the string 'Built-up, R-14 Board, R-38 Batt' in combo box. When I select the combo box it is only showing 'Built-up, R-14 Board, R-38' string not a full string.

Without increasing the combo box width is there any way to display the full string (like tooltip) when mouse/key move over the items in combo box?

Thanks in advance
Pankaj


Senior Software Engineer,
Infotech Enterprises Limited
Hyderabad, Andhra Pradesh, India.
URL :
 
Hi
You can use the ToolTipText property of the Combo Box.
I.e: Change the value of the property on the Cbo_Click event to the correct one, every time user selects one entry.

Good Luck!
;-)
 
Hi MrPtt,

Tooltip is only displaying when the poiter is moving above the control but my problem is different.

When I am moving from one value to another value at that moment, I want to display full string.

So is there any way to display the full string when poiter is moving from one value to another value either through keyboard or mouse.

Thanks
Pankaj

Senior Software Engineer,
Infotech Enterprises Limited
Hyderabad, Andhra Pradesh, India.
URL :
 
Yes, it's possible...but it is quite a lot of work to achieve with the standard combobox.
 
Can u give me some tips or source where I will get some information to create standard combobox.



Senior Software Engineer,
Infotech Enterprises Limited
Hyderabad, Andhra Pradesh, India.
URL :
 
Hi
Another option is use a label as a ToolTip. Every time you move betwin values, load the label and show the complete string.

Format the label as a ToolTip, and hide/show it when needed.

Rudimentary, but I think it'll work, isn't it?

What do you think?
 
Hi,

Yes u r right, I tried to use as a label but my problem is how to trace the mouse movement when mouse pointer move from one item to another. When the pointer is moving, immediate, I want to show the full string.

One more poblem how to identify the position of the selected items means when select the combo box it open the list downwards, when moving from one item to another how to identify the position of the select item because in same position I want to show the complete name.

Thanks


Senior Software Engineer,
Infotech Enterprises Limited
Hyderabad, Andhra Pradesh, India.
URL :
 
> how to trace the mouse movement
> how to identify the position of the selected items

As I said: "it is quite a lot of work
 
Yes, U r right, it requires a lot of efforts. No problem, I am ready for this so please give me some tips or links where can I get some useful information.

Thanks

Senior Software Engineer,
Infotech Enterprises Limited
Hyderabad, Andhra Pradesh, India.
URL :
 

Private Sub Combo1_Click()
Label1.Caption = Combo1.list(Combo1.ListIndex)
End Sub

May not be enough but it should point you in the right direction.

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Hi Guys,

This is not a problem, my problem is different-

1. How to trace mouse move when pointer move from one item to another in combobox?
2. How to identify the visual position (position w.r.t. form) of the selected item in combobox?

Please suggest me some solution regarding these problem.

Thanks


Senior Software Engineer,
Infotech Enterprises Limited
Hyderabad, Andhra Pradesh, India.
URL :
 
It won't be, as that will only work once a selection has been made...
 

Put suffitietly wide label, short combobox, and Timer control Timer1 with interval of 100 and run this code.

Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
Label1.Caption = Combo1.List(Combo1.ListIndex)
End Sub

Private Sub Form_Load()
Dim i

For i = 0 To 19
With Combo1
.AddItem "AAAAAAAAAAAAAAAAA" & CStr(i)
End With
Next i

End Sub

Private Sub Timer1_Timer()
Label1.Caption = Combo1.List(Combo1.ListIndex)
End Sub

You may want to add some code that would enable/disable the timer when you wish.

VladK
 
vladk's solution is probably sufficient in most cases, but here's the a solution that addresses some of the drawbacks of earlier ideas (although it may have some of it's own). Note that this is a proof of concept solution, not production code.

You'll need a form with a combobox on it (Combo1) and a picturebox (picToolTip). Drop the following code into the form:
Code:
Option Explicit

Private Type COMBOBOXINFO
   cbSize As Long
   rcItem As RECT
   rcButton As RECT
   stateButton  As Long
   hwndCombo  As Long
   hwndEdit  As Long
   hwndList As Long
End Type

Private Declare Function GetComboBoxInfo Lib "user32" (ByVal hwndCombo As Long, CBInfo As COMBOBOXINFO) As Long

Private HookWindow As Long


Private Sub Combo1_Click()
    Combo1.ToolTipText = Combo1.Text
End Sub

' Set up the example
Private Sub Form_Load()
    Dim CBI As COMBOBOXINFO
    
    Set vbToolTip = picToolTip
    With vbToolTip
       .Visible = False
       .BorderStyle = vbFixedSingle
       .Appearance = 0 ' flat
       .BackColor = &H80000018 ' tooltip background
        SetParent .hwnd, GetDesktopWindow
    End With
    
    Combo1.AddItem "Hello"
    Combo1.AddItem "there"
    Combo1.AddItem "Just an example of a rather long piece of text for the purposes of this demo"

    CBI.cbSize = Len(CBI)
    Call GetComboBoxInfo(Combo1.hwnd, CBI)
 
    HookWindow = CBI.hwndList
    HookIt HookWindow
End Sub

' MUST unhook in the IDE before exiting, else it is goodnight VB time...
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Unhook HookWindow
End Sub
You will also need a module with the following code:
Code:
Option Explicit

' Declarations required for our subclassing
Public Const GWL_WNDPROC = (-4)
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) 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

' Positioning stuff
Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Public Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Const HWND_TOPMOST = -1
Private Const SWP_SHOWWINDOW = &H40

' Messages we are interested in
Private Const WM_MOUSEMOVE = &H200
Private Const WM_SHOWWINDOW = &H18

Private Const LB_ITEMFROMPOINT = &H1A9
Private Const LB_GETTEXT = &H189
Private Const LB_GETTEXTLEN = &H18A
Private Const LB_GETITEMRECT = &H198

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

Private lpOldProc As Long

Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Public vbToolTip As PictureBox


Public Function WindowFunction(ByVal hwnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
   Dim lIndex As Long
   Dim strTip As String
   Dim lBuffLen As Long
   Dim WindowRect As RECT
   Dim myRect As RECT
 
   ' Is it a message we want to respond to?
   Select Case iMsg
        Case WM_MOUSEMOVE
            lIndex = SendMessage(hwnd, LB_ITEMFROMPOINT, 0, ByVal lParam)
            If Not (lIndex And &H10000) Then
                lBuffLen = SendMessage(hwnd, LB_GETTEXTLEN, lIndex, 0)
                If lBuffLen > 0 Then
                    strTip = Space(lBuffLen + 1)
                    SendMessage hwnd, LB_GETTEXT, lIndex, ByVal strTip
                    strTip = Left(strTip, lBuffLen)
                    GetWindowRect hwnd, WindowRect
                    SendMessage hwnd, LB_GETITEMRECT, lIndex, myRect
                End If
                
                If Form1.TextWidth(strTip) / Screen.TwipsPerPixelX + 2 > myRect.Right - myRect.Left Then
                    SetWindowPos vbToolTip.hwnd, HWND_TOPMOST, WindowRect.Left, myRect.Top + WindowRect.Top, Form1.TextWidth(strTip) / Screen.TwipsPerPixelX + 6, myRect.Bottom - myRect.Top + 3, SWP_SHOWWINDOW
                    vbToolTip.Cls
                    vbToolTip.CurrentX = 2 * Screen.TwipsPerPixelX
                    vbToolTip.Print strTip
                Else
                   vbToolTip.Visible = False
                End If
            End If
        Case WM_SHOWWINDOW
            If wParam = False Then vbToolTip.Visible = False ' Closing list portion
        Case Else
    End Select
    
    ' Now do default stuff
    WindowFunction = CallWindowProc(lpOldProc, hwnd, iMsg, wParam, lParam)
End Function

Sub Unhook(hwnd As Long)
    If lpOldProc Then
        Call SetWindowLong(hwnd, GWL_WNDPROC, lpOldProc)
        lpOldProc = 0
    End If
End Sub

Sub HookIt(hwnd As Long)
    If Not lpOldProc Then
        lpOldProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowFunction)
    End If
End Sub
 
If you only want to display the complete string in the dropped down part of the combo, try the following.
I think I got this from TT a while back.(not sure when or where)


Copy this into the Declarations of the form.
Code:
Private Declare Function SendMessage Lib "user32"  Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Private Const CB_SETDROPPEDWIDTH = &H160



Private Sub SetComboWidth(Cbo As ComboBox)
    Dim max_wid As Long
        ' See how wide the widest ComboBox entry is.
    Dim I As Integer
    Me.ScaleMode = vbPixels
    max_wid = 0
    For I = 0 To Cbo.ListCount - 1
        If max_wid < TextWidth(Cbo.List(I)) Then
            max_wid = TextWidth(Cbo.List(I))
        End If
    Next I

    SendMessage Cbo.hwnd, CB_SETDROPPEDWIDTH, max_wid, 20
    Me.ScaleMode = vbTwips
End Sub

In formload just call this sub.

Hope this may be of help.

good luck

Bob
 
Yep, that is similar to code that a number of us have posted in this forum in the past. However it sadly doesn't address the original question, which specifiaclly states "without increasing the combo box width"...
 
Hi strongm & tudogs,

Woo..Both the code is working. It’s look amazing. Star for you

It not only solves my problem but I learn so many new concepts also.

Lots of thanks guys, to give me your valuable time.

Best Wishes
Pankaj

Senior Software Engineer,
Infotech Enterprises Limited
Hyderabad, Andhra Pradesh, India.
URL :
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top