473,385 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

DrawItem event of combo box

Hi There

I have wired up an event handler to a combo boxes DrawItem event, and I
want to draw a string that is vertically centered in the combo box.

At the moment I am doing it like this:

int centeredY = e.Bounds.Y + (int)((e.Bounds.Height -
mf.GetHeight(e.Graphics))/2);

Is there an easier way ? FYI, here is a more complete listing:

private void cmbFonts_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
if (e.Index == -1 || sender == null)
return;

ComboBox cboFont = (ComboBox)sender;
FontFamily ff = new FontFamily(cboFont.Items[e.Index].ToString());
Font mf = new Font(ff.Name,10,GetFontStyle(ff));

e.DrawBackground();
e.DrawFocusRectangle();

int centeredY = e.Bounds.Y + (int)((e.Bounds.Height -
mf.GetHeight(e.Graphics))/2);

e.Graphics.DrawString(ff.Name, mf,new
SolidBrush(e.ForeColor),e.Bounds.X,centeredY);

}

private FontStyle GetFontStyle(FontFamily ff)
{
FontStyle mfs = FontStyle.Regular;
if(!ff.IsStyleAvailable(mfs))
mfs = FontStyle.Italic;
if(!ff.IsStyleAvailable(mfs))
mfs = FontStyle.Bold;
return mfs;
}

Cheers
Bill

Nov 17 '05 #1
0 1341

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: pothik05 | last post by:
I can not make the DrawItem event to work on the CheckedListBox control. I suggestion will be highly appreciated. I have the following code. The code works for ListBox and the second item (Two)...
0
by: Krisa | last post by:
Hello all, I just discovered something (stop me if you've heard this before....) that was causing me a significant performance hit when opening a form with subforms. To speed up loading the...
0
by: Jim H | last post by:
I'm trying to use an owner draw ListBox by subscribing to the DrawItem event to draw my custom control as an item in the ListBox. This event is triggered when I drag the scroll button but NOT when...
1
by: e | last post by:
I have an ownerdrawn listbox that paints items in various colors dependant upon whether or not it has focus, selected, etc. The code that physically paints the items is in the drawItem event, and...
6
by: Panos | last post by:
Hi everyone I'm trying to understand what property or method raises the drawitem event of a statusbar control. I've been struggling to draw a progressbar, but i can't make it work. Thank u all
0
by: Paul_Madden via DotNetMonster.com | last post by:
I am handling the Listbox DrawItem event to enable the listbox strings to be displayed in different colours for easy reading. Have set ScrollAlwaysVisible and HorizontalScrollbar to true. I...
2
by: Paul_Madden via DotNetMonster.com | last post by:
I am handling the Listbox DrawItem event to enable the listbox strings to be displayed in different colours for easy reading. Have set ScrollAlwaysVisible and HorizontalScrollbar to true. I...
0
by: Smokey Grindle | last post by:
This is just a wierd one... I am trying to draw the sub items in the drawitem event of the list view in owner drawn mode (because of documented W32 rendering bugs) instead of in two seperate items...
0
ChristopherJohn
by: ChristopherJohn | last post by:
I'm new to this so if this is posted in the wrong area, please advise. I'm using the DrawItem event to change the color of the text for the selected tab of a TabControl with the tabs on the top. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.