

In that case this code scales the image to fit nicely.

When the program is drawing the item on the control’s surface, however, the available size will be the size of the control, which is probably smaller than the space desired. When the program is drawing the ComboBox control’s dropdown list, this width and height will match the space available because that was specified by the MeasureItem method. It then gets the height available to draw the picture and calculates the width it should use to make the picture fit without stretching it. The method starts by calling e.DrawBackground to draw the appropriate background. Draw the focus rectangle if appropriate. Sf.LineAlignment = StringAlignment.Center Į.Graphics.DrawRectangle(Pens.Blue, Rectangle.Round(rect)) Using (StringFormat sf = new StringFormat()) Wid = e.Bounds.Width - rect.Right - 3 * MarginWidth Visible_text = Text.Substring(0, Text.IndexOf('\n')) Public void DrawItem(DrawItemEventArgs e)įloat hgt = e.Bounds.Height - 2 * MarginHeight The following code shows the class’s DrawItem method. The method then returns the calculated width and height. It sets the necessary width to the sum of the picture’s width and the text’s width (plus 4 times the margin width, once for each side and twice for spacing between the picture and text). It sets the necessary height to be the larger of the picture’s height and the text’s height (plus top and bottom margins). It uses the e.Graphics object’s MeasureString method to see how big the string will be when drawn. The MeasureItem method calculates the width and height if it has not yet done so.

The SizeCalculated variable keeps track of whether the width and height have been calculated yet. The private Width and Height variables store the calculated width and height so the object doesn’t need to calculate those values more than once. This method returns the amount of space needed to draw the ImageAndText object’s item. The width is the sum of the image and text widths. (int)Math.Max(Picture.Height, text_size.Height) The height is the maximum of the image height SizeF text_size = e.Graphics.MeasureString(Text, Font) Public void MeasureItem(MeasureItemEventArgs e) Set the size needed to display the image and text. The following code shows the class’s MeasureItem method. The constructor simply initializes the Picture, Text, and Font fields. It stores the font it will use to draw the text in its Font field. The class stores the image and text it will draw in its Picture and Text fields.

Public ImageAndText(Image picture, string text, Font font) Margins around owner drawn ComboBoxes. The following code shows the class’s fields, declaration, and constructor. To make it easier to lay out the items, the ImageAndText class also provides methods to measure and draw its item. Instead this example uses the ImageAndText class to hold an item’s image and text. This example needs to display both images and text so it can’t simply put Images in the Items collection. The previous example places Color or Image objects in the ComboBox control’s Items collection.
#VB NET 2010 HOW TO SHOW TEXT IN COMBOBOX PLUS#
This example shows how to make a ComboBox that displays a series of images plus associated text. The example Make a ComboBox display colors or images in C# shows how to make an owner-drawn ComboBox that displays either a series of color samples or a list of images.
