Text Wrap in a combobox?
Text Wrap in a combobox?
Text Wrap in a combobox?
Text Wrap in a combobox?
Text Wrap in a combobox?
Text Wrap in a combobox? Text Wrap in a combobox? Text Wrap in a combobox? Text Wrap in a combobox? Text Wrap in a combobox? Text Wrap in a combobox? Text Wrap in a combobox? Text Wrap in a combobox?
Text Wrap in a combobox? Text Wrap in a combobox?
Text Wrap in a combobox?
Go Back  Xtreme Visual Basic Talk > > > Text Wrap in a combobox?


Reply
 
Thread Tools Display Modes
  #1  
Old 02-07-2013, 08:43 AM
Shisno Shisno is offline
Newcomer
 
Join Date: Jan 2013
Posts: 11
Default Text Wrap in a combobox?


I'm working on another program to pull data from an IBM db file, and I need to get some of the lengthier lines to wordwrap. Is there a command for doing this in a combobox?
Reply With Quote
  #2  
Old 02-07-2013, 03:37 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Can't see anything in the object properties to allow word wrap, but you might want to try making it wider to see the whole text.

ComboBox1.DropDownWidth = 500
Reply With Quote
  #3  
Old 02-07-2013, 04:25 PM
AtmaWeapon's Avatar
AtmaWeaponText Wrap in a combobox? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I don't have time to make an example today, but if you owner-draw the combo box, you can then measure the text given the width of the control and decide if it's going to wrap. Given that measurement result, you can make each item tall enough to handle the wrapped text, then render the text yourself.

Or look for a third-party control that already does it. Often they're worth their price.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 02-07-2013, 05:52 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Just a word of caution if you do try to make it from scratch like AtmaWeapon suggested... The length part of the text is tricky to get right. You can find the value of the text length using

ComboBox1.DropDownWidth = ComboBox1.SelectedText.Length

but that won't give you the proper length of the string contained in the combobox, it will give you more like a length of standard characters.

The problem is the length in pixels of a W is bigger than the length of an i even though they are both one char.

Fairly sure I did manage to do this a very long time ago, but not sure what project I could find it in.
Reply With Quote
  #5  
Old 02-07-2013, 06:10 PM
AtmaWeapon's Avatar
AtmaWeaponText Wrap in a combobox? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

It's easy once you have a Graphics object (I think the appropriate MeasureItem event provides one.) There's a MeasureString() method. The trick is making sure you tell it a width, ensuring the StringFormat or whatever parameter allows wrapping, and paying attention to the height it returns. I'll make an example tomorrow but right now it's not possible.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #6  
Old 02-08-2013, 06:30 AM
Shisno Shisno is offline
Newcomer
 
Join Date: Jan 2013
Posts: 11
Default

Would it be more simplistic to do an hscrollbar?
I thought of that as I was tossing and turning last night, but thusfar, I can't seem to get it to work either.
Reply With Quote
  #7  
Old 02-08-2013, 09:28 AM
AtmaWeapon's Avatar
AtmaWeaponText Wrap in a combobox? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I've attached a pretty short example. If it were me, I'd wrap it all in a custom ComboBox class, but you might feel different.

I don't think it'd be easier to deal with an HScrollbar, because the stock ComboBox control isn't really built to handle that in any way shape or form. Half the battle there would be trying to find a way to make the drop-down, which is designed to disappear when it loses focus, stick around while you click on the scrollbar. In general it's a bad idea to put long stuff in a combo box.
Attached Files
File Type: zip WrappingComboBox.zip (12.0 KB, 9 views)
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #8  
Old 02-08-2013, 09:49 AM
Gruff's Avatar
GruffText Wrap in a combobox? Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Although I prefer Atmas elegant solution
another option might be to increase the width of the dropdown only.

The interesting bit about this method is that the resulting dropdown can exceed the width of the form.

