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#
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:
Edit
control and set the ES_CENTER
style for the control.Edit
control and set the ES_CENTER
style for the control.The post is a little old, but it might still be worth mentioning:
Both requirements are possible with the Windows form combo box.
Edit
control and set the control ES_CENTER
style.Edit
control and set the control ES_CENTER
style.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);
}
}
}
© 2024 OneMinuteCode. All rights reserved.