C# Combo Box Display Character Location

Asked 2 years ago, Updated 2 years ago, 79 views

Is it possible to display the characters in the right-aligned view of the combo box selection?

TEXTbox has TEXTalign properties, so you can set it up, but combobox doesn't have it, so I'm having trouble setting it up

c#

2022-09-30 21:48

1 Answers

I added the TextAlign property to ComboBox by referring to https://stackoverflow.com/q/11817062/9014308">This answer in Align Text in ComboBox introduced in the comment.

The original article's answer was fixed centering as follows, but it can be changed by adding TextAlign properties.

The post is a bit old but may be stillworth to say:
both requirements are possible for Windows Forms ComboBox:

  • Text align center (text area and the dropdown)
    • For the text area, find the Editcontrol and set the ES_CENTERstyle for the control.
    • For the dropdown items or the selected item in drop-down mode, to align text to center, just make the control owner-draw and draw the text at center.
  • Getrid of focus rectangle
    • Make the control owner-draw and just don't draw focus rectangle.
  • For the text area, find the Editcontrol and set the ES_CENTERstyle for the control.
  • For the dropdown items or the selected item in drop-down mode, to align text to center, just make the control owner-draw and draw the text at center.
  • Make the control owner-draw and just don't draw focus rectangle.

The post is a little old, but it might still be worth mentioning:
Both requirements are possible with the Windows form combo box.

  • Middle text alignment (text area and drop-down)
    • For text areas, locate the Edit control and set the control ES_CENTER style.
    • For drop-down items or items selected in drop-down mode, to center the text, draw the control owner and center the text.
  • Remove focus rectangle
    • Make the control owner drawing and avoid drawing focus rectangles
  • For text areas, locate the Edit control and set the control ES_CENTER style.
  • For drop-down items or items selected in drop-down mode, to center the text, draw the control owner and center the text.
  • Make the control owner drawing and avoid drawing focus rectangles

ComboBoxAlign.Designer.cs

namespace ComboBoxAlign
{
    partial class ComboBoxAlign
    {
        private System.ComponentModel.IContainer components=null;
        protected override void disposition (bool disposition)
        {
            if(disposing&&(components!=null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        private void InitializeComponent()
        {
            components=new System.ComponentModel.Container();
        }
    }
}

ComboBoxAlign.cs

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ComboBoxAlign
{
    public partial class ComboBoxAlign—ComboBox
    {
        private HorizontalAlignment_TextAlign;
        public HorizontalAlignment TextAlign
        {
            get {return_TextAlign;}
            set {_TextAlign=value;SetupEdit();} // Add this.Refresh(); if you want to reflect immediately
        }
        public ComboBoxAlign()
        {
            InitializeComponent();
            this.DrawMode=DrawMode.OwnerDrawFixed;
            _TextAlign=HorizontalAlignment.Left;
        }
        DllImport("user32.dll")]
        private static external int GetWindowLong (IntPtrHWnd, intnIndex);
        DllImport("user32.dll")]
        private static external int SetWindowLong (IntPtrWnd, intnIndex, intdwNewLong);
        private constint GWL_STYLE=-16;
        private constant ES_MASK=-4;
        private constant ES_LEFT=0x0000;
        private constant ES_CENTER = 0x0001;
        private constant ES_RIGHT = 0x0002;
        StructureLayout (LayoutKind.Sequential)
        public structure 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 external bool GetComboBoxInfo (IntPtrWnd, ref COMBOBOXINFO pcbi);
        StructureLayout (LayoutKind.Sequential)
        public structure COMBOBOXINFO
        {
            public int cbSize;
            public RECTrcItem;
            public RECT rcButton;
            public int stateButton;
            publicIntPtrwndCombo;
            publicIntPtrwndEdit;
            publicIntPtr hwndList;
        }
        protected override void OnHandleCreated (EventArgse)
        {
            base.OnHandleCreated(e);
            SetupEdit();
        }
        private int buttonWidth = SystemInformation.HorizontalScrollBarArrowWidth;
        private void SetupEdit()
        {
            var info = new COMBOBOXINFO();
            info.cbSize=Marshal.SizeOf(info);
            GetComboBoxInfo(this.Handle,refinfo);
            var style=GetWindowLong(info.hwndEdit, GWL_STYLE);
            style&=ES_MASK;
            int align=ES_LEFT;
            switch(_TextAlign)
            {
                case HorizontalAlignment.Center: align=ES_CENTER;break;
                case HorizontalAlignment.Right: align=ES_RIGHT;break;
            }
            style|=align;
            SetWindowLong(info.hwndEdit, GWL_STYLE, style);
        }
        protected override void OnDrawItem(DrawItemEventArgse)
        {
            base.OnDrawItem(e);
            e.DrawBackground();
            var text="";
            if(e.Index>=0)
            {
                text=GetItemText(Items[e.Index]);
            }
            TextFormatFlags flags = TextFormatFlags.Left;
            switch(this.TextAlign)
            {
                case HorizontalAlignment.Center:flags=TextFormatFlags.HorizontalCenter;break;
                case HorizontalAlignment.Right:flags=TextFormatFlags.Right;break;
            }
            TextRenderer.DrawText(e.Graphics, text, e.Font, e.Bounds, e.ForeColor, flags);
        }
    }
}


2022-09-30 21:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.