Code:
Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load With ComboBox1 ' Fill Combobox with sample text .Items.AddRange(New String() {"Badger", "Cow", "Eagle", "Wolf"}) .Items.Add("This is a very very long line. Lets see what we can do with it") .Items.AddRange(New String() {"Pigeon", "Sparrow", "Hawk", "Chicken"}) Dim MaxWidth As Integer = 0 ' Get Graphics object of the combobox Dim g As Graphics = ComboBox1.CreateGraphics ' Determine the longest line of text in the .items list. For Each s As String In .Items Dim CurrWidth As Integer = CInt(g.MeasureString(s, ComboBox1.Font, 1000).Width) If CurrWidth > MaxWidth Then MaxWidth = CurrWidth Next g.Dispose() .DropDownWidth = MaxWidth End With End Sub End Class
Attached Images
File Type: png LongCombo.png (29.4 KB, 5 views)
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 02-08-2013 at 02:55 PM.
Reply With Quote
  #9  
Old 02-08-2013, 11:33 AM
Shisno Shisno is offline
Newcomer
 
Join Date: Jan 2013
Posts: 11
Default

I tried the walkthrough example you showed me... and I think it's more than time for me to get over my newbie embarassment and just show you where I'm at.

o2.zip

When I switch the drawmode of my combobox from Normal to OwnerDrawVariable, all my data isn't visible, but is clickable to go to form2 and view.
Reply With Quote
  #10  
Old 02-08-2013, 02:56 PM
Gruff's Avatar
GruffText Wrap in a combobox? Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Added a Picture to my post above.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #11  
Old 02-08-2013, 05:44 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Quote:
Originally Posted by Shisno View Post
I tried the walkthrough example you showed me... and I think it's more than time for me to get over my newbie embarassment and just show you where I'm at.

Attachment 33113

When I switch the drawmode of my combobox from Normal to OwnerDrawVariable, all my data isn't visible, but is clickable to go to form2 and view.
Shisno what AtmaWeapon has given you there is actually surprisingly simple.
I have stepped through it, and it seems quite logical to follow even though I dont recognise everything he has used.

What I would suggest doing it cut and paste the code into your app. Shrink you ComboBox1. and copy the "theCombo" box off AW form onto yours that way all the settings should work.

That should then give you a working copy on your project.

Once it is working on your form I would replace some of this section..

Code:
    Protected Overrides Sub OnLoad(e As System.EventArgs)

        MyBase.OnLoad(e)
        ' Add a few long items to the combo box; enough to test what happens if there's a vertical scrollbar.
        Dim items() As String = {"one", "two", "three", "I'm really long and should wrap", "five", "six", "seven", "eight", "nine", _
                   "Ten items should be enough. Really long items aren't really a good idea but here goes."}
        For Each item As String In items
            theCombo.Items.Add(item)
        Next

    End Sub
Basically he is just loading some test data into a string array. All you would do is insert your database data into this section.

Now you would have you data in his combobox. Then see if you can resize his combobox without upsetting his code.

I notice you are using two different views, yours is using "Simple" his is using "DropDown" just try changing one thing at a time until it looks like you need it.

Last edited by CodeCruncher; 02-09-2013 at 05:02 PM.
Reply With Quote
  #12  
Old 02-08-2013, 05:58 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Here you go a couple of tweaks to AtmaWeapons existing code. No code changes, just a couple of graphical tweaks to match you existing ComboBox1.

Now you could get rid of your ComboBox1 altogether and just use the one AtmaWeapon created.
Attached Images
File Type: jpg Modified ComboBox.jpg (105.0 KB, 6 views)
Attached Files
File Type: zip WrappingComboBox 2.zip (16.5 KB, 10 views)
Reply With Quote
  #13  
Old 02-09-2013, 07:29 AM
AtmaWeapon's Avatar
AtmaWeaponText Wrap in a combobox? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

There is code behind forms, and sometimes you have to do more than drag, drop, and click the property grid to write a program. I did a quick skim and it doesn't look like you did anything anywhere to handle the MeasureItem or DrawItem events, and that's where I spent 28 minutes of the half-hour I spent on the example.

