我做了一个所有者绘制的组合框。这是它在表单上的显示方式。在附件上,它是OK按钮旁边的组合框。请参阅附件。我不需要将“实线文本”作为第一个选择,而是需要将实线图像显示为第一个选择。
这是我的代码:
public partial class comboBoxLineStyle : ComboBox
{
public comboBoxLineStyle()
{
InitializeComponent();
this.DrawMode = DrawMode.OwnerDrawFixed;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
if (e.Index < 0) { return; }
e.DrawBackground();
ComboBoxItem item = (ComboBoxItem)this.Items[e.Index];
e.Graphics.DrawImage(item.Picture,new Point(e.Bounds.X, e.Bounds.Y));
}
public new Image SelectedItem
{
get
{
return (Image)base.SelectedItem;
}
set
{
base.SelectedItem = value;
}
}
public new Image SelectedValue
{
get
{
return (Image)base.SelectedValue;
}
set
{
base.SelectedValue = value;
}
}
}
public class ComboBoxItem
{
public string text;
public Image Picture;
public Color foreColor;
public override string ToString()
{
return text;
}
public ComboBoxItem() { }
public ComboBoxItem(string pText, Image pValue)
{
text = pText;
Picture = pValue;
}
public ComboBoxItem(string pText, Image pValue, Color pColor)
{
text = pText; Picture = pValue; foreColor = pColor;
}
}
在Windows窗体上:
private void DlgGraphOptions_Load(object sender, EventArgs e)
{
ComboBoxItem item1Solid = new ComboBoxItem("Solid Line",Properties.Resources.Solidline);
ComboBoxItem item1dash = new ComboBoxItem("Dashed Line", Properties.Resources.dashedline);
ComboBoxItem item1dashed = new ComboBoxItem("Dashed Line", Properties.Resources.dashdash);
comboBoxLineStyle1.Items.Add(item1Solid);
comboBoxLineStyle1.Items.Add(item1dash);
comboBoxLineStyle1.Items.Add(item1dashed);
comboBoxLineStyle1.SelectedIndex = 0;
}
我有comboBoxLineStyle1.SelectedIndex = 0,这意味着它应该将“itemsolid1-solidline- image。”设置为所选值。
但相反它显示“实线文字”
请建议 谢谢。
答案 0 :(得分:1)
将ComboBoxStyle
设置为DropDownList
。这会禁用在ComboBox
中手动输入文本的功能。我想那时会显示图片而不是文字。