gpt4 book ai didi

c# - 在组合框中对齐文本

转载 作者:可可西里 更新时间:2023-11-01 08:50:19 25 4
gpt4 key购买 nike

我想在组合框中对齐我的文本,以便它显示在组合框的中心告诉我如何做到这一点你也可以看到当组合框处于焦点时它周围有一个默认边框我如何删除那边界也请解决我的两个问题谢谢

最佳答案

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

诀窍是将 ComboBox 的 DrawMode 属性设置为 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);
}
}
}

ComboBox-Preview

要右对齐项目,您只需将 StringAlignment.Center 替换为 StringAlignment.Far

关于c# - 在组合框中对齐文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11817062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com