溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ListBox控件的項如何設(shè)置不同的對齊方式

發(fā)布時間:2024-08-08 09:18:03 來源:億速云 閱讀:87 作者:小樊 欄目:編程語言

可以通過設(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)左對齊、居中和右對齊。

向AI問一下細(xì)節(jié)

免責(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)容。

AI