Solved

Resize Combobox

Posted on 2004-10-07
10
2,507 Views
Last Modified: 2012-05-05

i want to resize my comboboxes to match the size of the textboxes.

i've seen that i can use:
cbo.DrawMode = DrawMode.OwnerDrawFixed
cbo.ItemHeight =15

to specify a custom height.

the problem is that once i do that i can't see any items in the comboxbox dropdown. The dropdown is the correct height for the number of items, and if i click inside the dropdown it selects the text thats there, just i can't see the text.

i'm thinking i need to capture some event and maybe call the base class, just not sure which one or if i'm on the right track.
0
Comment
Question by:mpdalton
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12255360
When you set the drawMode property of the comboBox to DrawMode.OwnerDrawFixed, you have to draw you items by code, you are indicating that you want to paint your control as you want, thats the reason because you see nothing.
0
 
LVL 3

Author Comment

by:mpdalton
ID: 12255691
any code snippets that might show me how to do that ?
0
 
LVL 19

Assisted Solution

by:arif_eqbal
arif_eqbal earned 125 total points
ID: 12256388
in the DrawItem Event of the ComboBox write

 e.DrawBackground()
 e.Graphics.DrawString(ComboBox1.Items(e.Index), e.Font, Brushes.Blue, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))

0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12256393
sorry I wrote Brushes.Blue
it will show the Items in Blue color make it Brushes.Black
0
 
LVL 5

Assisted Solution

by:naiea1231
naiea1231 earned 375 total points
ID: 12256396
This is what you need

   Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
        ' Set the DrawMode property to draw fixed sized items.
        ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
        ' Draw the background of the ListBox control for each item.
        e.DrawBackground()
        ' Create a new Brush and initialize to a Black colored brush by default.
        Dim myBrush As Brush

        ' Determine the color of the brush to draw each item.
        myBrush = Brushes.Black

        ' Draw the current item text based on the current Font and the custom brush settings.
        e.Graphics.DrawString(ComboBox1.Items(e.Index), e.Font, myBrush, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
        ' If the ListBox has focus, draw a focus rectangle around the selected item.
        e.DrawFocusRectangle()
    End Sub

It basically draws in all the elements of the combobox as by changing the DrawMode to OwnerDrawFixed, the operating system no longer draws in the items, and this has to be done manually. Give that a go, it will allow you to use any item height.

James
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Expert Comment

by:naiea1231
ID: 12256405
My apologies, 3 minutes slow ;), arif_eqbal has the same code.
0
 
LVL 3

Author Comment

by:mpdalton
ID: 12272588
thanks for your replies.

the comboboxes i'm using are bound to a datatable to when the e.graphics.drawstring method is called it generates an error:

"Cast from type 'DataRowView' to type 'String' is not valid."

is it possible to use this method with a bound combobox ?
0
 
LVL 5

Accepted Solution

by:
naiea1231 earned 375 total points
ID: 12273530
Just had a look around for you and there is a good example here:
http://www.dotnet247.com/247reference/msgs/40/203845.aspx

You'll just have to adjust the listbox to combobox, as the same commands should apply to both without any issues. Let us know how it goes.

James
0
 
LVL 5

Expert Comment

by:naiea1231
ID: 12273534
Keep in mind that example is in C#, but you can see how it applies to VB.NET, however if you need a hand converting just ask.

James
0
 
LVL 3

Author Comment

by:mpdalton
ID: 12273567
Brilliant, thanks everyone it's working perfectly now ... finally all my controls are the same height.

i'm a happy man :-)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

21 Experts available now in Live!

Get 1:1 Help Now