コンボボックスの中央にテキストが表示されるようにテキストを揃えたいのですが、これを行う方法を教えてください。また、フォーカスが合っているときにコンボボックスの周囲にデフォルトの境界線があることがわかります。その境界線を削除するにはどうすればよいですか。親切に私の2つの問題を解決してくださいありがとう
6 に答える
この記事はあなたを助けます: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);
    }
  }
}

アイテムを右揃えにするには、単に。に置き換えることができStringAlignment.CenterますStringAlignment.Far。
これはComboBoxではサポートされていません。正確な理由は時間の霧の中で失われています。ComboBoxは90年代初頭から存在していましたが、テキストボックス部分のテキストをドロップダウンのテキストと揃えるのが面倒なことと関係があります。DrawItemを使用したカスタム描画でも解決できず、ドロップダウンアイテムの外観にのみ影響します。
考えられる回避策として、アイテムの文字列をスペースで埋めて中央に表示するなど、風変わりなことを行うことができます。各アイテムに追加するスペースの数を計算するには、TextRenderer.MeasureText()が必要です。
あなたが話している「境界線」は境界線ではなく、フォーカス長方形です。それを取り除くこともできません。Windowsは、フォーカスのあるコントロールを表示しないUIを作成することを拒否します。マウスよりもキーボードを好むユーザーはそれを気にします。そのための回避策はありません。
RightToLeftプロパティをに設定しますtrue。
文字の順序を逆にすることはありません。それは正しく正当化するだけです。
コントロールのカスタマイズに関しては、Winformsはかなり柔軟性がありません。よりカスタマイズされたユーザーエクスペリエンスを探している場合は、カスタムコントロールを定義できるWPFアプリケーションの作成を検討することをお勧めします。ただし、多少の手間がかかるので、本当に必要だと思ったらやりたいと思うことだけです。ここにあなたが始めるためのまともなサイトがありますhttp://www.wpftutorial.net/
投稿は少し古いですが、それでも言う価値があるかもしれません:
Windowsフォーム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);
    }
}
クエリの表示メンバーの前にスペースを追加することで、このようなことを行うことができます
例えば :
combobox1.DataSource = Query(Select col1 , ('   '+col2) as Col2 from tableName) 
combobox1.DisplayMember = "Col2";
combobox1.ValueMember = "col1";