If you have questions about how to handle events, feel free to ask, but please look at more than the form designer when given an example. The designer is boring and takes 10 minutes for you to learn everything it can do. It's in code where magic happens.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #14  
Old 02-10-2013, 03:21 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

If I didn’t already know how it worked, this really hits the nail on the head..

1. Set the DrawMode to OwnerDrawVariable. When you do this, you're telling the ComboBox, "I would like to do my own drawing for each item, and not all items are the same size." When set to this mode, when the control needs to draw its items it will raise the MeasureItem event to get the size and the DrawItem event to draw the item.
2. Handle the combo box's MeasureItem event. It tells you which item you're measuring with the e.Index parameter. I use the Graphics.MeasureString() method to get the size of the string. Then I set the e.ItemHeight and e.ItemWidth parameters to the size I want for the item.
3. Handle the combo box's DrawItem event. It tells you which item you're drawing via the e.Index parameter and gives you a Graphics object via the e.Graphics parameter. I let Windows draw the item's background, then draw the string inside the rectangle, letting Windows wrap it.
That is a really great summary of what is taking place. I’m sure this will help Shisno understand your code much better.

Not wanting to highjack his thread but this is the only bit I don’t fully understand.

Protected Overrides Sub OnLoad(e As System.EventArgs)

MyBase.OnLoad(e)

I have never used a Protected Overrides Sub before why use it instead of a Form Load?
Then it would appear with MyBase.Onload(e) you are calling the event from within itself?
Reply With Quote
  #15  
Old 02-10-2013, 07:00 PM
AtmaWeapon's Avatar
AtmaWeaponText Wrap in a combobox? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Good question!

There's no difference between doing that and handling the form's Load event. But the pattern most programs use to implement events looks like this:
Code:
Public Event TheEvent as EventHandler

Protected Overridable Sub OnTheEvent(e As EventArgs)
    RaiseEvent TheEvent(Me, e)
End Sub
The main reason is because a derived class might want to raise the event, but only the exact class that contains an event can use RaiseEvent. So derived classes can call the "On" method to force it to be raised. In addition, they're free to override it instead of handling it. Further, if you opt to not call that MyBase line, you can cause the event to not be raised. It's really rare that you'd want to do this, but it's nice to know it's there.

Not every event follows the pattern, and not ever event has an "On" method. Generally that's a subtle hint from the class's designer that they don't intend for derived classes to do much with the event, or at least they didn't expect it.

From the perspective of this tutorial, it's no different than if it were:
Code:
Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
I typically override the "On" methods when I can because it's easy to look at the list of methods in a derived class and pick out event handlers that way. I've got a convention of naming non-"On" handlers with "Handle_blah", but not everyone does and that means it's not always easy to pick out event handlers. For example, at work I'm dealing with a project where the event handler for Form.Shown is named "HandleCommandLineArgs". This would have been so much more clear:
Code:
Overrides OnShown(...)
   HandleCommandLineArgs()
End Sub
(Even though I think Shown is one of those events without an On method.)
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #16  
Old 02-11-2013, 01:23 PM
Shisno Shisno is offline
Newcomer
 
Join Date: Jan 2013
Posts: 11
Default

Thank you for all your hard work on this, I've implemented your suggestion fully and I'm happy to report that the wordwrap is working. I was even able to solve another little problem with my key fields not lining up to the wordwrapped descriptions on my own.

Edit:
Deleted text as off topic

Last edited by Gruff; 02-13-2013 at 10:00 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Text Wrap in a combobox?
Text Wrap in a combobox?
Text Wrap in a combobox? Text Wrap in a combobox?
Text Wrap in a combobox?
Text Wrap in a combobox?
Text Wrap in a combobox? Text Wrap in a combobox? Text Wrap in a combobox? Text Wrap in a combobox? Text Wrap in a combobox? Text Wrap in a combobox? Text Wrap in a combobox?
Text Wrap in a combobox?
Text Wrap in a combobox?
 
Text Wrap in a combobox?
Text Wrap in a combobox?
 
-->