 |
|
 |
Sample: Dim dtData As New DataTable
dtData.Columns.Add("Roger")
dtData.Columns.Add("Col2")
dtData.Columns.Add("Col3")
dtData.Columns.Add("Rabbit")
Dim MyDataRow As DataRow
MyDataRow = dtData.NewRow
MyDataRow.Item("Roger") = "Happy"
MyDataRow.Item("Rabbit") = "Days"
dtData.Rows.Add(MyDataRow)
MyDataRow = dtData.NewRow
MyDataRow.Item("Roger") = "Three"
MyDataRow.Item("Rabbit") = "Stooges"
dtData.Rows.Add(MyDataRow)
With DualColumnComboBox1
.DataSource = dtData
.DisplayMember = "Roger"
.DisplayMember2 = "Rabbit"
End With
DualColumnComboBox [ToolboxBitmap(typeof(System.Windows.Forms.ComboBox))]
public class DualColumnComboBox : ComboBox
{
private string mstrDisplayMember2 = string.Empty;
public DualColumnComboBox()
{
DrawMode = DrawMode.OwnerDrawVariable;
}
/// <summary>
/// Secondary Data Member.
/// </summary>
public string DisplayMember2
{
get
{
return (string.IsNullOrEmpty(this.mstrDisplayMember2)? "(none)" :this.mstrDisplayMember2);
}
set
{
this.mstrDisplayMember2 = value;
InitializeColumns();
}
}
protected override void OnDisplayMemberChanged(EventArgs e)
{
base.OnDisplayMemberChanged(e);
InitializeColumns();
}
public new DrawMode DrawMode
{
get
{
return base.DrawMode;
}
set
{
if (value != DrawMode.OwnerDrawVariable)
{
throw new NotSupportedException("Needs to be DrawMode.OwnerDrawVariable");
}
base.DrawMode = value;
}
}
public new ComboBoxStyle DropDownStyle
{
get
{
return base.DropDownStyle;
}
set
{
if (value == ComboBoxStyle.Simple)
{
throw new NotSupportedException("ComboBoxStyle.Simple not supported");
}
base.DropDownStyle = value;
}
}
protected override void OnDataSourceChanged(EventArgs e)
{
base.OnDataSourceChanged(e);
InitializeColumns();
}
protected override void OnValueMemberChanged(EventArgs e)
{
base.OnValueMemberChanged(e);
InitializeValueMemberColumn();
}
protected override void OnDropDown(EventArgs e)
{
base.OnDropDown(e);
this.DropDownWidth = (int)CalculateTotalWidth();
}
const int columnPadding = 5;
private float[] columnWidths = new float[2];
private String[] columnNames = new String[2];
private int valueMemberColumnIndex = 0;
private void InitializeColumns()
{
//PropertyDescriptorCollection propertyDescriptorCollection = DataManager.GetItemProperties();
// columnWidths = new float[2]; //propertyDescriptorCollection.Count];
// columnNames = new String[2]; //propertyDescriptorCollection.Count];
columnNames[0] = base.DisplayMember;
columnNames[1] = this.DisplayMember2;
}
private void InitializeValueMemberColumn()
{
int colIndex = 0;
foreach (String columnName in columnNames)
{
if (String.Compare(columnName, ValueMember, true, CultureInfo.CurrentUICulture) == 0)
{
valueMemberColumnIndex = colIndex;
break;
}
colIndex++;
}
}
private float CalculateTotalWidth()
{
float totWidth = 0;
foreach (int width in columnWidths)
{
totWidth += (width + columnPadding);
}
return totWidth + SystemInformation.VerticalScrollBarWidth;
}
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
base.OnMeasureItem(e);
if (DesignMode)
return;
for (int colIndex = 0; colIndex < columnNames.Length; colIndex++)
{
string item = Convert.ToString(FilterItemOnProperty( Items[e.Index], columnNames[colIndex]));
SizeF sizeF = e.Graphics.MeasureString(item, Font);
columnWidths[colIndex] = Math.Max(columnWidths[colIndex], sizeF.Width);
}
float totWidth = CalculateTotalWidth();
e.ItemWidth = (int)totWidth;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
if (DesignMode)
return;
e.DrawBackground();
Rectangle boundsRect = e.Bounds;
int lastRight = 0;
using (Pen linePen = new Pen(SystemColors.GrayText))
{
using (SolidBrush brush = new SolidBrush(ForeColor))
{
if (string.IsNullOrEmpty(this.DisplayMember2))
{
e.Graphics.DrawString(Convert.ToString(Items[e.Index]), Font, brush, boundsRect);
}
else
{
try
{
for (int colIndex = 0;colIndex < columnNames.Length; colIndex++)
{
string item = Convert.ToString(FilterItemOnProperty(Items[ e.Index], columnNames[colIndex]));
boundsRect.X = lastRight;
boundsRect.Width = (int)columnWidths[1 ] + columnPadding;
lastRight = boundsRect.Right;
if (colIndex == valueMemberColumnIndex)
{
using (Font boldFont = new Font(Font, FontStyle.Bold))
{
e.Graphics.DrawString(item, boldFont, brush, boundsRect);
}
}
else
{
e.Graphics.DrawString(item, Font, brush, boundsRect);
}
if (colIndex < columnNames.Length - 1)
{
e.Graphics.DrawLine(linePen, boundsRect.Right, boundsRect.Top, boundsRect.Right, boundsRect.Bottom);
}
}
}catch (Exception) {}
}
}
}
e.DrawFocusRectangle();
}
}
|
|
|
|
 |
