Solved

Resize Combobox

Posted on 2004-10-07
10
2,517 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

806 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