问题描述
我需要按左,右或中心对齐ComboBox的文本。我在WinForms中找不到TextAlignment或HorizontalAlignment属性ComboBox。
如何为ComboBox设置TextAlignment?
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)
{
//使用Sender,一个方法可以处理多个ComboBox
ComboBox cbx =发送方为ComboBox;
if(cbx!= null)
{
//总是绘制背景
e.DrawBackground();
//绘制其中一个项目?
if(e.Index> = 0)
{
//设置字符串对齐方式。选择是中心,近和远
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
//将画笔设置为ComboBox ForeColor以维护任何ComboBox颜色设置
//假设画笔是实体
画笔刷= new SolidBrush(cbx.ForeColor);
//如果绘制突出显示的选择,更改画笔
if((e.State& DrawItemState.Selected)== DrawItemState.Selected)
brush = SystemBrushes.HighlightText;
//绘制字符串
e.Graphics.DrawString(cbx.Items [e.Index] .ToString(),cbx.Font,brush,e.Bounds,sf);
}
}
}
}
代码只设置我的组合框项目在中心显示。如何将组合框中显示的文本设置为中心对齐?
感谢您的提前。
Venkatesan R
完整性:正如我们在注释,代码已经成功地对齐ComboBox项目,但为了编辑TextBox对齐你必须添加以下行,根据
I need to Align Text for ComboBox by left, right or center. I couldn't find TextAlignment or HorizontalAlignment property for ComboBox in WinForms.
How can i set TextAlignment for 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);
}
}
}
}
Above code only sets my combobox item to be displayed in center. How can the text displayed in the combobox be set to center alignment? My goal is to align that text box only not the dropdown items.
Thanks in Advance.
Regards,
Venkatesan R
For completeness: Just as we discussed in comments, the code you have successfully aligns the ComboBox items but for the editing TextBox to align you must add the following line, according to the originally linked blog:
comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
For the issue at hand:
Simply wrap your alignment lines of code in the DrawItem
method in the following if-statement:
if ((e.State & DrawItemState.ComboBoxEdit) == DrawItemState.ComboBoxEdit)
{
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
}
这篇关于如何在WinForms中的WinForms中对齐组合框的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!