如何在WinForms中对齐WinForms中组合框的文本

如何在WinForms中对齐WinForms中组合框的文本,winforms,combobox,Winforms,Combobox,我需要将组合框的文本左对齐、右对齐或居中对齐。我在WinForms中找不到ComboBox的TextAlignment或HorizontalAlignment属性 如何设置ComboBox的文本对齐方式 public partial class Form1 : Form { ComboBox comboBox; public Form1() { InitializeComponent(); var button = new Button()

我需要将组合框的文本左对齐、右对齐或居中对齐。我在WinForms中找不到ComboBox的TextAlignment或HorizontalAlignment属性

如何设置ComboBox的文本对齐方式

public partial class Form1 : Form
{
    ComboBox comboBox;
    public Form1()
    {
        InitializeComponent();
        var button = new Button() { Text = "Increase", Size = new Size(100, 20), Location = new Point(10, 10) };
        button.Click += button_Click;
        this.Controls.Add(button);
        comboBox = new ComboBox() { Location = new Point(100, 100), Size = new Size(200, 20), MinimumSize = new Size(0, 0), Font = new Font("Calibri", 11), Text = "Stack Overflow" };
        comboBox.Items.Add("One");
        comboBox.Items.Add("Two");
        comboBox.Items.Add("Three");
        comboBox.DrawMode = DrawMode.OwnerDrawVariable;
        comboBox.DrawItem += comboBox_DrawItem;
        this.Controls.Add(comboBox);
    }

    void comboBox_DrawItem(object sender, DrawItemEventArgs e)
    {
        // By using Sender, one method could handle multiple ComboBoxes
        ComboBox cbx = sender as ComboBox;
        if (cbx != null)
        {
            // Always draw the background
            e.DrawBackground();

            // Drawing one of the items?
            if (e.Index >= 0)
            {
                // Set the string alignment.  Choices are Center, Near and Far
                StringFormat sf = new StringFormat();
                sf.LineAlignment = StringAlignment.Center;
                sf.Alignment = StringAlignment.Center;

                // Set the Brush to ComboBox ForeColor to maintain any ComboBox color settings
                // Assumes Brush is solid
                Brush brush = new SolidBrush(cbx.ForeColor);

                // If drawing highlighted selection, change brush
                if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                    brush = SystemBrushes.HighlightText;

                // Draw the string
                e.Graphics.DrawString(cbx.Items[e.Index].ToString(), cbx.Font, brush, e.Bounds, sf);
            }
        }
    }
}
上面的代码仅将“我的组合框”项设置为显示在中间。如何将组合框中显示的文本设置为居中对齐?我的目标是只对齐该文本框,而不是对齐下拉项

提前谢谢

问候,


Venkatesan R

完整性:正如我们在评论中所讨论的,您已成功对齐组合框项目的代码,但要使编辑文本框对齐,您必须添加以下行,根据:


对于当前问题:

[M] y的目标是仅对齐该文本框,而不是对齐下拉项

只需在以下if语句中的
DrawItem
方法中包装代码对齐行:

if ((e.State & DrawItemState.ComboBoxEdit) == DrawItemState.ComboBoxEdit)
{
    sf.LineAlignment = StringAlignment.Center;
    sf.Alignment = StringAlignment.Center;
}

Hi@M.Schena的可能重复项,由此可知,他们只回答了combobox项,但我需要对齐其中加载的文本框。可以这样做吗?我还不知道,但是你能不能用文本框显示组合框的代码?嗨@M.Schena,我已经分享了我的代码。让我知道如何为最初显示在combobox中的文本设置文本对齐方式。Hi@Ohbouse我的目标是仅对齐该文本框,而不是对齐下拉项。
if ((e.State & DrawItemState.ComboBoxEdit) == DrawItemState.ComboBoxEdit)
{
    sf.LineAlignment = StringAlignment.Center;
    sf.Alignment = StringAlignment.Center;
}