2013-03-08 91 views
0

我有這樣的接口:更改列表框項目間距

enter image description here

我想要做的是有能力的ListBox與右邊的格左對齊或空間的名稱,以便每個名稱與每個網格行內聯。

我也試試這個:

lstNames.ItemHeight = 15; 

但這並不影響它。注意:我的listbox是動態創建的,並使用數據庫填充。

任何提示如何實現這一目標?

+1

'Tacit'你必須DrawMode屬性更改爲OwnerDrawFixed自定義使用ItemHeight。 當您使用DrawMode.OwnerDrawFixed時,必須「手動」繪製/繪製項目。 – MethodMan 2013-03-08 18:32:26

+0

@DJ KRAZE擊敗了我。我正在輸入相同的東西。 – TimWagaman 2013-03-08 18:34:47

+2

對所有數據使用DataGridView會更容易。 – HardCode 2013-03-08 18:41:23

回答

1

您必須將DrawMode屬性更改爲OwnerDrawFixed才能使用自定義ItemHeight

當您使用DrawMode.OwnerDrawFixed你必須paint/draw items "manually".

從這個StackoverflowMax引用發佈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); 
     } 
    } 
} 
+0

你能給我一些代碼片段 – Tacit 2013-03-08 18:37:51

+0

我給你的例子不是這是從Max引用我可以添加鏈接,但代碼示例會更容易讓你看到反對導航到衆多的鏈接 – MethodMan 2013-03-08 18:39:03

+0

這個例子應該與一個ListBox一起工作 – MethodMan 2013-03-08 18:40:31