您好,登錄后才能下訂單哦!
可以通過設(shè)置ListBox控件的Style屬性來改變項的對齊方式??梢允褂靡韵麓a來設(shè)置ListBox控件的項為左對齊、居中或右對齊:
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
if (e.Index >= 0)
{
StringFormat format = new StringFormat();
format.LineAlignment = StringAlignment.Center;
// 左對齊
format.Alignment = StringAlignment.Near;
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds, format);
// 居中
format.Alignment = StringAlignment.Center;
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds, format);
// 右對齊
format.Alignment = StringAlignment.Far;
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds, format);
}
}
以上代碼將ListBox控件的DrawMode屬性設(shè)置為OwnerDrawFixed,并在ListBox的DrawItem事件處理程序中自定義繪制ListBox的項,通過設(shè)置不同的StringAlignment屬性來實現(xiàn)左對齊、居中和右對齊。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。