文件名:Usercontrol.cs
在处定义控件时要设置其默认属性.
可以用
[DefaultValue( )]来设置,但是它并不是支持所有的类型如Font类和其它的自定义的类.
这时可以用Reset+属性名做成一个方法来,在设计窗体中用重置来设置默认.下面的
public void ResetControlFont()
{
ControlFont = new Font("宋体", 9);
}
就是一个例子.
[Category("Custom")]表示在设计时的属性分类.
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace CustomControls
{
/// <summary>
/// CustomComboBox class
/// </summary>
[Designer(typeof(CustomComboBox.CustomComboBoxDesigner))]
public class CustomComboBox : System.Windows.Forms.UserControl
{
#region Instance Fields
private System.Windows.Forms.TextBox tbxInput;
private System.Windows.Forms.Button btnDropDownList;
private System.Windows.Forms.ListBox lbxInput;
private SqlConnection cnn;
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataView dv = new DataView();
/// <summary>
/// ListBox data source
/// </summary>
private string ccbdatasource;
/// <summary>
/// ListBox data table
/// </summary>
private string ccbdatatable;
/// <summary>
/// TextBox and ListBox Font
/// </summary>
private Font ccbfont = new Font("Arial", 9);
/// <summary>
/// Width of ListBox based on measured string data
/// </summary>
private int dropdownwidth = 80;
/// <summary>
/// Measured string data
/// </summary>
private SizeF textSize;
/// <summary>
/// Control size
/// </summary>
private Point ccbsize = new Point(80, 100);
/// <summary>
/// Set drop down display direction to up
/// </summary>
private bool ccblistboxup;
/// <summary>
/// TextBox text
/// </summary>
private string ccbtext;
#endregion
#region Constructors
/// <summary>
/// CustomComboBox class constructor
/// </summary>
public CustomComboBox()
{
InitializeComponent();
}
#endregion
#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()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(CustomComboBox));
this.tbxInput = new System.Windows.Forms.TextBox();
this.btnDropDownList = new System.Windows.Forms.Button();
this.lbxInput = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// tbxInput
//
this.tbxInput.CausesValidation = false;
this.tbxInput.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.tbxInput.Name = "tbxInput";
this.tbxInput.Size = new System.Drawing.Size(80, 21);
this.tbxInput.TabIndex = 0;
this.tbxInput.Text = "";
//
// btnDropDownList
//
this.btnDropDownList.BackColor = System.Drawing.Color.Gainsboro;
this.btnDropDownList.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnDropDownList.Image = ((System.Drawing.Bitmap)(resources.GetObject("btnDropDownList.Image")));
this.btnDropDownList.Location = new System.Drawing.Point(80, 2);
this.btnDropDownList.Name = "btnDropDownList";
this.btnDropDownList.Size = new System.Drawing.Size(18, 18);
this.btnDropDownList.TabIndex = 1;
this.btnDropDownList.TabStop = false;
//
// lbxInput
//
this.lbxInput.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lbxInput.ItemHeight = 15;
this.lbxInput.Location = new System.Drawing.Point(0, 22);
this.lbxInput.Name = "lbxInput";
this.lbxInput.Size = new System.Drawing.Size(100, 109);
this.lbxInput.TabIndex = 2;
this.lbxInput.TabStop = false;
//
// CustomComboBox
//
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.lbxInput,
this.tbxInput,
this.btnDropDownList});
this.Name = "CustomComboBox";
this.Size = new System.Drawing.Size(100, 20);
this.Load += new System.EventHandler(this.CustomComboBox_Load);
this.ResumeLayout(false);
}
#endregion
#region Properties
/// <summary>
/// ListBox data source
/// </summary>
[DefaultValue(null)]
[Category("Custom")]
public string ListBoxDataSource
{
get
{
return ccbdatasource;
}
set
{
ccbdatasource = value;
}
}
/// <summary>
/// ListBox data table
/// </summary>
[DefaultValue(null)]
[Category("Custom")]
public string ListBoxTable
{
get
{
return ccbdatatable;
}
set
{
ccbdatatable = value;
}
}
/// <summary>
/// TextBox and ListBox Font
/// </summary>
[Category("Custom")]
public Font ControlFont
{
get
{
return ccbfont;
}
set
{
ccbfont = value;
tbxInput.Font = ccbfont;
lbxInput.Font = ccbfont;
dropdownwidth = tbxInput.Width;
updateListBoxWidth("All");
}
}
/// <summary>
/// Update ListBox width based on measured string data
/// </summary>
private void updateListBoxWidth(string textString)
{
Graphics gr = lbxInput.CreateGraphics();
if (textString == "All")
{
for (int i = 0; i < dv.Count; i++)
{
textSize = gr.MeasureString(dv[i]["ListValue"].ToString(),ccbfont);
if ((int)textSize.Width > dropdownwidth)
{
dropdownwidth = (int)textSize.Width;
}
}
}
else
{
textSize = gr.MeasureString(textString,ccbfont);
if ((int)textSize.Width > dropdownwidth)
{
dropdownwidth = (int)textSize.Width;
}
}
lbxInput.Width = dropdownwidth + 20;
this.Width = dropdownwidth + 20;
gr.Dispose();
}
/// <summary>
/// Reset Font to default
/// </summary>
public void ResetControlFont()
{
ControlFont = new Font("Arial", 9);
}
/// <summary>
/// Check for default Font
/// </summary>
public bool ShouldSerializeControlFont()
{
return ! ControlFont.Equals(new Font("Arial", 9));
}
/// <summary>
/// ListBox and TextBox combined size
/// </summary>
[Category("Custom")]
public Point ControlSize
{
get
{
return ccbsize;
}
set
{
ccbsize = value;
this.Width = ccbsize.X + btnDropDownList.Width;
this.Height = tbxInput.Height;
tbxInput.Width = ccbsize.X;
lbxInput.Width = ccbsize.X;
btnDropDownList.Left = ccbsize.X;
lbxInput.Height = ccbsize.Y - tbxInput.Height;
tbxInput.Top = 0;
lbxInput.Top = tbxInput.Height;
}
}
/// <summary>
/// Reset ControlSize to default
/// </summary>
public void ResetControlSize()
{
ControlSize = new Point(80, 100);
}
/// <summary>
/// Check for ControlSize default
/// </summary>
public bool ShouldSerializeControlSize()
{
return ControlSize != new Point(80,100);
}
/// <summary>
/// Set drop down display direction to up
/// </summary>
[DefaultValue(false)]
[Category("Custom")]
public bool ListBoxUp
{
get
{
return ccblistboxup;
}
set
{
ccblistboxup = value;
}
}
/// <summary>
/// TextBox text
/// </summary>
[DefaultValue(null)]
[Category("Custom")]
public string TextBoxText
{
get
{
ccbtext = tbxInput.Text;
return ccbtext;
}
set
{
ccbtext = value;
tbxInput.Text = ccbtext;
}
}
/// <summary>
/// Close drop down list
/// </summary>
[Category("Custom")]
public bool CloseListBox
{
set
{
tbxInput.SelectionLength = 0;
tbxInput.SelectionStart = 0;
if (lbxInput.Visible == true)
{
ccbtext = tbxInput.Text;
lbxInput.Visible = false;
this.Width = ccbsize.X + btnDropDownList.Width;
this.Height = tbxInput.Height;
if (ccblistboxup == true)
{
this.Top += ccbsize.Y - tbxInput.Height;
tbxInput.Top = 0;
btnDropDownList.Top =2;
lbxInput.Top = tbxInput.Height;
}
}
dv.RowFilter = "ListValue like '" + tbxInput.Text + "'";
if (dv.Count < 1 && tbxInput.Text.Length > 0)
{
insertRow();
}
}
}
#endregion
#region Instance Methods
/// <summary>
/// Load ComboBox with data
/// </summary>
private void CustomComboBox_Load(object sender, System.EventArgs e)
{
if (!this.DesignMode)
{
cnn = new System.Data.SqlClient.SqlConnection();
try
{
cnn.ConnectionString = @"data source=localhost;initial catalog=" + ccbdatasource + @";integrated security=SSPI;persist security info=False;packet size=4096";
cnn.Open();
}
catch
{
try
{
cnn.ConnectionString = @"data source=localhost;initial catalog=" + ccbdatasource + @";persist security info=False;user id=sa;packet size=4096";
cnn.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
ccbdatasource = null;
return;
}
}
SqlCommand cmd = cnn.CreateCommand();
cmd.CommandText = "SELECT ListValue FROM " + ccbdatatable;
da = new SqlDataAdapter(cmd);
da.FillSchema(ds,SchemaType.Source);
da.Fill(ds,ccbdatatable);
cnn.Close();
dv.Table = ds.Tables[ccbdatatable];
dv.Sort = "ListValue";
tbxInput.MaxLength = ds.Tables[0].Columns[0].MaxLength;
lbxInput.DataSource = dv;
lbxInput.DisplayMember = "ListValue";
lbxInput.SelectedIndex = -1;
lbxInput.TabStop = false;
lbxInput.Visible = false;
dropdownwidth = ccbsize.X;
updateListBoxWidth("All");
}
}
/// <summary>
/// Process drop down button click
/// </summary>
private void btnDropDownList_Click(object sender, System.EventArgs e)
{
if (lbxInput.Visible == false)
{
dv.RowFilter = null;
if (ccblistboxup == true)
{
this.Top -= ccbsize.Y - tbxInput.Height;
tbxInput.Top += ccbsize.Y - tbxInput.Height;
btnDropDownList.Top += ccbsize.Y - tbxInput.Height;
lbxInput.Top = 0;
}
this.Height = ccbsize.Y;
this.Width = dropdownwidth + 20;
lbxInput.Visible = true;
}
else
{
lbxInput.Visible = false;
this.Width = ccbsize.X + btnDropDownList.Width;
this.Height = tbxInput.Height;
if (ccblistboxup == true)
{
this.Top += ccbsize.Y - tbxInput.Height;
tbxInput.Top = 0;
btnDropDownList.Top = 2;
lbxInput.Top = tbxInput.Height;
}
}
tbxInput.Focus();
}
/// <summary>
/// Update ListBox based on text changes in TextBox
/// </summary>
private void tbxInput_TextChanged(object sender, System.EventArgs e)
{
dv.RowFilter = "ListValue like '" + tbxInput.Text + "%'";
lbxInput.SelectedIndex = -1;
if (tbxInput.Focused == false || dv.Count < 1 || tbxInput.Text.Length < 1)
{
if (lbxInput.Visible == true)
{
lbxInput.Visible = false;
this.Width = ccbsize.X + btnDropDownList.Width;
this.Height = tbxInput.Height;
if (ccblistboxup == true)
{
this.Top += ccbsize.Y - tbxInput.Height;
tbxInput.Top = 0;
btnDropDownList.Top = 2;
lbxInput.Top = tbxInput.Height;
}
}
}
else
{
if (lbxInput.Visible == false)
{
if (ccblistboxup == true)
{
this.Top -= ccbsize.Y - tbxInput.Height;
tbxInput.Top += ccbsize.Y - tbxInput.Height;
btnDropDownList.Top += ccbsize.Y - tbxInput.Height;
lbxInput.Top = 0;
}
this.Height = ccbsize.Y;
this.Width = dropdownwidth + 20;
lbxInput.Visible = true;
lbxInput.SelectedIndex = -1;
}
}
}
/// <summary>
/// Process TextBox arrow keys and delete key
/// </summary>
private void tbxInput_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
e.Handled = true;
if (lbxInput.Visible == false)
{
dv.RowFilter = "ListValue like '" + tbxInput.Text + "%'";
if (dv.Count > 0)
{
if (ccblistboxup == true)
{
this.Top -= ccbsize.Y - tbxInput.Height;
tbxInput.Top += ccbsize.Y - tbxInput.Height;
btnDropDownList.Top += ccbsize.Y - tbxInput.Height;
lbxInput.Top = 0;
}
this.Height = ccbsize.Y;
this.Width = dropdownwidth + 20;
lbxInput.Visible = true;
}
}
else
{
if (lbxInput.SelectedIndex+1 < lbxInput.Items.Count)
{
lbxInput.SelectedIndex = lbxInput.SelectedIndex + 1;
}
}
}
if (e.KeyCode == Keys.Up)
{
e.Handled = true;
if (lbxInput.Visible == true)
{
if (lbxInput.SelectedIndex > 0)
{
lbxInput.SelectedIndex = lbxInput.SelectedIndex - 1;
}
}
}
if (e.KeyCode == Keys.Delete)
{
if (lbxInput.SelectedIndex == -1)
{
MessageBox.Show("Please use the down arrow to select an item from the list to delete.", "Delete Selection",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
else if (lbxInput.Visible == true)
{
if (MessageBox.Show("Delete " + lbxInput.Text + "?", "Delete Selection",MessageBoxButtons.YesNo,MessageBoxIcon.Warning) == DialogResult.Yes)
{
deleteRow();
this.Height = tbxInput.Height;
lbxInput.Visible = false;
this.Width = ccbsize.X + btnDropDownList.Width;
if (ccblistboxup == true)
{
this.Top += ccbsize.Y - tbxInput.Height;
tbxInput.Top = 0;
btnDropDownList.Top = 2;
lbxInput.Top = tbxInput.Height;
}
}
}
}
}
/// <summary>
/// Delete entry from database
/// </summary>
private void deleteRow()
{
SqlCommand cmd = cnn.CreateCommand();
cmd.CommandText = "DELETE FROM " + ccbdatatable + " WHERE ListValue = @ListValue";
cmd.Parameters.Add(new SqlParameter("@ListValue",SqlDbType.VarChar, tbxInput.MaxLength));
cmd.Parameters["@ListValue"].Value = tbxInput.Text;
cmd.CommandType = System.Data.CommandType.Text;
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
DataRow row;
DataRow [] rows;
rows = ds.Tables[ccbdatatable].Select("ListValue='" + lbxInput.Text + "'");
row = rows[0];
row.Delete();
ds.AcceptChanges();
}
/// <summary>
/// Process mouse clicks in ListBox
/// Left mouse click selects item
/// Right mouse click offers delete option for selected item
/// </summary>
private void lbxInput_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
lbxInput.SelectedIndex = (lbxInput.TopIndex + (e.Y / lbxInput.ItemHeight));
if (MessageBox.Show("Delete " + lbxInput.Text + "?", "Delete Selection",MessageBoxButtons.YesNo,MessageBoxIcon.Warning) == DialogResult.Yes)
{
deleteRow();
this.Height = tbxInput.Height;
lbxInput.Visible = false;
this.Width = ccbsize.X + btnDropDownList.Width;
if (ccblistboxup == true)
{
this.Top += ccbsize.Y - tbxInput.Height;
tbxInput.Top = 0;
btnDropDownList.Top = 2;
lbxInput.Top = tbxInput.Height;
}
}
}
else
{
if (tbxInput.Text == lbxInput.Text)
{
this.Height = tbxInput.Height;
lbxInput.Visible = false;
this.Width = ccbsize.X + btnDropDownList.Width;
if (ccblistboxup == true)
{
this.Top += ccbsize.Y - tbxInput.Height;
tbxInput.Top = 0;
btnDropDownList.Top = 2;
lbxInput.Top = tbxInput.Height;
}
}
tbxInput.Text = lbxInput.Text;
}
}
/// <summary>
/// Insert entry into database
/// </summary>
private void insertRow()
{
SqlCommand cmd = cnn.CreateCommand();
cmd.CommandText = "INSERT INTO " + ccbdatatable + " (ListValue) VALUES (@ListValue)";
cmd.Parameters.Add(new SqlParameter("@ListValue",SqlDbType.VarChar, tbxInput.MaxLength));
cmd.Parameters["@ListValue"].Value = tbxInput.Text;
cmd.CommandType = System.Data.CommandType.Text;
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
DataRow NewDataRow = ds.Tables[ccbdatatable].NewRow();
NewDataRow[0] = tbxInput.Text;
ds.Tables[ccbdatatable].Rows.Add(NewDataRow);
ds.AcceptChanges();
}
#endregion
#region Method Overrides
/// <summary>
/// Process ComboBox tab and enter keys
/// </summary>
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Tab || keyData == Keys.Enter)
{
if (lbxInput.Visible == true)
{
if (lbxInput.SelectedIndex == -1)
{
ccbtext = tbxInput.Text;
}
else
{
tbxInput.Text = lbxInput.Text;
ccbtext = lbxInput.Text;
}
lbxInput.Visible = false;
this.Width = ccbsize.X + btnDropDownList.Width;
this.Height = tbxInput.Height;
if (ccblistboxup == true)
{
this.Top += ccbsize.Y - tbxInput.Height;
tbxInput.Top = 0;
btnDropDownList.Top = 2;
lbxInput.Top = tbxInput.Height;
}
}
dv.RowFilter = "ListValue like '" + tbxInput.Text + "'";
if (dv.Count < 1 && tbxInput.Text.Length > 0)
{
insertRow();
}
base.ProcessDialogKey(keyData);
updateListBoxWidth(tbxInput.Text);
return true;
}
if (keyData != Keys.Delete)
{
lbxInput.SelectedIndex = -1;
}
base.ProcessDialogKey(keyData);
return false;
}
#endregion
#region Internal Classes
/// <summary>
/// CustomComboBoxDesigner class
/// </summary>
internal class CustomComboBoxDesigner : ControlDesigner
{
#region Method Overrides
protected override void PostFilterProperties(System.Collections.IDictionary properties)
{
properties.Remove("DataBindings");
properties.Remove("DynamicProperties");
properties.Remove("AccessibleDescription");
properties.Remove("AccessibleName");
properties.Remove("AccessibleRole");
properties.Remove("AllowDrop");
properties.Remove("AutoScroll");
properties.Remove("AutoScrollMargin");
properties.Remove("AutoScrollMinSize");
properties.Remove("BackColor");
properties.Remove("BackgroundImage");
properties.Remove("Dock");
properties.Remove("DockPadding");
properties.Remove("ForeColor");
properties.Remove("Font");
properties.Remove("ImeMode");
properties.Remove("RightToLeft");
properties.Remove("Size");
properties.Remove("Tag");
properties.Remove("Text");
properties.Remove("TextBoxText");
properties.Remove("ListBoxText");
properties.Remove("CloseListBox");
properties.Remove("ListBoxCount");
}
#endregion
}
#endregion
}
}
在处定义控件时要设置其默认属性.
可以用
[DefaultValue( )]来设置,但是它并不是支持所有的类型如Font类和其它的自定义的类.
这时可以用Reset+属性名做成一个方法来,在设计窗体中用重置来设置默认.下面的
public void ResetControlFont()
{
ControlFont = new Font("宋体", 9);
}
就是一个例子.
[Category("Custom")]表示在设计时的属性分类.
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace CustomControls
{
/// <summary>
/// CustomComboBox class
/// </summary>
[Designer(typeof(CustomComboBox.CustomComboBoxDesigner))]
public class CustomComboBox : System.Windows.Forms.UserControl
{
#region Instance Fields
private System.Windows.Forms.TextBox tbxInput;
private System.Windows.Forms.Button btnDropDownList;
private System.Windows.Forms.ListBox lbxInput;
private SqlConnection cnn;
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataView dv = new DataView();
/// <summary>
/// ListBox data source
/// </summary>
private string ccbdatasource;
/// <summary>
/// ListBox data table
/// </summary>
private string ccbdatatable;
/// <summary>
/// TextBox and ListBox Font
/// </summary>
private Font ccbfont = new Font("Arial", 9);
/// <summary>
/// Width of ListBox based on measured string data
/// </summary>
private int dropdownwidth = 80;
/// <summary>
/// Measured string data
/// </summary>
private SizeF textSize;
/// <summary>
/// Control size
/// </summary>
private Point ccbsize = new Point(80, 100);
/// <summary>
/// Set drop down display direction to up
/// </summary>
private bool ccblistboxup;
/// <summary>
/// TextBox text
/// </summary>
private string ccbtext;
#endregion
#region Constructors
/// <summary>
/// CustomComboBox class constructor
/// </summary>
public CustomComboBox()
{
InitializeComponent();
}
#endregion
#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()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(CustomComboBox));
this.tbxInput = new System.Windows.Forms.TextBox();
this.btnDropDownList = new System.Windows.Forms.Button();
this.lbxInput = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// tbxInput
//
this.tbxInput.CausesValidation = false;
this.tbxInput.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.tbxInput.Name = "tbxInput";
this.tbxInput.Size = new System.Drawing.Size(80, 21);
this.tbxInput.TabIndex = 0;
this.tbxInput.Text = "";
//
// btnDropDownList
//
this.btnDropDownList.BackColor = System.Drawing.Color.Gainsboro;
this.btnDropDownList.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnDropDownList.Image = ((System.Drawing.Bitmap)(resources.GetObject("btnDropDownList.Image")));
this.btnDropDownList.Location = new System.Drawing.Point(80, 2);
this.btnDropDownList.Name = "btnDropDownList";
this.btnDropDownList.Size = new System.Drawing.Size(18, 18);
this.btnDropDownList.TabIndex = 1;
this.btnDropDownList.TabStop = false;
//
// lbxInput
//
this.lbxInput.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lbxInput.ItemHeight = 15;
this.lbxInput.Location = new System.Drawing.Point(0, 22);
this.lbxInput.Name = "lbxInput";
this.lbxInput.Size = new System.Drawing.Size(100, 109);
this.lbxInput.TabIndex = 2;
this.lbxInput.TabStop = false;
//
// CustomComboBox
//
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.lbxInput,
this.tbxInput,
this.btnDropDownList});
this.Name = "CustomComboBox";
this.Size = new System.Drawing.Size(100, 20);
this.Load += new System.EventHandler(this.CustomComboBox_Load);
this.ResumeLayout(false);
}
#endregion
#region Properties
/// <summary>
/// ListBox data source
/// </summary>
[DefaultValue(null)]
[Category("Custom")]
public string ListBoxDataSource
{
get
{
return ccbdatasource;
}
set
{
ccbdatasource = value;
}
}
/// <summary>
/// ListBox data table
/// </summary>
[DefaultValue(null)]
[Category("Custom")]
public string ListBoxTable
{
get
{
return ccbdatatable;
}
set
{
ccbdatatable = value;
}
}
/// <summary>
/// TextBox and ListBox Font
/// </summary>
[Category("Custom")]
public Font ControlFont
{
get
{
return ccbfont;
}
set
{
ccbfont = value;
tbxInput.Font = ccbfont;
lbxInput.Font = ccbfont;
dropdownwidth = tbxInput.Width;
updateListBoxWidth("All");
}
}
/// <summary>
/// Update ListBox width based on measured string data
/// </summary>
private void updateListBoxWidth(string textString)
{
Graphics gr = lbxInput.CreateGraphics();
if (textString == "All")
{
for (int i = 0; i < dv.Count; i++)
{
textSize = gr.MeasureString(dv[i]["ListValue"].ToString(),ccbfont);
if ((int)textSize.Width > dropdownwidth)
{
dropdownwidth = (int)textSize.Width;
}
}
}
else
{
textSize = gr.MeasureString(textString,ccbfont);
if ((int)textSize.Width > dropdownwidth)
{
dropdownwidth = (int)textSize.Width;
}
}
lbxInput.Width = dropdownwidth + 20;
this.Width = dropdownwidth + 20;
gr.Dispose();
}
/// <summary>
/// Reset Font to default
/// </summary>
public void ResetControlFont()
{
ControlFont = new Font("Arial", 9);
}
/// <summary>
/// Check for default Font
/// </summary>
public bool ShouldSerializeControlFont()
{
return ! ControlFont.Equals(new Font("Arial", 9));
}
/// <summary>
/// ListBox and TextBox combined size
/// </summary>
[Category("Custom")]
public Point ControlSize
{
get
{
return ccbsize;
}
set
{
ccbsize = value;
this.Width = ccbsize.X + btnDropDownList.Width;
this.Height = tbxInput.Height;
tbxInput.Width = ccbsize.X;
lbxInput.Width = ccbsize.X;
btnDropDownList.Left = ccbsize.X;
lbxInput.Height = ccbsize.Y - tbxInput.Height;
tbxInput.Top = 0;
lbxInput.Top = tbxInput.Height;
}
}
/// <summary>
/// Reset ControlSize to default
/// </summary>
public void ResetControlSize()
{
ControlSize = new Point(80, 100);
}
/// <summary>
/// Check for ControlSize default
/// </summary>
public bool ShouldSerializeControlSize()
{
return ControlSize != new Point(80,100);
}
/// <summary>
/// Set drop down display direction to up
/// </summary>
[DefaultValue(false)]
[Category("Custom")]
public bool ListBoxUp
{
get
{
return ccblistboxup;
}
set
{
ccblistboxup = value;
}
}
/// <summary>
/// TextBox text
/// </summary>
[DefaultValue(null)]
[Category("Custom")]
public string TextBoxText
{
get
{
ccbtext = tbxInput.Text;
return ccbtext;
}
set
{
ccbtext = value;
tbxInput.Text = ccbtext;
}
}
/// <summary>
/// Close drop down list
/// </summary>
[Category("Custom")]
public bool CloseListBox
{
set
{
tbxInput.SelectionLength = 0;
tbxInput.SelectionStart = 0;
if (lbxInput.Visible == true)
{
ccbtext = tbxInput.Text;
lbxInput.Visible = false;
this.Width = ccbsize.X + btnDropDownList.Width;
this.Height = tbxInput.Height;
if (ccblistboxup == true)
{
this.Top += ccbsize.Y - tbxInput.Height;
tbxInput.Top = 0;
btnDropDownList.Top =2;
lbxInput.Top = tbxInput.Height;
}
}
dv.RowFilter = "ListValue like '" + tbxInput.Text + "'";
if (dv.Count < 1 && tbxInput.Text.Length > 0)
{
insertRow();
}
}
}
#endregion
#region Instance Methods
/// <summary>
/// Load ComboBox with data
/// </summary>
private void CustomComboBox_Load(object sender, System.EventArgs e)
{
if (!this.DesignMode)
{
cnn = new System.Data.SqlClient.SqlConnection();
try
{
cnn.ConnectionString = @"data source=localhost;initial catalog=" + ccbdatasource + @";integrated security=SSPI;persist security info=False;packet size=4096";
cnn.Open();
}
catch
{
try
{
cnn.ConnectionString = @"data source=localhost;initial catalog=" + ccbdatasource + @";persist security info=False;user id=sa;packet size=4096";
cnn.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
ccbdatasource = null;
return;
}
}
SqlCommand cmd = cnn.CreateCommand();
cmd.CommandText = "SELECT ListValue FROM " + ccbdatatable;
da = new SqlDataAdapter(cmd);
da.FillSchema(ds,SchemaType.Source);
da.Fill(ds,ccbdatatable);
cnn.Close();
dv.Table = ds.Tables[ccbdatatable];
dv.Sort = "ListValue";
tbxInput.MaxLength = ds.Tables[0].Columns[0].MaxLength;
lbxInput.DataSource = dv;
lbxInput.DisplayMember = "ListValue";
lbxInput.SelectedIndex = -1;
lbxInput.TabStop = false;
lbxInput.Visible = false;
dropdownwidth = ccbsize.X;
updateListBoxWidth("All");
}
}
/// <summary>
/// Process drop down button click
/// </summary>
private void btnDropDownList_Click(object sender, System.EventArgs e)
{
if (lbxInput.Visible == false)
{
dv.RowFilter = null;
if (ccblistboxup == true)
{
this.Top -= ccbsize.Y - tbxInput.Height;
tbxInput.Top += ccbsize.Y - tbxInput.Height;
btnDropDownList.Top += ccbsize.Y - tbxInput.Height;
lbxInput.Top = 0;
}
this.Height = ccbsize.Y;
this.Width = dropdownwidth + 20;
lbxInput.Visible = true;
}
else
{
lbxInput.Visible = false;
this.Width = ccbsize.X + btnDropDownList.Width;
this.Height = tbxInput.Height;
if (ccblistboxup == true)
{
this.Top += ccbsize.Y - tbxInput.Height;
tbxInput.Top = 0;
btnDropDownList.Top = 2;
lbxInput.Top = tbxInput.Height;
}
}
tbxInput.Focus();
}
/// <summary>
/// Update ListBox based on text changes in TextBox
/// </summary>
private void tbxInput_TextChanged(object sender, System.EventArgs e)
{
dv.RowFilter = "ListValue like '" + tbxInput.Text + "%'";
lbxInput.SelectedIndex = -1;
if (tbxInput.Focused == false || dv.Count < 1 || tbxInput.Text.Length < 1)
{
if (lbxInput.Visible == true)
{
lbxInput.Visible = false;
this.Width = ccbsize.X + btnDropDownList.Width;
this.Height = tbxInput.Height;
if (ccblistboxup == true)
{
this.Top += ccbsize.Y - tbxInput.Height;
tbxInput.Top = 0;
btnDropDownList.Top = 2;
lbxInput.Top = tbxInput.Height;
}
}
}
else
{
if (lbxInput.Visible == false)
{
if (ccblistboxup == true)
{
this.Top -= ccbsize.Y - tbxInput.Height;
tbxInput.Top += ccbsize.Y - tbxInput.Height;
btnDropDownList.Top += ccbsize.Y - tbxInput.Height;
lbxInput.Top = 0;
}
this.Height = ccbsize.Y;
this.Width = dropdownwidth + 20;
lbxInput.Visible = true;
lbxInput.SelectedIndex = -1;
}
}
}
/// <summary>
/// Process TextBox arrow keys and delete key
/// </summary>
private void tbxInput_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
e.Handled = true;
if (lbxInput.Visible == false)
{
dv.RowFilter = "ListValue like '" + tbxInput.Text + "%'";
if (dv.Count > 0)
{
if (ccblistboxup == true)
{
this.Top -= ccbsize.Y - tbxInput.Height;
tbxInput.Top += ccbsize.Y - tbxInput.Height;
btnDropDownList.Top += ccbsize.Y - tbxInput.Height;
lbxInput.Top = 0;
}
this.Height = ccbsize.Y;
this.Width = dropdownwidth + 20;
lbxInput.Visible = true;
}
}
else
{
if (lbxInput.SelectedIndex+1 < lbxInput.Items.Count)
{
lbxInput.SelectedIndex = lbxInput.SelectedIndex + 1;
}
}
}
if (e.KeyCode == Keys.Up)
{
e.Handled = true;
if (lbxInput.Visible == true)
{
if (lbxInput.SelectedIndex > 0)
{
lbxInput.SelectedIndex = lbxInput.SelectedIndex - 1;
}
}
}
if (e.KeyCode == Keys.Delete)
{
if (lbxInput.SelectedIndex == -1)
{
MessageBox.Show("Please use the down arrow to select an item from the list to delete.", "Delete Selection",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
else if (lbxInput.Visible == true)
{
if (MessageBox.Show("Delete " + lbxInput.Text + "?", "Delete Selection",MessageBoxButtons.YesNo,MessageBoxIcon.Warning) == DialogResult.Yes)
{
deleteRow();
this.Height = tbxInput.Height;
lbxInput.Visible = false;
this.Width = ccbsize.X + btnDropDownList.Width;
if (ccblistboxup == true)
{
this.Top += ccbsize.Y - tbxInput.Height;
tbxInput.Top = 0;
btnDropDownList.Top = 2;
lbxInput.Top = tbxInput.Height;
}
}
}
}
}
/// <summary>
/// Delete entry from database
/// </summary>
private void deleteRow()
{
SqlCommand cmd = cnn.CreateCommand();
cmd.CommandText = "DELETE FROM " + ccbdatatable + " WHERE ListValue = @ListValue";
cmd.Parameters.Add(new SqlParameter("@ListValue",SqlDbType.VarChar, tbxInput.MaxLength));
cmd.Parameters["@ListValue"].Value = tbxInput.Text;
cmd.CommandType = System.Data.CommandType.Text;
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
DataRow row;
DataRow [] rows;
rows = ds.Tables[ccbdatatable].Select("ListValue='" + lbxInput.Text + "'");
row = rows[0];
row.Delete();
ds.AcceptChanges();
}
/// <summary>
/// Process mouse clicks in ListBox
/// Left mouse click selects item
/// Right mouse click offers delete option for selected item
/// </summary>
private void lbxInput_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
lbxInput.SelectedIndex = (lbxInput.TopIndex + (e.Y / lbxInput.ItemHeight));
if (MessageBox.Show("Delete " + lbxInput.Text + "?", "Delete Selection",MessageBoxButtons.YesNo,MessageBoxIcon.Warning) == DialogResult.Yes)
{
deleteRow();
this.Height = tbxInput.Height;
lbxInput.Visible = false;
this.Width = ccbsize.X + btnDropDownList.Width;
if (ccblistboxup == true)
{
this.Top += ccbsize.Y - tbxInput.Height;
tbxInput.Top = 0;
btnDropDownList.Top = 2;
lbxInput.Top = tbxInput.Height;
}
}
}
else
{
if (tbxInput.Text == lbxInput.Text)
{
this.Height = tbxInput.Height;
lbxInput.Visible = false;
this.Width = ccbsize.X + btnDropDownList.Width;
if (ccblistboxup == true)
{
this.Top += ccbsize.Y - tbxInput.Height;
tbxInput.Top = 0;
btnDropDownList.Top = 2;
lbxInput.Top = tbxInput.Height;
}
}
tbxInput.Text = lbxInput.Text;
}
}
/// <summary>
/// Insert entry into database
/// </summary>
private void insertRow()
{
SqlCommand cmd = cnn.CreateCommand();
cmd.CommandText = "INSERT INTO " + ccbdatatable + " (ListValue) VALUES (@ListValue)";
cmd.Parameters.Add(new SqlParameter("@ListValue",SqlDbType.VarChar, tbxInput.MaxLength));
cmd.Parameters["@ListValue"].Value = tbxInput.Text;
cmd.CommandType = System.Data.CommandType.Text;
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
DataRow NewDataRow = ds.Tables[ccbdatatable].NewRow();
NewDataRow[0] = tbxInput.Text;
ds.Tables[ccbdatatable].Rows.Add(NewDataRow);
ds.AcceptChanges();
}
#endregion
#region Method Overrides
/// <summary>
/// Process ComboBox tab and enter keys
/// </summary>
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Tab || keyData == Keys.Enter)
{
if (lbxInput.Visible == true)
{
if (lbxInput.SelectedIndex == -1)
{
ccbtext = tbxInput.Text;
}
else
{
tbxInput.Text = lbxInput.Text;
ccbtext = lbxInput.Text;
}
lbxInput.Visible = false;
this.Width = ccbsize.X + btnDropDownList.Width;
this.Height = tbxInput.Height;
if (ccblistboxup == true)
{
this.Top += ccbsize.Y - tbxInput.Height;
tbxInput.Top = 0;
btnDropDownList.Top = 2;
lbxInput.Top = tbxInput.Height;
}
}
dv.RowFilter = "ListValue like '" + tbxInput.Text + "'";
if (dv.Count < 1 && tbxInput.Text.Length > 0)
{
insertRow();
}
base.ProcessDialogKey(keyData);
updateListBoxWidth(tbxInput.Text);
return true;
}
if (keyData != Keys.Delete)
{
lbxInput.SelectedIndex = -1;
}
base.ProcessDialogKey(keyData);
return false;
}
#endregion
#region Internal Classes
/// <summary>
/// CustomComboBoxDesigner class
/// </summary>
internal class CustomComboBoxDesigner : ControlDesigner
{
#region Method Overrides
protected override void PostFilterProperties(System.Collections.IDictionary properties)
{
properties.Remove("DataBindings");
properties.Remove("DynamicProperties");
properties.Remove("AccessibleDescription");
properties.Remove("AccessibleName");
properties.Remove("AccessibleRole");
properties.Remove("AllowDrop");
properties.Remove("AutoScroll");
properties.Remove("AutoScrollMargin");
properties.Remove("AutoScrollMinSize");
properties.Remove("BackColor");
properties.Remove("BackgroundImage");
properties.Remove("Dock");
properties.Remove("DockPadding");
properties.Remove("ForeColor");
properties.Remove("Font");
properties.Remove("ImeMode");
properties.Remove("RightToLeft");
properties.Remove("Size");
properties.Remove("Tag");
properties.Remove("Text");
properties.Remove("TextBoxText");
properties.Remove("ListBoxText");
properties.Remove("CloseListBox");
properties.Remove("ListBoxCount");
}
#endregion
}
#endregion
}
}