[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 997
  • Last Modified:

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!
0
si2030
Asked:
si2030
3 Solutions
 
HooKooDooKuCommented:
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).
0
 
David L. HansenProgrammer AnalystCommented:
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

0
 
David L. HansenProgrammer AnalystCommented:
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

0
 
omegaomegaDeveloperCommented:
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.

Cheers,
Randy

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
        Timer1.Start()
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Stop()
        ComboBox1.SelectionLength = 0
    End Sub
 
End Class

Open in new window

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now