2012-06-03 54 views
1

我做了一個所有者繪製組合框。這是它在表單上顯示的方式。在附件上,它是OK按鈕旁邊的組合框。請參閱附件。首先選擇「實線文本」,而不是「實線文本」,我需要將實線圖像顯示爲第一選擇。店主繪製組合

Image

這裏是我的代碼:

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-圖像。「作爲選定的值。

但相反,它表明「實線文本」

請建議 謝謝。

回答

1

ComboBoxStyle設置爲DropDownList。這會禁用在ComboBox中手動輸入文本的功能。我想那個圖片會被顯示,而不是文本。