|
|
 |
|
 |
hi the following code doesnt worrk its says Invalid object name.
could u plz tell me whats wrong in that.
thnx
Raghbir
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
''''' The Following Coding is to Fetch the Data from the Table and Load's,
''''' it into the combo (Begin)
Dim CnStr As String
' Dim Appcn As New System.Data.OleDb.OleDbConnection()
' Dim Da As New System.Data.OleDb.OleDbDataAdapter()
Dim Appcn As New SqlClient.SqlConnection()
Dim da As New SqlClient.SqlDataAdapter()
Dim Ds As DataSet
CnStr = "SERVER=RAGHBIR-PC\BSS; DATABASE=SCHOOL; USER ID=RAGHBIR; PASSWORD=DHANJAL"
Appcn = New SqlClient.SqlConnection(CnStr)
'Appcn = New System.Data.OleDb.OleDbConnection(CnStr)
Appcn.Open()
da = New SqlClient.SqlDataAdapter("Select * from Authenicate", Appcn)
'da = New System.Data.OleDb.OleDbDataAdapter("Select * From Employee", Appcn)
Ds = New DataSet("Combo")
da.Fill(Ds, "Combo")
MulCol.DataSource = Ds.Tables("Combo")
''''' (End)
MulCol.AddItem("RAC001,RACHID") ' This is to add the add into the combo at run time
MulCol.LoadData() 'Load the combo functions
End Sub
Private Sub CmdShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdShow.Click
''''' If u click this button it will tell the combo value and text
MessageBox.Show(MulCol.Value + " - " + MulCol.Text)
End Sub
|
|
|
|
 |
|
 |
1. Can't resize the controls.
2. I place this control just above group box in the form, when clicked on this control, the group box lost its border, so it doesn't repaint automatically.
3. Finally I have used the multi-column combo box supplied by VB6.
thanks,
Pankaj
pkumar
|
|
|
|
 |
|
 |
Is there a possibility to have the SelectedIndexEvent attached to this control. Because I need to automatticaly make some operations after the index is changed.
Thank you
suditur@yahoo.com
Raluca Iosub
|
|
|
|
 |
|
 |
hey,
You have written a nice article
Code is very precide & efficiently written
Can this be embedded in the datagrid.
thanks in advance
Hema Chaudhry
|
|
|
|
 |
|
 |
I want to show text in Multi-column Combo the frist time
Thank You.
puvadols
|
|
|
|
 |
|
 |
Can I craete a combo box that should dispay itemcode, and item name in a single line. It needs to be separated by a line also. The first column should have a size of the largest values width. Then the item name. Please help me to answer this question..
Bye
Thomas
If any corrections send mail
|
|
|
|
 |
|
 |
Please help to change the backcolor of progress bar filling rectangles . It is always displayed as blue in color in vb.net. Is it have any direct properties. Ur answer will be very much appreciated if ?
Bye
Thomas
If any corrections send mail
|
|
|
|
 |
|
 |
Is there a way to prevent this control from 'chopping off' on any side where it runs against the edge of the form? instead of continuing 'past' to display itself fully like a regular ComboBox would if you set its dropdownwidth so it would go 'off' the edge of the form. Thanks, barring that the control is excellent and perfect for my use, well done.
|
|
|
|
 |
|
 |
Was not able to set SelectedValue at run time. Any hints as to what is wrong would be appreciated. In meantime created a hack: Set ID value into column 3 with 0 width If IDToSelect <> "" Then For i As Integer = 0 To .Items.Count - 1 comboTest.SelectedIndex = i If (comboTest.SelectedItem.col3.ToString = IDToSelect.ToString) Then Exit For End If Next Else comboTest.Text = "" comboTest.SelectedIndex = -1 End If
'Option Explicit did not like 'comboTest.SelectedItem.col3 compare (disallows late binding??). Had to set it off.
|
|
|
|
 |
|
 |
Wonder if i can apply this control to the .NET CF, for example Windows CE platform. Thanks
|
|
|
|
 |
|
 |
Nice work! I wonder how we can modify it so that, if the droplist is showing, clicking anywhere outside the control dismisses it?
|
|
|
|
 |
|
 |
Interesting article, however, have you considered drawing the columns manually on the combobox by setting the OwnerDraw property and overriding the DrawItem method?
Cheers
Tom
|
|
|
|
 |
|
 |
One minor thing about the control.. Once you pull it down, you are committed to making a selection. No way to abort. Like in a standard combo, you can hit escape to close the pulldown.
|
|
|
|
 |