2016-02-12 48 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

相关问题