1

我做了一个所有者绘制的组合框。这就是它在表格上的显示方式。在附件上,它是“确定”按钮旁边的组合框。请参阅附件。我需要将实线图像显示为第一个选择,而不是“实线文本”作为第一个选择。

图片

这是我的代码:

 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.”设置为选定值。

但相反,它显示“实线文本”

请建议 谢谢。

4

1 回答 1

1

设置ComboBoxStyleDropDownList。这将禁用在ComboBox. 我想那时将显示图片而不是文本。

于 2012-06-03T22:48:16.407 回答