:: Home

  login:         
  passwords:  

Winforms Interview Questions

Windows Forms Deployment
Windows Forms Controls
Windows Forms Data Binding
Windows Forms Datagrid
Windows Forms Docking
Windows Forms Keyboard Handling
Windows Forms Layout
Windows Forms Licensing
Windows Forms Menus
Windows Forms Mouse Handling
Windows Forms from MFC
Windows Forms from VB6
Windows Forms Patterns
Windows Forms Printing
Windows Forms Resources
Windows Form Scrolling
Windows Forms Tips
Windows Forms Common Dialogs
Windows Forms Listbox
Windows Forms ComboBox
Windows Forms Rich TextBox
Windows Forms ListView
Windows Forms TreeView
Windows Forms Button
Windows Forms TabControl
Windows Forms TextBox
Windows Forms MDI
Windows Forms Cursors
Windows Forms WebBrowser
Windows Forms PictureBox
Windows Forms Form
Windows Forms MDI
Windows Forms In IE
GDI+Bitmaps&Images
GDI+Font
GDI+Color
GDI+Brushes
GDI Drawing Tips
GDI+ from GDI
GDI Paths Regions
GDI+Pens
Interioerability WIn32
Tools Metadata Viewers
Design Time Serialization
Design Time Custom Designers
Design Time Tips
Design Time Type Editors
Vs.Net Tips
Vs.Net Debugging
Vs.Net Macros
Framework Tips General
Framework Tips Events
Framework Tips General IO
Framework Tips Strings
Framework Tips Threading
Tool Resource Editor
Design Time UI
Framework Tips CGI
Framework Tips XML

WPF Interview Qs

SilverLight Interview Qs

SAP Interview Questions

Oracle Interview Questions

PHP Interview Questions

Ajax Interview Questions

IIS 7.0

OOP Interview Questions

Ruby Interview Questions

Sql Server Interview Questions

SharePoint 2007 Questions

Microsoft Crm Questions

Windows Forms ComboBOx

The General Winforms Interview Questions consists the most frequently asked questions in Winforms. This list of 100+ questions guage your familiarity with the Winforms platform. The q&a have been collected over a period of time from various blogs, forums and other similar Winforms sites

20.Windows Forms ComboBox

    20.1 How do I bind the values of an enum to a ComboBox?
    20.2 How can I programmatically prevent the combobox from dropping?
    20.3 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?
    20.4 How can I adjust the height of the of my ComboBox dropdown?
    20.5 How do I implement an owner drawn combobox?
    20.6 How do I set the width of a combobox to fit the entries in its list?
    20.7 How can I programmatically create a new list for my ComboBox dropdown?

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:


[C#]
// Setup the binding as follows:
// MyValues is the enum type
comboBox1.DataSource = Enum.GetValues(typeof MyValues);
[VB]
comboBox1.DataSource = Enum.GetValues(Type.GetType(MyValues))
Then in the SelectedValueChanged event for the ComboBox.
[C#]
private void ComboBox1ValueChanged(object sender, EventArgs e)
{
MyValues v = (MyValues)this.comboBox1.SelectedValue;
}
[VB]
Private Sub ComboBox1ValueChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim v As MyValues = CType(Me.comboBox1.SelectedValue, MyValues)
End Sub

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.

[C#]
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
return;
base.WndProc(ref m);
}
}
[VB.NET]
Public Class MyComboBox
Inherits ComboBox
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
Return

End If
MyBase.WndProc(m)
End Sub 'WndProc
End Class 'MyComboBox

20.3 How do I implement an ownerdrawn listbox?

Set the combobox's DropDownStyle property to DropDownList .


20.4 How can I adjust the height of the of my ComboBox dropdown?

You can subclass ComboBox. In your derived class, make sure you set

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.5 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;

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();
float maxWidth = 0f;
foreach(object o in comboBox1.Items)
{
float w = g.MeasureString(o.ToString(), comboBox1.Font).Width;
if(w > maxWidth)
maxWidth = w;
}
g.Dispose();
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?

You can subclass ComboBox. In your derived class, make sure you set

Here are some snippets. (Courtesy of Michael Lang)

[C#]
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[0][0] = "one";
list.Rows[0][1] = 1;
list.Rows[1][0] = "two";
list.Rows[1][1] = 2;
list.Rows[2][0] = "three";
list.Rows[2][1] = 3;
comboBox1.DataSource = list;
comboBox1.DisplayMember = "Display";
comboBox1.ValueMember = "Id";
[VB.NET]


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(0)(1) = 1 '
list.Rows(1)(0) = "two" '
list.Rows(1)(1) = 2 '
list.Rows(2)(0) = "three" '
list.Rows(2)(1) = 3 '
comboBox1.DataSource = list
comboBox1.DisplayMember = "Display"
comboBox1.ValueMember = "Id"

Copyright 2007, Megasolutions Ltd
     
Copyright 2007, Megasolutions Ltd