[英]ComboBox Custom Display
I have created a Custom Combo Box where-in I have been able to Display Multi-Column items along with image when ComboBox is dropped down. 我创建了一个自定义组合框,在其中下拉组合框时,我可以在其中显示多列项目以及图像。 Now the problem I am facing is when an item is selected, i need to display the item exactly as it was displayed in dropdown list ie .
现在,我面临的问题是当选择一个项目时,我需要完全按照下拉列表中显示的内容显示该项目,即。 So which event I should do it in?
那么我应该参加哪个活动? Or How can I accomplish this?
或如何才能做到这一点?
So far I have this 到目前为止,我有这个
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;
}
}
I assume you don't expect the user to type - given the format. 我假设您不希望用户键入-给定格式。 For this, you need to set
DropDownStyle == DropDownList
. 为此,您需要设置
DropDownStyle == DropDownList
。 .. and your current code should work just fine. ..并且您当前的代码应该可以正常工作。
OnDrawItem is called for both the drop down and the top edit/text box portion. 下拉菜单和顶部编辑/文本框部分均调用OnDrawItem。
As explained in: https://stackoverflow.com/a/5111692/631687 , you can differentiate which is being rendered. 如https://stackoverflow.com/a/5111692/631687中所述 ,您可以区分正在呈现的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.