Application/C# (WinForm)
[C# WinForm] ComboBox 텍스트 정렬 하기
devsalix
2023. 10. 16. 14:59
728x90
ComboxBox의 값을 가운데 정렬 하기 위해서는
DrawItem 이벤트를 생성 후 사용 해야 합니다
우선 속성 값 중 DrawMode 값을 OwnerDrawVariable 값으로 변경합니다
이후 DrawItem 이벤트를 생성 후 이벤트에 아래와 같이 코드를 작성합니다
private void comboBox_DrawItem(object sender, DrawItemEventArgs e)
{
ComboBox cbx = sender as ComboBox;
if (cbx != null)
{
e.DrawBackground();
if (e.Index >= 0)
{
StringFormat sf = new StringFormat();
Brush brush = new SolidBrush(cbx.ForeColor);
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
brush = SystemBrushes.HighlightText;
}
e.Graphics.DrawString(cbx.Items[e.Index].ToString(), cbx.Font, brush, e.Bounds, sf);
}
}
}
제 글이 도움이 되셨다면 댓글 & 공감 부탁드려요 😀
728x90
반응형