13

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

4

6 回答 6

32

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

诀窍是将DrawModeComboBox 的 -Property设置OwnerDrawFixed为并订阅它的 event 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);
    }
  }
}

组合框预览

要右对齐项目,您可以简单地替换StringAlignment.CenterStringAlignment.Far.

于 2014-02-06T14:29:55.300 回答
25

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

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

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

于 2012-08-05T14:50:08.407 回答
9

RightToLeft属性设置为true
它不会颠倒字符序列。它只会正确证明。

于 2013-01-09T12:46:34.140 回答
2

Winforms 在自定义控件方面相当不灵活。如果您正在寻找更自定义的用户体验,那么我建议您考虑创建一个 WPF 应用程序,该应用程序允许您定义自定义控件。不过,这需要一些工作,所以只有当你真的认为有必要时,你才会想要去做。这是一个不错的网站,可以帮助您入门http://www.wpftutorial.net/

于 2012-08-05T14:13:51.493 回答
2

这篇文章有点旧,但可能仍然值得说:

对于 Windows 窗体组合框,这两个要求都是可能的:

  • 文本居中对齐(文本区域和下拉菜单)
    • 对于文本区域,找到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);
    }
}
于 2019-11-20T13:18:15.477 回答
0

您可以通过在查询中的显示成员之前添加空格来执行此类操作

例如 :

combobox1.DataSource = Query(Select col1 , ('   '+col2) as Col2 from tableName) 
combobox1.DisplayMember = "Col2";
combobox1.ValueMember = "col1";
于 2017-06-01T09:18:37.103 回答