我有這樣的接口:更改列表框項目間距
我想要做的是有能力的ListBox
與右邊的格左對齊或空間的名稱,以便每個名稱與每個網格行內聯。
我也試試這個:
lstNames.ItemHeight = 15;
但這並不影響它。注意:我的listbox
是動態創建的,並使用數據庫填充。
任何提示如何實現這一目標?
我有這樣的接口:更改列表框項目間距
我想要做的是有能力的ListBox
與右邊的格左對齊或空間的名稱,以便每個名稱與每個網格行內聯。
我也試試這個:
lstNames.ItemHeight = 15;
但這並不影響它。注意:我的listbox
是動態創建的,並使用數據庫填充。
任何提示如何實現這一目標?
您必須將DrawMode
屬性更改爲OwnerDrawFixed
才能使用自定義ItemHeight
。
當您使用DrawMode.OwnerDrawFixed
你必須paint/draw items "manually".
從這個Stackoverflow
的Max
引用發佈Combobox appearance
public class ComboBoxEx : ComboBox
{
public ComboBoxEx()
{
base.DropDownStyle = ComboBoxStyle.DropDownList;
base.DrawMode = DrawMode.OwnerDrawFixed;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
if(e.State == DrawItemState.Focus)
e.DrawFocusRectangle();
var index = e.Index;
if(index < 0 || index >= Items.Count) return;
var item = Items[index];
string text = (item == null)?"(null)":item.ToString();
using(var brush = new SolidBrush(e.ForeColor))
{
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
e.Graphics.DrawString(text, e.Font, brush, e.Bounds);
}
}
}
'Tacit'你必須DrawMode屬性更改爲OwnerDrawFixed自定義使用ItemHeight。 當您使用DrawMode.OwnerDrawFixed時,必須「手動」繪製/繪製項目。 – MethodMan 2013-03-08 18:32:26
@DJ KRAZE擊敗了我。我正在輸入相同的東西。 – TimWagaman 2013-03-08 18:34:47
對所有數據使用DataGridView會更容易。 – HardCode 2013-03-08 18:41:23