Hi Linda,
This solution is a poor one because the form would take the focus and
inactivate the main window form. The functionality of the dropdown would be
difficult to manage too.
The correct way to do this is to derive a class from NativeWindow and create
a popup window, similar to a tooltip window, that can be shown using the
SW_SHOWNOACTIVATE setting and the ShowWindow function via interop. A class
created in this way would mimic the behaviour of the base Win32 controls and
it would be easier to create the desired internal functionality such as item
selection and so-on.
As proof of it's nastiness I created a quick and dirty combo-box in the code
below my signature. It's horrible in every way.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
--------------------------------------------------------------------------
public class FauxCombo : System.Windows.Forms.UserControl
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public FauxCombo()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitializeComponent call
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(23, 23);
this.button1.TabIndex = 1;
this.button1.Text = "V";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// FauxCombo
//
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "FauxCombo";
this.Size = new System.Drawing.Size(128, 24);
this.ResumeLayout(false);
}
#endregion
Form dropdown;
private void button1_Click(object sender, System.EventArgs e)
{
if(dropdown==null)
{
dropdown=new Form();
dropdown.FormBorderStyle=FormBorderStyle.None;
dropdown.BackColor=Color.White;
dropdown.ShowInTaskbar=false;
dropdown.StartPosition=FormStartPosition.Manual;
dropdown.Location=this.PointToScreen(new Point(0,23));
dropdown.Size=new Size(80,200);
dropdown.MouseDown+=new MouseEventHandler(dropdown_MouseDown);
dropdown.Closed+=new EventHandler(dropdown_Closed);
dropdown.Show();
dropdown.Capture=true;
}
else
{
dropdown.Hide();
dropdown.MouseDown-=new MouseEventHandler(dropdown_MouseDown);
dropdown.Closed-=new EventHandler(dropdown_Closed);
dropdown.Dispose();
dropdown=null;
}
}
private void dropdown_MouseDown(object sender, MouseEventArgs e)
{
Form f=(Form)sender;
if(!f.ClientRectangle.Contains(new Point(e.X,e.Y)))
f.Close();
}
private void dropdown_Closed(object sender, EventArgs e)
{
dropdown.MouseDown-=new MouseEventHandler(dropdown_MouseDown);
dropdown.Closed-=new EventHandler(dropdown_Closed);
dropdown.Dispose();
dropdown=null;
}
}