akin_l

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!



Re: Windows Forms General ComboBox Font

element109

Yes but it requires creating a custom combobox. Download the following sample:

http://www.codeproject.com/cs/miscctrl/ImageComboBoxControl.asp





Re: Windows Forms General ComboBox Font

zbo

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();
}
}

}
}






Re: Windows Forms General ComboBox Font

akin_l

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.





Re: Windows Forms General ComboBox Font

Bob zhu-MSFT

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~:)






Re: Windows Forms General ComboBox Font

akin_l

I have tried that. What happens is that the font style of that comboboxItem becomes italic but the selected text that appears in the comboBox still has the default font style. Maybe I am missing something here.



Re: Windows Forms General ComboBox Font

element109

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.