✅ Changing the individual font of items in a Combo Box - C# Windows Forms
Hello! I'm making a font changer windows forms program as a practice program and I want to change the font of the items listed in a combo box, and make it display the selected item as it is. I've been trying for a while, but I don't know what to do exactly. I've been seeing answers of using the DrawItem event, and I've tried something I've seen in a Stack Overflow post, but I don't really understand what's going on in that code and the combo box displays [Font Family: Name = <Font Name>] instead of "<Font Name with its respective font style>" when I select an item. I've been trying to modify it for a while, trying to declare variables to make it only show its face name with .Name, but I get errors. What should I do? Thanks in advance
16 Replies
DrawItem
is the correct approach. In fact, you are pretty close with this. You mentioned that you get something cryptic instead of a font name. Can you show me the DrawItem event and what you set as your DataSource for the combobox?
And you'll probably have to change OwnerDraw
of your comboboxah sure hang on
when I select something it shows [Font Family: Name = <Font Name>]. I wish for it to be like "Calibri"
I tried to declare variables to hold the System.Drawing.FontFamily.Families, then put it inside cboFont.DataSource (something like a cboFont.DataSource = font.Name) so I can put a .Name on the variable, but ig that doesn't work. I've never worked with DrawItem events nor .DataSource, this is perhaps my first time trying to understand these
Ok, the baseline is:
DataSource
is a collection of objects. Any objects at all. The combobox will then display the .ToString()
representation of those objects.
DrawItem
is used to change the way the combobox items are drawn. That happens on a per item-basis
For DrawItem to take effect, you'll need to enable OwnerDraw
on your comboboxyep, I've enabled this to
OwnerDrawFixed
Good
So usually, you put the list of font families as datasource and in
DrawItem
you get the family and draw the name of it. I guess you did that already perhaps I did, this came from the code snippet I found from stack overflow btw, I don't completely understand what is happening aside from that
DataSource
is a collection of objects and that DrawItem
changes how a combo box/listbox is drawn. The rest I have a vague understandingSo what exactly is it that is hard to understand? 😄
hmm, I want the combo box to display "Calibri" for example instead of "[Font Family: Name=Calibri]" when I select it from the list of items. But I don't know what's causing it to be like that, and which to change
So this screenshot here is not the current state?
this is the current state. As you can see in the combo box it displays [Font Family: Name = <Font Name>] instead of just the font name
Oh there. I haven't noticed that.
But that's odd. I have copied your code and that does in fact work for me. So there must be something else with your combobox
Ah hold on. Can you change
DropDownStyle
to DropDownList
?oh sure I'll try that
Aaaah there we go
This has been troubling me since this morning. And it was the work of one setting
Thanks for the help :3
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.