Solved

Long text in a combo box

Posted on 2000-03-10
7
185 Views
Last Modified: 2010-08-05
Hi,

I am having a problem with the combo box. I have set the physical size of the control (which is already pretty long) quite long because of the text it holds (some really long sentences). So when I select the combo box, any text that is long it cuts the end off.

I have two questions about this:

1. Can I set a property on this control, so when its selected it opens up to the size of the longest text in it. Or..

2. If the text is longer than the length of the control, automatically place a vertical control bar, so the user can scroll across to see the full length of the option.

Can both of these be accomplished. Let me know.

Thanks.

Mari
0
Comment
Question by:mari_carmen
7 Comments
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2606241
You can go thru all the items in the combobox checking the width of all items, using code like this...

  Dim a As Integer
  Dim MaxWidth As Long
  Dim CurrWidth As Long
  Dim MeFont As New StdFont
 
  'If you want you can set MaxWidth to prevent the Combobox
  'to be more narrow than a specific value
  'MaxWidth = (MinWidth)

  MeFont = Me.Font
  Me.Font = Combo1.Font
  For a = 1 To Combo1.ListCount
    CurrWidth = Me.TextWidth(Combo1.List(a - 1))
    If (CurrWidth > MaxWidth) Then MaxWidth = CurrWidth
  Next
  Combo1.Width = MaxWidth + 30 * Screen.TwipsPerPixelX
  Me.Font = MeFont


This code uses the Me.Font property (only temporary, the property is restored when the code has executed) to be able to use the Me.TextWidth function, if you do not want to use this way there is a way to calculate the width of a string using API's.. a little bit more complex but it's doable. Let me know and I can post that code.
0
 
LVL 15

Expert Comment

by:ameba
ID: 2606356
Use ToolTipText property of your combobox to present the whole text, e.g.:

Option Explicit

Private Sub Form_Load()
    Combo1.AddItem "Normal Item1"
    Combo1.AddItem "Normal Item2"
    Combo1.AddItem "Very, very, very, very, very, very, long Item1"
    Combo1.AddItem "Very, very, very, very, very, very, long Item2"
End Sub

Private Sub Combo1_Change()
    ' make sure form and Combo1 use the same font
    If Me.TextWidth(Combo1.Text) > Combo1.Width Then
        Combo1.ToolTipText = Combo1.Text
    Else
        Combo1.ToolTipText = ""
    End If
End Sub
0
 

Author Comment

by:mari_carmen
ID: 2606376
VBmaster,

The problem is I do not want to change the size of the actual combo box.

When you click the combo and it shows the list, is there anyway of displaying that part (list part) to the length of the longest text.

Mari
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:ameba
ID: 2606379
'or
Private Sub Combo1_Change()
    If Me.TextWidth(Combo1.Text) > Combo1.Width - 300 Then
        Combo1.ToolTipText = Combo1.Text
    Else
        Combo1.ToolTipText = ""
    End If
End Sub

Private Sub Combo1_Click()
    Combo1_Change
End Sub
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2606392
See the code:

"Changing the Dropdown Width of a Combo Box, Advanced"

http://www.mvps.org/vbnet/code/listapi/combowidthadv.htm
0
 
LVL 15

Accepted Solution

by:
ameba earned 80 total points
ID: 2606394
Change the width of the dropdown portion of a ComboBox:
http://www.vb2themax.com/Item.asp?PageID=TipBank&ID=115

width is in pixels, to calculate it, use
 pw = Textwidth(Combo1.Text) / 15
0
 

Author Comment

by:mari_carmen
ID: 2609361
Thanks, thats perfect for what i need.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Controlling which port to download from 4 71
Restricting text box entry from \/:?<>"| 9 77
Excel object stays open 19 71
change vba from autofit to 13.5 width? 4 28
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now