[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

ComboBox text longer than the combo visible at the end instead of start

Posted on 2011-09-27
5
Medium Priority
?
270 Views
Last Modified: 2012-05-12
My problem is ComboBox, with the text of the first item longer than the combo - I don't see the text from the start, I see the end of the text. I wanna see text from the start. I've tried 'SelectionStart' property set to 0, but it has nothing to do with my problem - it only sets where from text is selected.

Longer explanation:

I have several combo boxes on my form, which I have to adjust theirs drop down list width to be suitable for the longest item. So, when I open drop down list, it's width is long as the longest item is. But, there is a problem - I have a special function, which goes through list of combo boxes and made theirs drop down width long as the longest item:

 Public Sub MakeDropDownListWider()
        Dim conKontrola As ComboBox
        conKontrola = Me

        'make the dropdown wider so the entire selection can be seen
        If conKontrola.Items.Count > 0 Then
            Dim pixlength As Graphics = Graphics.FromHwnd(New IntPtr)
            Dim lengthHolding As Int32
            Dim stringWidth As Int32
            Dim g As Graphics = conKontrola.CreateGraphics

            For Each myItem As Object In conKontrola.Items

                If myItem.GetType().ToString() = "System.Data.DataRowView" Then
                    lengthHolding = pixlength.MeasureString(myItem.Row.Item(conKontrola.DisplayMember).ToString, conKontrola.Font).ToSize.Width
                Else
                    lengthHolding = g.MeasureString(myItem, conKontrola.Font).Width + 15
                End If

                If lengthHolding > stringWidth Then
                    stringWidth = lengthHolding
                End If
            Next


            Dim allowedWidth As Int32 = 0
            If Me.Parent.Width > 0 Then                
                allowedWidth = Me.Parent.Width - conKontrola.Location.X - 10
            End If
            If allowedWidth > 0 And (stringWidth + 15 > allowedWidth) Then
                conKontrola.DropDownWidth = allowedWidth
            Else
                conKontrola.DropDownWidth = stringWidth + 15 'add 15 for the scrollbar
            End If

        End If
    End Sub


When I run this function on every combo box of my form, all of my combo boxes are selected after the form is shown. (I call this method on forms shown event). I didn't wanted combo boxes to be selected, so I used SelectionStart property of combo box, like this:

myComboBox1.SelectionStart = myComboBox1.Text.Length

That way, none of the combo boxes appears to be selected. But, another problem has arised: I see only end of the selection on my combo boxes. If the first item is short, then everything is cool. But, if the first item is longer than the combo box, I see only the end of the item - but, I must the item from the start.

So, f.e.: my first item is: "C# is a very good programming language designed by Anders Hejlsberg" and my combo is shorter than the text, I will see only "designed by Anders Hejlsberg." - I wanna see "C# is a very good programming".

I can't move the 'SelectionStart' property to 0, because all of my combo boxes gets selected again. And even if I do that, I still see end of the first item, not the start - the only difference is that item is selected.

Any idea how to see the text of the first item from the start?


Greetings, Frenky
0
Comment
Question by:AntonioRodrigo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:himanshut
ID: 36714838
Try increasing the Width property for ComboBox to some pixels.

Ideally something like <asp:ComboBox:cB1. ...... Width="50px" > 

You may need to adjust the pixels value according to your largest text
0
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 36714857
Does this do the trick?

myComboBox1.SelectionStart = 0
myComboBox1.SelectionLength = 0
0
 

Author Comment

by:AntonioRodrigo
ID: 36714865
I can't do this - I have a lot of combo boxes on my form, inside containers. There is a very little space between them and other controls.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 36714897
and my suggestion?
0
 

Author Closing Comment

by:AntonioRodrigo
ID: 36714953
Very good job on this solution. Thank you.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

656 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