locked
Narrowing Font in Combobox RRS feed

  • Question

  • How can i change font to narrow if length of text inside a Combobox1 exceeds 20 characters

    i can change the font size by using this command:

    '----keep font size at 8 unless the length of text exceeds 20 characters---------

    if Len(My_Form.Combobox1)>20 then My_Form.Combobox1.Font.Size = 6 Else My_Form.Combobox1.Font.Size = 8

    is there a way??

    Wednesday, August 8, 2018 8:26 AM

All replies

  • Hi

    Can try the code below:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ComboBox1.Items.Add("1")
            ComboBox1.Items.Add("234")
            ComboBox1.Items.Add("abcdefg")
            ComboBox1.DroppedDown = True
            ComboBox1.DrawMode = DrawMode.OwnerDrawVariable
        End Sub
    
        Private Sub ComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ComboBox1.DrawItem
            Dim g As Graphics = e.Graphics
            Dim bFont As Font = New Font("Arial", 8, FontStyle.Bold)
            Dim lFont As Font = New Font("Arial", 10, FontStyle.Bold)
    
    
            If ComboBox1.Items(e.Index).ToString.Length > 5 Then
                g.DrawString(ComboBox1.Items(e.Index).ToString(), lFont, Brushes.Red, e.Bounds.X, e.Bounds.Y)
            Else
                g.DrawString(ComboBox1.Items(e.Index).ToString(), bFont, Brushes.Black, e.Bounds.X, e.Bounds.Y)
            End If
    
        End Sub
    
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 8, 2018 9:20 AM
  • MC,

    I agree with Alex you will need to draw your own text to change the font for one item.

    However, that is not a very good technique in general. There are so many different possible screen sizes and scalings if you are running on several systems you will still have problems still I am sure. The 20 character limit will change requirements. Furthermore what happens when one item is 19 chars and one is 20. I don't think that will draw correctly?

    Why are you doing this? Fitting the combo into a fixed space?

    Wouldn't it be better to reduce the number of characters instead of changing the font?

    For example, if the word is "twentycharacters" then change it to   "twentychar..." or "twenty...ers" similar to how we change file paths when too long to fit the box.

    Finally, if you do go with the font change dont use a size smaller than 8 points or most cant read it anyway.

    :)

    Wednesday, August 8, 2018 11:08 AM
  • Hi

    Why not adjust the drop down width to suit the longest item?


    Regards Les, Livingston, Scotland

    Wednesday, August 8, 2018 1:30 PM
  • Hi

    Here is some example code. This example has a ComboBox1 with font size 14 and at run time, the DropDownWidth is set to an initial 250 and some sample strings added - strings increasing in length.

    When the ComboBox is opened, the DropDownWidth is adjusted to suit longest string, and, the longest is removed each time ComboBox is opened). So, to test, Open/Close the ComboBox several repeatedly and observe the width of the DropDown list.

      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim ts As String = "Test String Length"
        For i As Integer = 0 To 20
          ComboBox1.Items.Add(ts)
          ts &= "X"
        Next
        ComboBox1.DropDownWidth = 250
      End Sub
      Private Sub ComboBox1_DropDown(sender As Object, e As EventArgs) Handles ComboBox1.DropDown
        Dim wdth As Integer = 250
        Dim fnt As Font = ComboBox1.Font
        For Each s As String In ComboBox1.Items
          Dim w As Integer = TextRenderer.MeasureText(s, fnt).Width
          If w > wdth Then
            wdth = w
            ComboBox1.DropDownWidth = wdth
          End If
        Next
        ComboBox1.Items.RemoveAt(ComboBox1.Items.Count - 1)
      End Sub


    Regards Les, Livingston, Scotland

    Wednesday, August 8, 2018 1:52 PM