ComboBox中的文本前空格

时间:2016-02-12 00:01:16

标签: c# winforms

我制作了自定义组合框,它有一个集成按钮来添加新项目,它在DropDownStyle = ComboBoxStyle.DropDownList时效果很好但是当组合框的文本框DropDownStyle = ComboBoxStyle.DropDown覆盖按钮时出现问题我做的。

如果组合框的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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

通常,如果控件不是为了轻松完成某项功能而设计的,通常就有理由这样做。 我建议你在继续之前重新考虑你的设计。

无论如何,如果你坚持并继续 - 那么这些线程应该给你所需的结果/点你正确的方向(至少移动文本)。

Align Text in Combobox

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

段:

Snippet from source