【问题标题】:ComboBox Custom Display组合框自定义显示
【发布时间】:2011-12-13 05:38:29
【问题描述】:

我创建了一个自定义组合框,当下拉组合框时,我可以在其中显示多列项目以及图像。现在我面临的问题是当一个项目被选中时,我需要完全按照它在下拉列表中的显示方式显示该项目,即。那么我应该在哪个活动中做呢?或者我怎样才能做到这一点?

到目前为止我有这个

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;            
    }
}

【问题讨论】:

  • 有什么代码可以分享给我们吗?您已经在使用哪些事件?

标签: c# winforms


【解决方案1】:

我假设您不希望用户输入 - 给定格式。为此,您需要设置DropDownStyle == DropDownList。 ..您当前的代码应该可以正常工作。

OnDrawItem 为下拉顶部编辑/文本框部分调用。

https://stackoverflow.com/a/5111692/631687 中所述,您可以区分正在渲染的内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 2018-05-17
    • 2020-07-10
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多