user575219
user575219

Reputation: 2442

owner drawn combo

I made a owner drawn combo box. This is how it is shown on the form. On the attachment, it is the combo box next to the OK button. please see the attachment. Instead of "Solid line- text" as the first selected, I need the solid line image to be displayed as first selection.

Image

Here's my code:

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


}

On the windows form:

        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;

    }

I have comboBoxLineStyle1.SelectedIndex = 0, meaning it should set "itemsolid1-solidline- image. " as the selected value.

But instead it shows "solid line-text"

Please suggest Thank you.

Upvotes: 1

Views: 1072

Answers (1)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112772

Set the ComboBoxStyle to DropDownList. This disables the ability to enter text manually in the ComboBox. I guess that the picture will be displayed instead of the text then.

Upvotes: 1

Related Questions