2016-02-12 61 views
-3

我製作了自定義組合框,它具有集成按鈕以添加新項目,並且在DropDownStyle = ComboBoxStyle.DropDownList的情況下工作良好,但組合框的文本覆蓋了我製作的按鈕時出現問題。ComboBox中的文本之前的空間

如果將組合框的DropDownStyle設置爲DropDown,那麼在文本之前是否有空格?您可以在圖像中看到問題。

[Image showing the spacing issue on the combobox]1

public class ComboBoxButton3 : ComboBox 
{ 
    public ComboBoxButton3() 
    { 
     myButton = new Button(); 

     this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable; 
     this.DropDownStyle = ComboBoxStyle.DropDownList; 
    } 

    protected override void OnCreateControl() 
    { 
     this.myButton.Size = new Size(23, this.ClientSize.Height); 
     this.myButton.Location = new Point(0, 0); 
     this.myButton.Cursor = Cursors.Default; 
     this.Button.BackgroundImage = global::RibbonMenuControlTest.Properties.Resources.add1; 
     this.Button.BackgroundImageLayout = ImageLayout.Stretch; 
     this.Button.FlatStyle = FlatStyle.Flat; 
     this.Controls.Add(this.myButton); 

     base.OnCreateControl(); 
    } 


    protected override void OnDrawItem(DrawItemEventArgs e) 
    { 
     if (this != null) 
     { 
      e.DrawBackground(); 
      if (e.Index >= 0) 
      { 
       StringFormat sf = new StringFormat(); 
       sf.LineAlignment = StringAlignment.Center; 
       sf.Alignment = StringAlignment.Center; 

       Brush brush = new SolidBrush(this.ForeColor); 

       if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) 
        brush = SystemBrushes.HighlightText; 

       e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font, brush, e.Bounds, sf); 
      } 
     } 

     base.OnDrawItem(e); 
    } 

    public Button myButton; 
    public Button Button 
    { 
     get 
     { 
      return myButton; 
     } 
     set 
     { 
      myButton = value; 
     } 
    } 
} 
+0

這是一個有效的問題,我不明白爲什麼它值得很多downvotes – Ian

回答

0

一般來說,如果一個控件的設計中不容易某些功能,通常有它的一個原因。 我建議在繼續之前重新考慮您的設計。無論如何,如果你堅持並繼續 - 那麼這些線程應該給你想要的結果/指向你在正確的方向(至少移動文本)。

Align Text in Combobox

http://blog.michaelgillson.org/2010/05/18/left-right-center-where-do-you-align/

段:

Snippet from source

+0

你的代碼只工作時, DropDownStyle = ComboBoxStyle.DropDownList – masalahi

相關問題