在Combobox中对齐文本

时间:2012-08-05 14:04:42

标签: c# winforms combobox visual-c#-express-2010

我想在组合框中对齐我的文本,以便它将显示在组合框的中心告诉我如何执行此操作也可以看到组合框周围有一个默认边框,当它处于焦点时我怎么能删除边界也 请解决我的两个问题 感谢

8 个答案:

答案 0 :(得分:23)

本文将为您提供帮助:http://blog.michaelgillson.org/2010/05/18/left-right-center-where-do-you-align/

诀窍是将ComboBox的DrawMode - 属性设置为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);
    }
  }
}

ComboBox-Preview

要右对齐项目,您只需将StringAlignment.Center替换为StringAlignment.Far

答案 1 :(得分:19)

ComboBox不支持此功能。确切的原因在时间的迷雾中丢失,ComboBox自九十年代初以来一直存在,但肯定与使文本框部分中的文本与下拉列表中的文本对齐的尴尬有关。使用DrawItem的自定义绘图也无法解决,只会影响下拉项目的外观。

作为一种可能的解决方法,您可以做一些古怪的事情,比如用空格填充项目字符串,以便它们看起来居中。您需要TextRenderer.MeasureText()来确定每个项目要添加的空间数。

你所谈论的“边界”不是边框,而是焦点矩形。你无法摆脱这种情况,Windows拒绝让你创建一个不会显示焦点控件的UI。喜欢键盘而不是鼠标的用户关心这一点。没有解决方法。

答案 2 :(得分:6)

将'RightToLeft'属性设置为true。它不会反转字符序列。这只是正确的理由。

答案 3 :(得分:4)

你无法在Windows窗体控件专用组合框

中执行此操作

答案 4 :(得分:2)

Winforms在控件的自定义方面相当不灵活。如果您正在寻找更加个性化的用户体验,那么我建议您考虑创建一个允许您定义自定义控件的WPF应用程序。这需要一些工作,所以如果你真的发现它是必要的,那么你只需要进行一些工作。这是一个很好的网站,可以帮助您http://www.wpftutorial.net/

答案 5 :(得分:0)

您可以通过在查询

中的显示成员之前添加空格来执行此类操作

例如:

combobox1.DataSource = Query(Select col1 , ('   '+col2) as Col2 from tableName) 
combobox1.DisplayMember = "Col2";
combobox1.ValueMember = "col1";

答案 6 :(得分:0)

帖子有点陈旧,但仍然值得一说:

Windows Forms ComboBox的两个要求都是可能的:

  • 文本居中对齐(文本区域和下拉列表)
    • 对于文本区域,找到Edit控件并为该控件设置ES_CENTER样式。
    • 对于下拉菜单项或处于下拉模式的选定项,要使文本居中对齐,只需使控件所有者绘制并在中心绘制文本即可。
  • 摆脱焦点矩形
    • 使控件所有者绘制,而不绘制焦点矩形。

示例

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class MyComboBox : ComboBox
{
    public MyComboBox()
    {
        DrawMode = DrawMode.OwnerDrawFixed;
    }

    [DllImport("user32.dll")]
    static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    [DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    const int GWL_STYLE = -16;
    const int ES_LEFT = 0x0000;
    const int ES_CENTER = 0x0001;
    const int ES_RIGHT = 0x0002;
    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
        public int Width { get { return Right - Left; } }
        public int Height { get { return Bottom - Top; } }
    }
    [DllImport("user32.dll")]
    public static extern bool GetComboBoxInfo(IntPtr hWnd, ref COMBOBOXINFO pcbi);

    [StructLayout(LayoutKind.Sequential)]
    public struct COMBOBOXINFO
    {
        public int cbSize;
        public RECT rcItem;
        public RECT rcButton;
        public int stateButton;
        public IntPtr hwndCombo;
        public IntPtr hwndEdit;
        public IntPtr hwndList;
    }
    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        SetupEdit();
    }
    private int buttonWidth = SystemInformation.HorizontalScrollBarArrowWidth;
    private void SetupEdit()
    {
        var info = new COMBOBOXINFO();
        info.cbSize = Marshal.SizeOf(info);
        GetComboBoxInfo(this.Handle, ref info);
        var style = GetWindowLong(info.hwndEdit, GWL_STYLE);
        style |= 1;
        SetWindowLong(info.hwndEdit, GWL_STYLE, style);
    }
    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        base.OnDrawItem(e);
        e.DrawBackground();
        var txt = "";
        if (e.Index >= 0)
            txt = GetItemText(Items[e.Index]);
        TextRenderer.DrawText(e.Graphics, txt, Font, e.Bounds,
            ForeColor, TextFormatFlags.Left | TextFormatFlags.HorizontalCenter);
    }
}

答案 7 :(得分:-1)

只想添加@OhBeWise所说的内容,对于Telerik Winforms,它应该看起来像这样。comboBox.DropDownStyle= Telerik.WinControls.RadDropDownStyle.DropDownList;