Align text to the left in a combobox

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!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David L. HansenCEOCommented:
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

David L. HansenCEOCommented:
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

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

si2030Author Commented:
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...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.