C#中ListBox控件设置Item字体并居中显示

ListBox居中显示字体

 

首先将Listbox的DrawMode属性设置为DrawMode.OwnerDrawVariable

加载事件DrawItem和MeasureItem,如不加入MeasureItem事件,则Item会使用默认高度重绘,字体显示不完全,各位可以自己尝试一下

 

ListBox _listBox = new ListBox();
_listBox.DrawMode = DrawMode.OwnerDrawVariable;
_listBox.DrawItem += _listBox_DrawItem;
_listBox.MeasureItem += _listBox_MeasureItem;

 

// set listbox item height
        void _listBox_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            e.ItemHeight = 30;
        }


        // make the item text center aligned 
        void _listBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();


            System.Drawing.StringFormat strFmt = new System.Drawing.StringFormat(System.Drawing.StringFormatFlags.NoClip);
            strFmt.Alignment = System.Drawing.StringAlignment.Center;


            RectangleF rf = new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);


            //You can also use DrawImage to add some customized image before or after text string, of course backgroud image


            e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new System.Drawing.SolidBrush(e.ForeColor), rf, strFmt);
        }

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# ListBox 控件嵌入到 ComboBox 的下拉框,可以通过以下步骤实现覆盖 ComboBox 下拉框: 1. 将 ComboBox 控件的 DrawMode 属性设置为 OwnerDrawFixed。 2. 在 ComboBox 控件的 DropDown 事件添加以下代码: ``` private void comboBox1_DropDown(object sender, EventArgs e) { // 创建 ListBox 控件 ListBox listBox = new ListBox(); // 设置 ListBox 控件的属性 listBox.Items.Add("Item 1"); listBox.Items.Add("Item 2"); listBox.Items.Add("Item 3"); listBox.Dock = DockStyle.Fill; // 将 ListBox 控件添加到 ComboBox 的下拉框 comboBox1.Controls.Add(listBox); } ``` 3. 在 ComboBox 控件的 DrawItem 事件添加以下代码: ``` private void comboBox1_DrawItem(object sender, DrawItemEventArgs e) { // 只处理 ComboBox 的下拉框的项 if (e.Index < 0) { return; } // 获取 ComboBox 的 Graphics 对象 Graphics g = e.Graphics; // 获取 ComboBox 的绘制区域 Rectangle bounds = e.Bounds; // 如果当前项被选,则绘制背景颜色 if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) { g.FillRectangle(Brushes.LightBlue, bounds); } else { g.FillRectangle(Brushes.White, bounds); } // 获取当前项的文本 string text = comboBox1.Items[e.Index].ToString(); // 设置字体和颜色 Font font = new Font(comboBox1.Font, FontStyle.Regular); Color color = Color.Black; // 绘制文本 g.DrawString(text, font, new SolidBrush(color), bounds.X, bounds.Y); // 如果当前项被选,则绘制 ListBox 控件 if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) { // 获取 ListBox 控件 ListBox listBox = (ListBox)comboBox1.Controls[0]; // 绘制 ListBox 控件 listBox.Location = new Point(bounds.X, bounds.Y + bounds.Height); listBox.Visible = true; } else { // 隐藏 ListBox 控件 ListBox listBox = (ListBox)comboBox1.Controls[0]; listBox.Visible = false; } } ``` 这样,当用户点击 ComboBox 的下拉框时,就会显示一个覆盖 ComboBox 下拉框的 ListBox 控件,用户可以通过 ListBox 控件来选择项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值