Solved

Resize Combobox

Posted on 2004-10-07
10
2,512 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

14 Experts available now in Live!

Get 1:1 Help Now