Align text to the left in a combobox

Posted on 2008-11-12
Last Modified: 2012-05-05
Simple Question I think.

I have a combobox. The selected item is longer than the box. I end up seeing the end rather than the start.

Apart from making the box bigger or reducing the length of the items how can i make the selected item display from the start in the combobox??

I need to make the combobox show the start of the string not the end!
Question by:si2030
    LVL 16

    Accepted Solution

    The first though that comes to mind is to create an array that is logically linked to the combobox.  The idea would be to have the "true" value of the combobox to be stored in the array.  Then load the combobox with only the Left(N) characters from the array (followed by ellipses if the string exceeds a certain size).
    LVL 15

    Assisted Solution

    by:David L. Hansen
    If you are only truncating a smalll amount, try changing the size of the font.

    ComboBox1.Font = New Drawing.Font("Comic Sans MS", FontStyle.Bold) 
    ComboBox1.ForeColor = Color.Black 

    Open in new window

    LVL 15

    Expert Comment

    by:David L. Hansen
    Sorry, I left out the size (note the "8" below):

    ComboBox1.Font = New Drawing.Font("Comic Sans MS", 8, FontStyle.Regular)

    Open in new window

    LVL 12

    Assisted Solution

    Hello, si2030,

    I like HooKooDooKu's idea of adding the ellipses.  But that might be more complicated than you like.  So my next thought was perhaps you are just looking for something like "ComboBox1.SelectionStart = 0".

    That seemed to work just fine, provided that I invoked it from a Button_Click event.  But I couldn't get it to work from any of the likely ComboBox events.  

    Finally was forced into using "ComboBox1.SelectionLength = 0" invoked from a Timer_Tick event where the timer (with an interval of one millisecond) was triggered by the ComboBox SelectionChangeCommitted event.    Well, that's a bit kludgy, but it seems to work.

    The attached snippet shows an example.


    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ComboBox1.Items.Add("1. this is the first quite rather long long long string")
            ComboBox1.Items.Add("2. this is the second quite rather long long long string")
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            ComboBox1.SelectionStart = 0
        End Sub
        Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
        End Sub
        Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            ComboBox1.SelectionLength = 0
        End Sub
    End Class

    Open in new window


    Author Comment

    In the end I have forced the user to shange focus to the next control. This way it removes the selected area and resets the string to start from the start of the combobox...

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    729 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