Recently I was working on an assignment wherein I had to create a Custom ComboBox to display multiple columns when it is dropped down. Something similar to below image.
The actual assignmet was little more complex than the one I have shown in the above image. Anyways, if you look at the image, you will also find the combo box pretty useful as it can display data in column manner like FirstName-LastName, Name-Place etc. So, if you like it, you can design it yourself. All you have to do is, create class that inherits ComboBox, set the DrawMode property to OwnerDrawFixed, override OnDrawItem method to draw the columns manually.
Sounds simple, isn't it? Now, how do we supply items to this ComboBox? If we have to display an item in multicolumn fashion, ideally the item must be composite one i.e. a Dictionary<T, T> or a class with 2 instance members. Since ComboBox's Items property is a collection of objects, we can place anything into it. This has got both advantage as well as disadvantages. Advantage is we can place object of our custom class into ComboBox while the disadvantage is user can enter any kind of item which cannot be represented in the way we need to display it.
Ok, all that said, I will create a class whose instances we are going to place in the combo box. I will call this class as XComboItem (X stands for Extended).
public class XComboItem
{
public String DisplayName { get; set; }
public Object Value { get; set; }
public String Description { get; set; }
}
So, this is the class, whose objects we are going to place in out custom combo Box. I will name the combo as XComboBox.
public partial class XComboBox : ComboBox
{
private Int32 ColumnGap = 20;
private Int32 firstColumnWidth;
private Int32 secondColumnWidth;
public XComboBox(): base()
{
DrawMode = DrawMode.OwnerDrawFixed;
firstColumnWidth = DropDownWidth / 2;
secondColumnWidth = DropDownWidth / 2;
}
}
In this, firstColumnWidth and secondColumnWidth are the widths of both columns that I am going to display in combo dropdown list (in pixels). Column gap is gap between two columns.Initially I will set the column widths to half the width of DropDownWidth. The only major thing is to override OnDrawItem() function. By overriding this function, you can draw the items in DropDownList of the combobox in the way you want. You can display each item with different font, differen style or even add image to each item. In our case, we need to display DisplayItem & Description of XComboItem in two seperate columns. The logic is getting the drawing object of current item, then use DrawString method appropriately. So, here goes our OnDrawItem.
protected override void OnDrawItem(DrawItemEventArgs e)
{
if (e.Index < 0)
{
return;
}
XComboItem item = (XComboItem)Items[e.Index];
ColumnGap = firstColumnWidth == 0 ? 0 : ColumnGap;
e.DrawBackground();
e.DrawFocusRectangle();
String first = item.DisplayName;
String second = item.Description;
while (TextRenderer.MeasureText(first, e.Font).Width > firstColumnWidth)
{
first = first.Substring(0, first.Length - 1);
}
Brush solidBrush = new SolidBrush(e.ForeColor);
e.Graphics.DrawString(first, e.Font, solidBrush, e.Bounds.Left, e.Bounds.Top);
e.Graphics.DrawString(second, e.Font, solidBrush, e.Bounds.Left + firstColumnWidth +
ColumnGap, e.Bounds.Top);
}
You can add any properties or methods to make the control more user friendly. So, That's it, our control is ready. To use, create the object of XComboItem and add the object to Items collection of XComboBox. Then run your application to see the effect.
please post demo
ReplyDeleteMulti Column ComboBox in C#
ReplyDeleteinformative post.. thanks for sharing .
ReplyDelete.Net training in Lucknow
I found so many interesting in your blog especially its discussion.
ReplyDeleteAdvanced .Net training
Dot Net Training in Chennai | Dot Net Training in Chennai
ReplyDelete
ReplyDeleteThanks for sharing this valuable information.
java projects in chennai
dotnet projects in chennai
ns2 projects in chennai
I read your blog. It’s very useful for me. I have some new idea to share with you .
ReplyDeletephp training company in Ahmedabad
php live project training in ahmedabad
live project training in Ahmedabad
php training in noida
ReplyDeletephp training institute in noida
php training cenetr in noida
Nice blog on the multi column combobox. you can give more suggestion on topic .NET Training and ASP.NET training
ReplyDeleteGood Information..thanks for sharing the wonderful post... Best software Training institute in Bangalore
ReplyDeleteThank you for sharing nice blog
ReplyDeleteE accounting Course in Uttam Nagar
thank your valuable content.we are very thankful to you.one of the recommended blog.which is very useful to new learners and professionals.content is very useful for Hadoop learners...
ReplyDeleteSalesforce Training in Chennai
Salesforce Online Training in Chennai
Salesforce Training in Bangalore
Salesforce Training in Hyderabad
Salesforce training in ameerpet
Salesforce Training in Pune
Salesforce Online Training
Salesforce Training
Great post. Thank you for sharing such useful information. Please keep sharing
ReplyDeleteClick Now
Click Now
Click Now
Click Now
Click Now
Click Now
Click Now
Click Now
Click Now
Click Now
Great information...I really enjoyed your blog keep posting..!!!
ReplyDeleteClick Now
Click Now
Click Now
Click Now
Click Now
Click Now
Click Now
Click Now
Wow such an amazing content keep it up. I have bookmarked your page to check out more informative content here.
ReplyDeleteSASVBA is recognized as the best Machine Learning course in Delhi Whether you are a project manager, college student, or IT student, Professionals are the best machine learning institute in Delhi, providing the best learning environment, experienced machine learning instructors, and flexible training programs for the entire module.
FOR MORE INFO:
pendik alarko carrier klima servisi
ReplyDeletetuzla toshiba klima servisi
ataşehir lg klima servisi
beykoz beko klima servisi
üsküdar beko klima servisi
pendik lg klima servisi
çekmeköy bosch klima servisi
ataşehir bosch klima servisi
çekmeköy arçelik klima servisi
Thanks for sharing such captivating information with us. And I hope you will share some more information about. PHP Course in Noida please keep sharing!
ReplyDeletechirurgie nez
ReplyDelete