Hi,
Is it possible to change the FontStyle of the text displayed in the combobox while the font of all the objects in the item collection remains the same.
Thanks!
Windows Forms General
Hi,
Is it possible to change the FontStyle of the text displayed in the combobox while the font of all the objects in the item collection remains the same.
Thanks!
Yes but it requires creating a custom combobox. Download the following sample:
http://www.codeproject.com/cs/miscctrl/ImageComboBoxControl.asp
Hi we can add drawItem event to the combo and control font ,color, and so on using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace zbo
{
public partial class Customer_Combo : Form
{
public Customer_Combo()
{
InitializeComponent();
}
private void Customer_Combo_Load(object sender, EventArgs e)
{
this.comboBox1.Items.Add("item1");
this.comboBox1.Items.Add("item2");
}
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
string a = (string)comboBox1.Items[e.Index];
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
e.Graphics.DrawRectangle(new Pen (new SolidBrush(Color.Red)), e.Bounds);
if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
{
e.Graphics.FillRectangle(new SolidBrush(Color.White), e.Bounds);
e.Graphics.DrawString(a, new Font("Veranda", 12, FontStyle.Italic), new SolidBrush(Color.Blue), e.Bounds, sf);
e.DrawFocusRectangle();
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.Yellow), e.Bounds);
e.Graphics.DrawString(a, new Font("Veranda", 12, FontStyle.Italic), new SolidBrush(Color.Blue), e.Bounds, sf);
e.DrawFocusRectangle();
}
}
}
}
Thanks a lot for the suggestions. The problem I am facing is that I have two ComboBoxes, comboBox1 and comboBox2. When the user selects something from comboBox1 then comboBox2 is automatically populated. I need to set the font style of this automatic selection as italic.
It will be great if someone can guide me ho wto do this.
I think the example above can used to your comboBox2, and change font italic in drawitem function,
and I think you can hide the comboBox2 first and show() it to realize automatically populated function~:)
The link to the sample I provided earlier will display your Italic Font in the combo.text if you select dropdownlist as the style.
You don't have to provide images either if that is your concern with using that sample.