C
C#17mo ago
PotatOBiTz

✅ 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
HimmDawg
HimmDawg17mo ago
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 combobox
PotatOBiTz
PotatOBiTzOP17mo ago
ah sure hang on
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace g7ccs
{
public partial class frmFont : Form
{
public frmFont()
{
InitializeComponent();
cboFont.DrawItem += cboFont_DrawItem;
/*foreach(FontFamily fontfamily in FontFamily.Families)
{
cboFont.Items.Add(fontfamily.Name);
}
*/
cboFont.DataSource = System.Drawing.FontFamily.Families.ToList();
}

private void cboFont_DrawItem(object sender, DrawItemEventArgs e)
{
var comboBox = (ComboBox)sender;
var fontFamily = (FontFamily)comboBox.Items[e.Index];
var font = new Font(fontFamily, comboBox.Font.SizeInPoints);

e.DrawBackground();
e.Graphics.DrawString (font.Name, font, Brushes.Black, e.Bounds.X, e.Bounds.Y);
}

private void cboFont_SelectedIndexChanged(object sender, EventArgs e)
{
string sFontName = cboFont.SelectedItem.ToString();
Font selectedFont = new Font(sFontName, lblSample.Font.Size, lblSample.Font.Style);
lblSample.Font = selectedFont;
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace g7ccs
{
public partial class frmFont : Form
{
public frmFont()
{
InitializeComponent();
cboFont.DrawItem += cboFont_DrawItem;
/*foreach(FontFamily fontfamily in FontFamily.Families)
{
cboFont.Items.Add(fontfamily.Name);
}
*/
cboFont.DataSource = System.Drawing.FontFamily.Families.ToList();
}

private void cboFont_DrawItem(object sender, DrawItemEventArgs e)
{
var comboBox = (ComboBox)sender;
var fontFamily = (FontFamily)comboBox.Items[e.Index];
var font = new Font(fontFamily, comboBox.Font.SizeInPoints);

e.DrawBackground();
e.Graphics.DrawString (font.Name, font, Brushes.Black, e.Bounds.X, e.Bounds.Y);
}

private void cboFont_SelectedIndexChanged(object sender, EventArgs e)
{
string sFontName = cboFont.SelectedItem.ToString();
Font selectedFont = new Font(sFontName, lblSample.Font.Size, lblSample.Font.Style);
lblSample.Font = selectedFont;
}
PotatOBiTz
PotatOBiTzOP17mo ago
when I select something it shows [Font Family: Name = <Font Name>]. I wish for it to be like "Calibri"
PotatOBiTz
PotatOBiTzOP17mo ago
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
HimmDawg
HimmDawg17mo ago
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 combobox
PotatOBiTz
PotatOBiTzOP17mo ago
yep, I've enabled this to OwnerDrawFixed
HimmDawg
HimmDawg17mo ago
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 owo
PotatOBiTz
PotatOBiTzOP17mo ago
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 understanding
HimmDawg
HimmDawg17mo ago
So what exactly is it that is hard to understand? 😄
PotatOBiTz
PotatOBiTzOP17mo ago
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
HimmDawg
HimmDawg17mo ago
So this screenshot here is not the current state?
PotatOBiTz
PotatOBiTzOP17mo ago
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
PotatOBiTz
PotatOBiTzOP17mo ago
HimmDawg
HimmDawg17mo ago
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?
PotatOBiTz
PotatOBiTzOP17mo ago
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
Accord
Accord17mo ago
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.
Want results from more Discord servers?
Add your server