2011-11-04 80 views
0

我已經創建了一個自定義組合框,其中我已經能夠在組合框下拉時顯示多列項目以及圖像。現在我面臨的問題是選擇某個項目時,我需要顯示與下拉列表中顯示的項目完全相同的項目。那麼我應該參加哪個活動?或者我該如何做到這一點?組合框自定義顯示

到目前爲止,我有這個

public partial class XComboBox : ComboBox 
{ 
    private Int32 ColumnGap = 10; 
    private Int32 firstColumnWidth; 
    private Int32 secondColumnWidth; 

    public XComboBox() 
    { 
     DrawMode = DrawMode.OwnerDrawFixed; 
     firstColumnWidth = DropDownWidth/2; 
     secondColumnWidth = DropDownWidth/2; 
     AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems; 

    } 

    public Boolean MultiColumn 
    { 
     get; 
     set; 
    } 

    public String ColumnWidths 
    { 
     get 
     { 
      return String.Concat(firstColumnWidth.ToString(), ";", secondColumnWidth.ToString()); 
     } 
     set 
     { 
      if (Regex.Match(value, "^[0-9]+;[0-9]+$").Success) 
      { 
       String[] widths = value.Split(';'); 
       firstColumnWidth = Int32.Parse(widths[0]); 
       secondColumnWidth = Int32.Parse(widths[1]); 
       DropDownWidth = (firstColumnWidth + secondColumnWidth + ColumnGap) > Width ? (firstColumnWidth + secondColumnWidth + ColumnGap) : Width; 
      } 
      else 
      { 
       throw new ArgumentException("Invalid argument specified. Value of ColumnWidths property should be in \"[0-9];[0-9]\" format"); 
      } 
     } 
    } 

    protected override void OnDrawItem(DrawItemEventArgs e) 
    { 
     XComboItem item = (XComboItem)Items[e.Index]; 
     ColumnGap = firstColumnWidth == 0 ? 0 : ColumnGap; 

     e.DrawBackground(); 
     e.DrawFocusRectangle(); 

     string first = item.DisplayName; 
     string second = item.Description; 

     if (MultiColumn) 
     { 
      while (TextRenderer.MeasureText(first, e.Font).Width > firstColumnWidth) 
      { 
       first = first.Substring(0, first.Length - 1); 
      } 

      e.Graphics.DrawString(first, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left, e.Bounds.Top); 
      e.Graphics.DrawString(second, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + firstColumnWidth + ColumnGap, e.Bounds.Top); 
     } 
     else 
     { 
      e.Graphics.DrawString(second, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left, e.Bounds.Top); 
     } 
    } 

    protected override void OnMeasureItem(MeasureItemEventArgs e) 
    { 
     base.OnMeasureItem(e); 
    } 

    protected override void OnSelectedValueChanged(EventArgs e) 
    { 
     base.OnSelectedValueChanged(e); 
    } 
} 

public class XComboItem 
{ 
    public Int32 ItemId { get; set; } 
    public String DisplayName { get; set; } 
    public Object Value { get; set; } 
    public String Description { get; set; } 

    public XComboItem() 
    { 
     DisplayName = String.Empty; 
     Description = String.Empty; 
     DisplayText = String.Empty; 
    } 

    internal String DisplayText 
    { 
     get; 
     set; 
    } 

    public override string ToString() 
    { 
     return DisplayName;    
    } 
} 
+0

任何代碼與我們分享?你已經使用了哪些事件? – sq33G

回答

0

我想你不希望用戶鍵入 - 定的格式。爲此,您需要設置DropDownStyle == DropDownList。 ..和你當前的代碼應該工作得很好。

OnDrawItem被調用的下拉列表頂部的編輯/文本框部分。

如在https://stackoverflow.com/a/5111692/631687中所解釋的那樣,您可以區分正在渲染哪個。