以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  listBox的Item如何居中显示  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=153487)

--  作者:nxhylczh
--  发布时间:2020/8/12 23:30:00
--  listBox的Item如何居中显示
如题所示!
--  作者:有点蓝
--  发布时间:2020/8/13 9:35:00
--  
没有办法设置
--  作者:nxhylczh
--  发布时间:2020/8/13 10:46:00
--  

1、首先设置DrawMode属性为OwnerDrawVariable

2、 增加事件重画ListBox

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
StringFormat strFmt = new System.Drawing.StringFormat();
strFmt.Alignment = StringAlignment.Center; //文本垂直居中
strFmt.LineAlignment = StringAlignment.Center; //文本水平居中
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds, strFmt);
}


我搜索了百度  说C#能居中,老师可否参考一下,让居中吧!


--  作者:有点蓝
--  发布时间:2020/8/13 11:47:00
--  
全局代码:

Public Sub listBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs)
    e.DrawBackground()
    e.DrawFocusRectangle()
    Dim strFmt As StringFormat = New System.Drawing.StringFormat()
    strFmt.Alignment = StringAlignment.Center
    strFmt.LineAlignment = StringAlignment.Center
    e.Graphics.DrawString(sender.Items(e.Index).ToString(), e.Font, New SolidBrush(e.ForeColor), e.Bounds, strFmt)
End Sub

窗口afterload
Dim lb As WinForm.ListBox = e.Form.Controls("ListBox1")
Dim a As System.Windows.Forms.listBox = lb.basecontrol
AddHandler a.DrawItem, AddressOf listBox1_DrawItem

--  作者:nxhylczh
--  发布时间:2020/8/13 11:59:00
--  
实现了,谢谢老师