以下是一个控件的源码(参考一下吧):
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace WinFormDemo
{
public class ListBoxTextChangingEventArgs : CancelEventArgs
{
private string newText;
public ListBoxTextChangingEventArgs(string newText)
{
this.newText = newText;
}
public string NewText
{
get{return this.newText;}
}
}
public delegate void ListBoxTextChangingEventHandler(object sender, ListBoxTextChangingEventArgs e);
/// <summary>
/// Summary description for EditableList.
/// </summary>
public class EditableList : System.Windows.Forms.UserControl
{
public event EventHandler ButtonClick;
public event ListBoxTextChangingEventHandler ItemChanging;
protected System.Windows.Forms.ListBox listBox;
protected System.Windows.Forms.TextBox textBox;
protected System.Windows.Forms.Button button;
protected bool ignoreNextMouseUp = true;
protected bool editing = false;
private static int EditorStripHeight = 20;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public EditableList()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitForm call
}
public ListBox ListBox
{
get{return this.listBox;}
}
public TextBox TextBox
{
get{return this.textBox;}
}
public Button Button
{
get{return this.button;}
}
public bool Editing
{
get{return this.editing;}
set
{
if(this.editing != value)
{
if(!value)
this.EndEditing(value);
else
this.StartEditing();
}
}
}
/// <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.listBox = new System.Windows.Forms.ListBox();
this.textBox = new System.Windows.Forms.TextBox();
this.button = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listBox
//
this.listBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.listBox.Name = "listBox";
this.listBox.Size = new System.Drawing.Size(150, 147);
this.listBox.TabIndex = 0;
this.listBox.MouseUp += new System.Windows.Forms.MouseEventHandler(this.ListBox_MouseUp);
this.listBox.SelectedIndexChanged += new System.EventHandler(this.ListBox_SelectedIndexChanged);
//
// textBox
//
this.textBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox.Location = new System.Drawing.Point(8, 120);
this.textBox.Name = "textBox";
this.textBox.TabIndex = 2;
this.textBox.Text = "";
this.textBox.Visible = false;
this.textBox.Size = new Size(100, EditorStripHeight);
this.textBox.LostFocus += new System.EventHandler(this.TextBox_LostFocus);
//
// button
//
this.button.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button.Location = new System.Drawing.Point(112, 120);
this.button.Name = "button";
this.button.Size = new System.Drawing.Size(30, EditorStripHeight);
this.button.TabIndex = 2;
this.button.Text = "...";
this.button.Visible = false;
this.button.Click += new System.EventHandler(this.Button_Click);
this.button.LostFocus += new System.EventHandler(this.Button_LostFocus);
//
// EditableList
//
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button,
this.textBox,
this.listBox});
this.Name = "EditableList";
this.ResumeLayout(false);