Solved

Resize Combobox

Posted on 2004-10-07
10
2,521 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
[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
  • 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
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!

 
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

Industry Leaders: 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!

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

739 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