use*_*440 10 c# combobox winforms visual-c#-express-2010
我想在组合框中对齐我的文本,以便它将显示在组合框的中心告诉我如何做到这一点你也可以看到组合框周围有一个默认边框,当它处于焦点时如何我也删除该边框请解决我的两个问题谢谢
mod*_*diX 25
本文将为您提供帮助:http://blog.michaelgillson.org/2010/05/18/left-right-center-where-do-you-align/
诀窍是DrawMode将ComboBox 的-Property 设置OwnerDrawFixed为以及订阅其事件DrawItem.
您的活动应包含以下代码:
// Allow Combo Box to center aligned
private void cbxDesign_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);
}
}
}
Run Code Online (Sandbox Code Playgroud)

要右对齐,你可以简单地更换项目StringAlignment.Center有StringAlignment.Far.
Han*_*ant 20
ComboBox不支持此功能.确切的原因在时间的迷雾中丢失,ComboBox自九十年代初以来一直存在,但肯定与使文本框部分中的文本与下拉列表中的文本对齐的尴尬有关.使用DrawItem的自定义绘图也无法解决,只会影响下拉项的外观.
作为一种可能的解决方法,您可以做一些古怪的事情,比如用空格填充项目字符串,使它们看起来居中.您需要TextRenderer.MeasureText()来确定要为每个项目添加多少空格.
你所谈论的"边界"不是边框,而是焦点矩形.你无法摆脱这种情况,Windows拒绝让你创建一个不会显示焦点控件的UI.喜欢键盘而不是鼠标的用户关心这一点.没有解决方法.