|
|
20.1 How do I bind the values of an enum to a ComboBox?
|
 |
This entry was created using the feedback provided by Jay Harlow in the newsgroups.
The enum values can be bound to a combobox as follows:
|
// Setup the binding as follows:
|
// MyValues is the enum type
|
comboBox1.DataSource = Enum.GetValues(typeof MyValues);
|
comboBox1.DataSource = Enum.GetValues(Type.GetType(MyValues))
|
Then in the SelectedValueChanged event for the ComboBox.
|
private void ComboBox1ValueChanged(object sender, EventArgs e)
|
MyValues v = (MyValues)this.comboBox1.SelectedValue;
|
Private Sub ComboBox1ValueChanged(ByVal sender As Object, ByVal e As EventArgs)
|
Dim v As MyValues = CType(Me.comboBox1.SelectedValue, MyValues)
|
20.2 How can I programmatically prevent the combobox from dropping?
|
 |
You can avoid the combobox dropping by overriding its WndProc method and ignoring the WM_LBUTTONDOWN and WM_LBUTTONDBLCLK.
|
public class MyComboBox : ComboBox
|
protected override void WndProc(ref System.Windows.Forms.Message m)
|
if(m.Msg == 0x201 //WM_LBUTTONDOWN
|
|| m.Msg == 0x203) //WM_LBUTTONDBLCLK
|
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
|
If m.Msg = &H201 OrElse m.Msg = &H203 Then 'WM_LBUTTONDOWN or WM_LBUTTONDBLCLK
|
20.3 How can I adjust the height of the of my ComboBox dropdown?
|
 |
You can control the height by changing the value of the MaxDropDownItems property of the the Combobox, which is the setting for maximum number of entries that will be displayed by the drop-down list. |
20.4 How do I implement an owner drawn combobox?
|
 |
You can subclass ComboBox. In your derived class, make sure you set
|
this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
|
this.DropDownStyle = ComboBoxStyle.DropDownList;
|
You also need to handle the DrawItem event to actually implement the drawing. Check out the details in the OwnerDrawnComboBox sample.
|
20.5 How do I add a ComboBox button to a toolbar?
|
 |
Michael Gold discusses this problem in How to create a ComboBox button in a toolbar in .NET on C# Corner. |
20.6 How do I set the width of a combobox to fit the entries in its list?
|
 |
You can iterate through the list to find the longest text extent using MeasureString, and then use this as the combobox width adding a fudge factor for the dropdown button.
|
System.Drawing.Graphics g = comboBox1.CreateGraphics();
|
foreach(object o in comboBox1.Items)
|
float w = g.MeasureString(o.ToString(), comboBox1.Font).Width;
|
comboBox1.Width = (int) maxWidth + 20; // 20 is to take care of button width
|
20.7 How can I programmatically create a new list for my ComboBox dropdown?
|
 |
Here are some snippets. (Courtesy of Michael Lang)
|
DataTable list = new DataTable();
|
list.Columns.Add(new DataColumn("Display", typeof(string)));
|
list.Columns.Add(new DataColumn("Id", typeof(int)));
|
list.Rows.Add(list.NewRow());
|
list.Rows.Add(list.NewRow());
|
list.Rows.Add(list.NewRow());
|
list.Rows[2][0] = "three";
|
comboBox1.DataSource = list;
|
comboBox1.DisplayMember = "Display";
|
comboBox1.ValueMember = "Id";
|
Dim list As New DataTable()
|
list.Columns.Add(New DataColumn("Display", GetType(String)))
|
list.Columns.Add(New DataColumn("Id", GetType(Integer)))
|
list.Rows.Add(list.NewRow())
|
list.Rows.Add(list.NewRow())
|
list.Rows.Add(list.NewRow())
|
list.Rows(0)(0) = "one" '
|
list.Rows(1)(0) = "two" '
|
list.Rows(2)(0) = "three" '
|
comboBox1.DataSource = list
|
comboBox1.DisplayMember = "Display"
|
comboBox1.ValueMember = "Id"
|
20.8 How can I turn off editing in the textbox portion of a ComboBox, restricting the user to selecting only those options in the drop list?
|
 |
Set the combobox's DropDownStyle property to DropDownList . |
|
|
|
|