【问题标题】:owner drawn combo所有者绘制的组合
【发布时间】:2012-06-04 06:48:16
【问题描述】:

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

这是我的代码:

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

但它显示的是“实线文本”

请推荐 谢谢。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    ComboBoxStyle 设置为DropDownList。这将禁用在ComboBox 中手动输入文本的功能。我猜那时会显示图片而不是文字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 2011-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多