2011-11-04 66 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中所解释的那样,您可以区分正在渲染哪个。