Add Items to ComboBox
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class ComboBoxes : Form
{
ComboBox cmb;
Button btnDisplay;
Button btnInsert;
Button btnSelect;
Label lblEdit;
TextBox txtDisplay;
Boolean boolChange = false;
Boolean boolProcessed = false;
public ComboBoxes()
{
Size = new Size(300,400);
this.Load += new EventHandler(this_Load);
cmb = new ComboBox();
cmb.Parent = this;
cmb.Location = new Point(10,10);
cmb.Size = new Size(ClientSize.Width / 2, Height - 200);
cmb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
cmb.DropDownStyle = ComboBoxStyle.DropDown;
cmb.DropDownStyle = ComboBoxStyle.Simple;
cmb.DropDownWidth = (int)(cmb.Width * 1.5);
cmb.MaxDropDownItems = 12;
cmb.MaxLength = 20;
cmb.SelectionChangeCommitted += new EventHandler(cmb_SelectionChangeCommitted);
cmb.Leave += new EventHandler(cmb_Leave);
btnInsert = new Button();
btnInsert.Parent = this;
btnInsert.Text = "&Insert Item";
btnInsert.Size = new Size((int)(Font.Height * .75) * btnInsert.Text.Length, cmb.Height);
btnInsert.Location = new Point(cmb.Right + 10, cmb.Top);
btnInsert.Click += new System.EventHandler(btnInsert_Click);
lblEdit = new Label();
lblEdit.Parent = this;
lblEdit.BorderStyle = BorderStyle.Fixed3D;
lblEdit.Location = new Point(cmb.Left, cmb.Bottom + 10);
lblEdit.BackColor = Color.LightGray;
lblEdit.Text = "";
lblEdit.Size = new Size(cmb.DropDownWidth, Font.Height * 2);
btnDisplay = new Button();
btnDisplay.Parent = this;
btnDisplay.Text = "&Display Items";
btnDisplay.Size = new Size((int)(Font.Height * .75) * btnDisplay.Text.Length, cmb.Height);
btnDisplay.Location = new Point(lblEdit.Left, lblEdit.Bottom + 10);
btnDisplay.Click += new System.EventHandler(btnDisplay_Click);
txtDisplay = new TextBox();
txtDisplay.Parent = this;
txtDisplay.Location = new Point(btnDisplay.Left, btnDisplay.Bottom + 10);
txtDisplay.Multiline = true;
txtDisplay.ReadOnly = true;
txtDisplay.BackColor = Color.LightGray;
txtDisplay.ScrollBars = ScrollBars.Vertical;
txtDisplay.Text = "";
txtDisplay.Size = new Size(cmb.DropDownWidth, 200);
btnSelect = new Button();
btnSelect.Parent = this;
btnSelect.Text = "&Select 4";
btnSelect.Size = new Size((int)(Font.Height * .75) * btnSelect.Text.Length, cmb.Height);
btnSelect.Location = new Point(btnDisplay.Right + 10, btnDisplay.Top);
btnSelect.Click += new System.EventHandler(btnSelect_Click);
cmb.Items.Add("A");
cmb.Items.Add("B");
cmb.Items.Add("C");
cmb.Items.Add("D");
cmb.Items.Add("E");
cmb.SelectedIndex = 0;
}
static void Main()
{
Application.Run(new ComboBoxes());
}
private void this_Load(object sender, EventArgs e)
{
cmb.TextChanged += new EventHandler(cmb_TextChanged);
cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
}
private void cmb_TextChanged(object sender, EventArgs e)
{
if (!boolProcessed)
lblEdit.Text = cmb.Text;
boolChange = true;
}
private void cmb_SelectedIndexChanged(object sender, EventArgs e)
{
if (boolChange)
{
boolChange = false;
boolProcessed = false;
}
}
private void cmb_SelectionChangeCommitted(object sender, EventArgs e)
{
if (boolChange)
ProcessChange();
}
private void cmb_Leave(object sender, EventArgs e)
{
if (boolChange)
{
ProcessChange();
boolChange = false;
}
}
private void ProcessChange()
{
lblEdit.Text = "Edited: " + cmb.Text;
boolProcessed = true;
}
private void btnDisplay_Click(object sender, EventArgs e)
{
string str = DateTime.Now.ToString() + "\r\n";
foreach (object item in cmb.Items)
{
str += item.ToString() + "\r\n";
}
txtDisplay.Text = str;
}
private void btnSelect_Click(object sender, EventArgs e)
{
cmb.Select(1,2);
}
private void btnInsert_Click(object sender, EventArgs e)
{
if (cmb.FindStringExact(cmb.Text) != -1)
{
MessageBox.Show(""" + cmb.Text + "" already exists in the list.\r\n" +
"Will not be added again.",
"Already Exists!");
}
else if (cmb.Text == "")
{
MessageBox.Show("There is nothing to add.","Nothing There");
}
else
{
cmb.Items.Add(cmb.Text);
}
}
}
AutoComplete ComboBox
/*
Revised from cdoe
Visual C# 2005 Recipes A Problem-Solution Approach
# By Allen Jones
Matthew MacDonald
Rakesh Rajan
# ISBN: 1590595890
# ISBN-13: 9781590595893
# 592 pp.
# Published: Jan 2006
*/
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
public partial class ComboBoxHolder : Form
{
public ComboBoxHolder()
{
this.SuspendLayout();
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "ComboBoxHolder";
this.Text = "ComboBoxHolder";
this.ResumeLayout(false);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
AutoCompleteComboBox combo = new AutoCompleteComboBox();
combo.Location = new Point(10, 10);
this.Controls.Add(combo);
combo.Items.Add("word");
combo.Items.Add("world");
combo.Items.Add("wording");
combo.Items.Add("worse");
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new ComboBoxHolder());
}
}
public class AutoCompleteComboBox : ComboBox
{
private bool controlKey = false;
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
base.OnKeyPress(e);
if (e.KeyChar == (int)Keys.Escape)
{
this.SelectedIndex = -1;
this.Text = "";
controlKey = true;
} else if (Char.IsControl(e.KeyChar)) {
controlKey = true;
} else {
controlKey = false;
}
}
protected override void OnTextChanged(System.EventArgs e)
{
base.OnTextChanged(e);
if (this.Text != "" && !controlKey)
{
string matchText = this.Text;
int match = this.FindString(matchText);
if (match != -1)
{
this.SelectedIndex = match;
this.SelectionStart = matchText.Length;
this.SelectionLength = this.Text.Length - this.SelectionStart;
}
}
}
}
ComboBox focus leave event
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class ComboBoxes : Form
{
ComboBox cmb;
Button btnDisplay;
Button btnInsert;
Button btnSelect;
Label lblEdit;
TextBox txtDisplay;
Boolean boolChange = false;
Boolean boolProcessed = false;
public ComboBoxes()
{
Size = new Size(300,400);
this.Load += new EventHandler(this_Load);
cmb = new ComboBox();
cmb.Parent = this;
cmb.Location = new Point(10,10);
cmb.Size = new Size(ClientSize.Width / 2, Height - 200);
cmb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
cmb.DropDownStyle = ComboBoxStyle.DropDown;
cmb.DropDownStyle = ComboBoxStyle.Simple;
cmb.DropDownWidth = (int)(cmb.Width * 1.5);
cmb.MaxDropDownItems = 12;
cmb.MaxLength = 20;
cmb.SelectionChangeCommitted += new EventHandler(cmb_SelectionChangeCommitted);
cmb.Leave += new EventHandler(cmb_Leave);
btnInsert = new Button();
btnInsert.Parent = this;
btnInsert.Text = "&Insert Item";
btnInsert.Size = new Size((int)(Font.Height * .75) * btnInsert.Text.Length, cmb.Height);
btnInsert.Location = new Point(cmb.Right + 10, cmb.Top);
btnInsert.Click += new System.EventHandler(btnInsert_Click);
lblEdit = new Label();
lblEdit.Parent = this;
lblEdit.BorderStyle = BorderStyle.Fixed3D;
lblEdit.Location = new Point(cmb.Left, cmb.Bottom + 10);
lblEdit.BackColor = Color.LightGray;
lblEdit.Text = "";
lblEdit.Size = new Size(cmb.DropDownWidth, Font.Height * 2);
btnDisplay = new Button();
btnDisplay.Parent = this;
btnDisplay.Text = "&Display Items";
btnDisplay.Size = new Size((int)(Font.Height * .75) * btnDisplay.Text.Length, cmb.Height);
btnDisplay.Location = new Point(lblEdit.Left, lblEdit.Bottom + 10);
btnDisplay.Click += new System.EventHandler(btnDisplay_Click);
txtDisplay = new TextBox();
txtDisplay.Parent = this;
txtDisplay.Location = new Point(btnDisplay.Left, btnDisplay.Bottom + 10);
txtDisplay.Multiline = true;
txtDisplay.ReadOnly = true;
txtDisplay.BackColor = Color.LightGray;
txtDisplay.ScrollBars = ScrollBars.Vertical;
txtDisplay.Text = "";
txtDisplay.Size = new Size(cmb.DropDownWidth, 200);
btnSelect = new Button();
btnSelect.Parent = this;
btnSelect.Text = "&Select 4";
btnSelect.Size = new Size((int)(Font.Height * .75) * btnSelect.Text.Length, cmb.Height);
btnSelect.Location = new Point(btnDisplay.Right + 10, btnDisplay.Top);
btnSelect.Click += new System.EventHandler(btnSelect_Click);
cmb.Items.Add("A");
cmb.Items.Add("B");
cmb.Items.Add("C");
cmb.Items.Add("D");
cmb.Items.Add("E");
cmb.SelectedIndex = 0;
}
static void Main()
{
Application.Run(new ComboBoxes());
}
private void this_Load(object sender, EventArgs e)
{
cmb.TextChanged += new EventHandler(cmb_TextChanged);
cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
}
private void cmb_TextChanged(object sender, EventArgs e)
{
if (!boolProcessed)
lblEdit.Text = cmb.Text;
boolChange = true;
}
private void cmb_SelectedIndexChanged(object sender, EventArgs e)
{
if (boolChange)
{
boolChange = false;
boolProcessed = false;
}
}
private void cmb_SelectionChangeCommitted(object sender, EventArgs e)
{
if (boolChange)
ProcessChange();
}
private void cmb_Leave(object sender, EventArgs e)
{
if (boolChange)
{
ProcessChange();
boolChange = false;
}
}
private void ProcessChange()
{
lblEdit.Text = "Edited: " + cmb.Text;
boolProcessed = true;
}
private void btnDisplay_Click(object sender, EventArgs e)
{
string str = DateTime.Now.ToString() + "\r\n";
foreach (object item in cmb.Items)
{
str += item.ToString() + "\r\n";
}
txtDisplay.Text = str;
}
private void btnSelect_Click(object sender, EventArgs e)
{
cmb.Select(1,2);
}
private void btnInsert_Click(object sender, EventArgs e)
{
if (cmb.FindStringExact(cmb.Text) != -1)
{
MessageBox.Show(""" + cmb.Text + "" already exists in the list.\r\n" +
"Will not be added again.",
"Already Exists!");
}
else if (cmb.Text == "")
{
MessageBox.Show("There is nothing to add.","Nothing There");
}
else
{
cmb.Items.Add(cmb.Text);
}
}
}
ComboBox selection changed event
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class ComboBoxes : Form
{
ComboBox cmb;
Button btnDisplay;
Button btnInsert;
Button btnSelect;
Label lblEdit;
TextBox txtDisplay;
Boolean boolChange = false;
Boolean boolProcessed = false;
public ComboBoxes()
{
Size = new Size(300,400);
this.Load += new EventHandler(this_Load);
cmb = new ComboBox();
cmb.Parent = this;
cmb.Location = new Point(10,10);
cmb.Size = new Size(ClientSize.Width / 2, Height - 200);
cmb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
cmb.DropDownStyle = ComboBoxStyle.DropDown;
cmb.DropDownStyle = ComboBoxStyle.Simple;
cmb.DropDownWidth = (int)(cmb.Width * 1.5);
cmb.MaxDropDownItems = 12;
cmb.MaxLength = 20;
cmb.SelectionChangeCommitted += new EventHandler(cmb_SelectionChangeCommitted);
cmb.Leave += new EventHandler(cmb_Leave);
btnInsert = new Button();
btnInsert.Parent = this;
btnInsert.Text = "&Insert Item";
btnInsert.Size = new Size((int)(Font.Height * .75) * btnInsert.Text.Length, cmb.Height);
btnInsert.Location = new Point(cmb.Right + 10, cmb.Top);
btnInsert.Click += new System.EventHandler(btnInsert_Click);
lblEdit = new Label();
lblEdit.Parent = this;
lblEdit.BorderStyle = BorderStyle.Fixed3D;
lblEdit.Location = new Point(cmb.Left, cmb.Bottom + 10);
lblEdit.BackColor = Color.LightGray;
lblEdit.Text = "";
lblEdit.Size = new Size(cmb.DropDownWidth, Font.Height * 2);
btnDisplay = new Button();
btnDisplay.Parent = this;
btnDisplay.Text = "&Display Items";
btnDisplay.Size = new Size((int)(Font.Height * .75) * btnDisplay.Text.Length, cmb.Height);
btnDisplay.Location = new Point(lblEdit.Left, lblEdit.Bottom + 10);
btnDisplay.Click += new System.EventHandler(btnDisplay_Click);
txtDisplay = new TextBox();
txtDisplay.Parent = this;
txtDisplay.Location = new Point(btnDisplay.Left, btnDisplay.Bottom + 10);
txtDisplay.Multiline = true;
txtDisplay.ReadOnly = true;
txtDisplay.BackColor = Color.LightGray;
txtDisplay.ScrollBars = ScrollBars.Vertical;
txtDisplay.Text = "";
txtDisplay.Size = new Size(cmb.DropDownWidth, 200);
btnSelect = new Button();
btnSelect.Parent = this;
btnSelect.Text = "&Select 4";
btnSelect.Size = new Size((int)(Font.Height * .75) * btnSelect.Text.Length, cmb.Height);
btnSelect.Location = new Point(btnDisplay.Right + 10, btnDisplay.Top);
btnSelect.Click += new System.EventHandler(btnSelect_Click);
cmb.Items.Add("A");
cmb.Items.Add("B");
cmb.Items.Add("C");
cmb.Items.Add("D");
cmb.Items.Add("E");
cmb.SelectedIndex = 0;
}
static void Main()
{
Application.Run(new ComboBoxes());
}
private void this_Load(object sender, EventArgs e)
{
cmb.TextChanged += new EventHandler(cmb_TextChanged);
cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
}
private void cmb_TextChanged(object sender, EventArgs e)
{
if (!boolProcessed)
lblEdit.Text = cmb.Text;
boolChange = true;
}
private void cmb_SelectedIndexChanged(object sender, EventArgs e)
{
if (boolChange)
{
boolChange = false;
boolProcessed = false;
}
}
private void cmb_SelectionChangeCommitted(object sender, EventArgs e)
{
if (boolChange)
ProcessChange();
}
private void cmb_Leave(object sender, EventArgs e)
{
if (boolChange)
{
ProcessChange();
boolChange = false;
}
}
private void ProcessChange()
{
lblEdit.Text = "Edited: " + cmb.Text;
boolProcessed = true;
}
private void btnDisplay_Click(object sender, EventArgs e)
{
string str = DateTime.Now.ToString() + "\r\n";
foreach (object item in cmb.Items)
{
str += item.ToString() + "\r\n";
}
txtDisplay.Text = str;
}
private void btnSelect_Click(object sender, EventArgs e)
{
cmb.Select(1,2);
}
private void btnInsert_Click(object sender, EventArgs e)
{
if (cmb.FindStringExact(cmb.Text) != -1)
{
MessageBox.Show(""" + cmb.Text + "" already exists in the list.\r\n" +
"Will not be added again.",
"Already Exists!");
}
else if (cmb.Text == "")
{
MessageBox.Show("There is nothing to add.","Nothing There");
}
else
{
cmb.Items.Add(cmb.Text);
}
}
}
ComboBox text changed
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class ComboBoxes : Form
{
ComboBox cmb;
Button btnDisplay;
Button btnInsert;
Button btnSelect;
Label lblEdit;
TextBox txtDisplay;
Boolean boolChange = false;
Boolean boolProcessed = false;
public ComboBoxes()
{
Size = new Size(300,400);
this.Load += new EventHandler(this_Load);
cmb = new ComboBox();
cmb.Parent = this;
cmb.Location = new Point(10,10);
cmb.Size = new Size(ClientSize.Width / 2, Height - 200);
cmb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
cmb.DropDownStyle = ComboBoxStyle.DropDown;
cmb.DropDownStyle = ComboBoxStyle.Simple;
cmb.DropDownWidth = (int)(cmb.Width * 1.5);
cmb.MaxDropDownItems = 12;
cmb.MaxLength = 20;
cmb.SelectionChangeCommitted += new EventHandler(cmb_SelectionChangeCommitted);
cmb.Leave += new EventHandler(cmb_Leave);
btnInsert = new Button();
btnInsert.Parent = this;
btnInsert.Text = "&Insert Item";
btnInsert.Size = new Size((int)(Font.Height * .75) * btnInsert.Text.Length, cmb.Height);
btnInsert.Location = new Point(cmb.Right + 10, cmb.Top);
btnInsert.Click += new System.EventHandler(btnInsert_Click);
lblEdit = new Label();
lblEdit.Parent = this;
lblEdit.BorderStyle = BorderStyle.Fixed3D;
lblEdit.Location = new Point(cmb.Left, cmb.Bottom + 10);
lblEdit.BackColor = Color.LightGray;
lblEdit.Text = "";
lblEdit.Size = new Size(cmb.DropDownWidth, Font.Height * 2);
btnDisplay = new Button();
btnDisplay.Parent = this;
btnDisplay.Text = "&Display Items";
btnDisplay.Size = new Size((int)(Font.Height * .75) * btnDisplay.Text.Length, cmb.Height);
btnDisplay.Location = new Point(lblEdit.Left, lblEdit.Bottom + 10);
btnDisplay.Click += new System.EventHandler(btnDisplay_Click);
txtDisplay = new TextBox();
txtDisplay.Parent = this;
txtDisplay.Location = new Point(btnDisplay.Left, btnDisplay.Bottom + 10);
txtDisplay.Multiline = true;
txtDisplay.ReadOnly = true;
txtDisplay.BackColor = Color.LightGray;
txtDisplay.ScrollBars = ScrollBars.Vertical;
txtDisplay.Text = "";
txtDisplay.Size = new Size(cmb.DropDownWidth, 200);
btnSelect = new Button();
btnSelect.Parent = this;
btnSelect.Text = "&Select 4";
btnSelect.Size = new Size((int)(Font.Height * .75) * btnSelect.Text.Length, cmb.Height);
btnSelect.Location = new Point(btnDisplay.Right + 10, btnDisplay.Top);
btnSelect.Click += new System.EventHandler(btnSelect_Click);
cmb.Items.Add("A");
cmb.Items.Add("B");
cmb.Items.Add("C");
cmb.Items.Add("D");
cmb.Items.Add("E");
cmb.SelectedIndex = 0;
}
static void Main()
{
Application.Run(new ComboBoxes());
}
private void this_Load(object sender, EventArgs e)
{
cmb.TextChanged += new EventHandler(cmb_TextChanged);
cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
}
private void cmb_TextChanged(object sender, EventArgs e)
{
if (!boolProcessed)
lblEdit.Text = cmb.Text;
boolChange = true;
}
private void cmb_SelectedIndexChanged(object sender, EventArgs e)
{
if (boolChange)
{
boolChange = false;
boolProcessed = false;
}
}
private void cmb_SelectionChangeCommitted(object sender, EventArgs e)
{
if (boolChange)
ProcessChange();
}
private void cmb_Leave(object sender, EventArgs e)
{
if (boolChange)
{
ProcessChange();
boolChange = false;
}
}
private void ProcessChange()
{
lblEdit.Text = "Edited: " + cmb.Text;
boolProcessed = true;
}
private void btnDisplay_Click(object sender, EventArgs e)
{
string str = DateTime.Now.ToString() + "\r\n";
foreach (object item in cmb.Items)
{
str += item.ToString() + "\r\n";
}
txtDisplay.Text = str;
}
private void btnSelect_Click(object sender, EventArgs e)
{
cmb.Select(1,2);
}
private void btnInsert_Click(object sender, EventArgs e)
{
if (cmb.FindStringExact(cmb.Text) != -1)
{
MessageBox.Show(""" + cmb.Text + "" already exists in the list.\r\n" +
"Will not be added again.",
"Already Exists!");
}
else if (cmb.Text == "")
{
MessageBox.Show("There is nothing to add.","Nothing There");
}
else
{
cmb.Items.Add(cmb.Text);
}
}
}
Find item in a ComboBox
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class ComboBoxes : Form
{
ComboBox cmb;
Button btnDisplay;
Button btnInsert;
Button btnSelect;
Label lblEdit;
TextBox txtDisplay;
Boolean boolChange = false;
Boolean boolProcessed = false;
public ComboBoxes()
{
Size = new Size(300,400);
this.Load += new EventHandler(this_Load);
cmb = new ComboBox();
cmb.Parent = this;
cmb.Location = new Point(10,10);
cmb.Size = new Size(ClientSize.Width / 2, Height - 200);
cmb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
cmb.DropDownStyle = ComboBoxStyle.DropDown;
cmb.DropDownStyle = ComboBoxStyle.Simple;
cmb.DropDownWidth = (int)(cmb.Width * 1.5);
cmb.MaxDropDownItems = 12;
cmb.MaxLength = 20;
cmb.SelectionChangeCommitted += new EventHandler(cmb_SelectionChangeCommitted);
cmb.Leave += new EventHandler(cmb_Leave);
btnInsert = new Button();
btnInsert.Parent = this;
btnInsert.Text = "&Insert Item";
btnInsert.Size = new Size((int)(Font.Height * .75) * btnInsert.Text.Length, cmb.Height);
btnInsert.Location = new Point(cmb.Right + 10, cmb.Top);
btnInsert.Click += new System.EventHandler(btnInsert_Click);
lblEdit = new Label();
lblEdit.Parent = this;
lblEdit.BorderStyle = BorderStyle.Fixed3D;
lblEdit.Location = new Point(cmb.Left, cmb.Bottom + 10);
lblEdit.BackColor = Color.LightGray;
lblEdit.Text = "";
lblEdit.Size = new Size(cmb.DropDownWidth, Font.Height * 2);
btnDisplay = new Button();
btnDisplay.Parent = this;
btnDisplay.Text = "&Display Items";
btnDisplay.Size = new Size((int)(Font.Height * .75) * btnDisplay.Text.Length, cmb.Height);
btnDisplay.Location = new Point(lblEdit.Left, lblEdit.Bottom + 10);
btnDisplay.Click += new System.EventHandler(btnDisplay_Click);
txtDisplay = new TextBox();
txtDisplay.Parent = this;
txtDisplay.Location = new Point(btnDisplay.Left, btnDisplay.Bottom + 10);
txtDisplay.Multiline = true;
txtDisplay.ReadOnly = true;
txtDisplay.BackColor = Color.LightGray;
txtDisplay.ScrollBars = ScrollBars.Vertical;
txtDisplay.Text = "";
txtDisplay.Size = new Size(cmb.DropDownWidth, 200);
btnSelect = new Button();
btnSelect.Parent = this;
btnSelect.Text = "&Select 4";
btnSelect.Size = new Size((int)(Font.Height * .75) * btnSelect.Text.Length, cmb.Height);
btnSelect.Location = new Point(btnDisplay.Right + 10, btnDisplay.Top);
btnSelect.Click += new System.EventHandler(btnSelect_Click);
cmb.Items.Add("A");
cmb.Items.Add("B");
cmb.Items.Add("C");
cmb.Items.Add("D");
cmb.Items.Add("E");
cmb.SelectedIndex = 0;
}
static void Main()
{
Application.Run(new ComboBoxes());
}
private void this_Load(object sender, EventArgs e)
{
cmb.TextChanged += new EventHandler(cmb_TextChanged);
cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
}
private void cmb_TextChanged(object sender, EventArgs e)
{
if (!boolProcessed)
lblEdit.Text = cmb.Text;
boolChange = true;
}
private void cmb_SelectedIndexChanged(object sender, EventArgs e)
{
if (boolChange)
{
boolChange = false;
boolProcessed = false;
}
}
private void cmb_SelectionChangeCommitted(object sender, EventArgs e)
{
if (boolChange)
ProcessChange();
}
private void cmb_Leave(object sender, EventArgs e)
{
if (boolChange)
{
ProcessChange();
boolChange = false;
}
}
private void ProcessChange()
{
lblEdit.Text = "Edited: " + cmb.Text;
boolProcessed = true;
}
private void btnDisplay_Click(object sender, EventArgs e)
{
string str = DateTime.Now.ToString() + "\r\n";
foreach (object item in cmb.Items)
{
str += item.ToString() + "\r\n";
}
txtDisplay.Text = str;
}
private void btnSelect_Click(object sender, EventArgs e)
{
cmb.Select(1,2);
}
private void btnInsert_Click(object sender, EventArgs e)
{
if (cmb.FindStringExact(cmb.Text) != -1)
{
MessageBox.Show(""" + cmb.Text + "" already exists in the list.\r\n" +
"Will not be added again.",
"Already Exists!");
}
else if (cmb.Text == "")
{
MessageBox.Show("There is nothing to add.","Nothing There");
}
else
{
cmb.Items.Add(cmb.Text);
}
}
}
Get Items in a ComboBox
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class ComboBoxes : Form
{
ComboBox cmb;
Button btnDisplay;
Button btnInsert;
Button btnSelect;
Label lblEdit;
TextBox txtDisplay;
Boolean boolChange = false;
Boolean boolProcessed = false;
public ComboBoxes()
{
Size = new Size(300,400);
this.Load += new EventHandler(this_Load);
cmb = new ComboBox();
cmb.Parent = this;
cmb.Location = new Point(10,10);
cmb.Size = new Size(ClientSize.Width / 2, Height - 200);
cmb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
cmb.DropDownStyle = ComboBoxStyle.DropDown;
cmb.DropDownStyle = ComboBoxStyle.Simple;
cmb.DropDownWidth = (int)(cmb.Width * 1.5);
cmb.MaxDropDownItems = 12;
cmb.MaxLength = 20;
cmb.SelectionChangeCommitted += new EventHandler(cmb_SelectionChangeCommitted);
cmb.Leave += new EventHandler(cmb_Leave);
btnInsert = new Button();
btnInsert.Parent = this;
btnInsert.Text = "&Insert Item";
btnInsert.Size = new Size((int)(Font.Height * .75) * btnInsert.Text.Length, cmb.Height);
btnInsert.Location = new Point(cmb.Right + 10, cmb.Top);
btnInsert.Click += new System.EventHandler(btnInsert_Click);
lblEdit = new Label();
lblEdit.Parent = this;
lblEdit.BorderStyle = BorderStyle.Fixed3D;
lblEdit.Location = new Point(cmb.Left, cmb.Bottom + 10);
lblEdit.BackColor = Color.LightGray;
lblEdit.Text = "";
lblEdit.Size = new Size(cmb.DropDownWidth, Font.Height * 2);
btnDisplay = new Button();
btnDisplay.Parent = this;
btnDisplay.Text = "&Display Items";
btnDisplay.Size = new Size((int)(Font.Height * .75) * btnDisplay.Text.Length, cmb.Height);
btnDisplay.Location = new Point(lblEdit.Left, lblEdit.Bottom + 10);
btnDisplay.Click += new System.EventHandler(btnDisplay_Click);
txtDisplay = new TextBox();
txtDisplay.Parent = this;
txtDisplay.Location = new Point(btnDisplay.Left, btnDisplay.Bottom + 10);
txtDisplay.Multiline = true;
txtDisplay.ReadOnly = true;
txtDisplay.BackColor = Color.LightGray;
txtDisplay.ScrollBars = ScrollBars.Vertical;
txtDisplay.Text = "";
txtDisplay.Size = new Size(cmb.DropDownWidth, 200);
btnSelect = new Button();
btnSelect.Parent = this;
btnSelect.Text = "&Select 4";
btnSelect.Size = new Size((int)(Font.Height * .75) * btnSelect.Text.Length, cmb.Height);
btnSelect.Location = new Point(btnDisplay.Right + 10, btnDisplay.Top);
btnSelect.Click += new System.EventHandler(btnSelect_Click);
cmb.Items.Add("A");
cmb.Items.Add("B");
cmb.Items.Add("C");
cmb.Items.Add("D");
cmb.Items.Add("E");
cmb.SelectedIndex = 0;
}
static void Main()
{
Application.Run(new ComboBoxes());
}
private void this_Load(object sender, EventArgs e)
{
cmb.TextChanged += new EventHandler(cmb_TextChanged);
cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
}
private void cmb_TextChanged(object sender, EventArgs e)
{
if (!boolProcessed)
lblEdit.Text = cmb.Text;
boolChange = true;
}
private void cmb_SelectedIndexChanged(object sender, EventArgs e)
{
if (boolChange)
{
boolChange = false;
boolProcessed = false;
}
}
private void cmb_SelectionChangeCommitted(object sender, EventArgs e)
{
if (boolChange)
ProcessChange();
}
private void cmb_Leave(object sender, EventArgs e)
{
if (boolChange)
{
ProcessChange();
boolChange = false;
}
}
private void ProcessChange()
{
lblEdit.Text = "Edited: " + cmb.Text;
boolProcessed = true;
}
private void btnDisplay_Click(object sender, EventArgs e)
{
string str = DateTime.Now.ToString() + "\r\n";
foreach (object item in cmb.Items)
{
str += item.ToString() + "\r\n";
}
txtDisplay.Text = str;
}
private void btnSelect_Click(object sender, EventArgs e)
{
cmb.Select(1,2);
}
private void btnInsert_Click(object sender, EventArgs e)
{
if (cmb.FindStringExact(cmb.Text) != -1)
{
MessageBox.Show(""" + cmb.Text + "" already exists in the list.\r\n" +
"Will not be added again.",
"Already Exists!");
}
else if (cmb.Text == "")
{
MessageBox.Show("There is nothing to add.","Nothing There");
}
else
{
cmb.Items.Add(cmb.Text);
}
}
}
OwnerDraw ComboBox
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.ruboBox comboBox1 = new System.Windows.Forms.ruboBox();
private System.Windows.Forms.Label label1 = new System.Windows.Forms.Label();
private System.Windows.Forms.ruboBox comboBox2 = new System.Windows.Forms.ruboBox();
private System.Windows.Forms.Label label2 = new System.Windows.Forms.Label();
ArrayList colorArray = new ArrayList();
ArrayList fontArray = new ArrayList();
public Form1() {
this.SuspendLayout();
this.ruboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
this.ruboBox1.ItemHeight = 25;
this.ruboBox1.Location = new System.Drawing.Point(16, 40);
this.ruboBox1.Size = new System.Drawing.Size(264, 31);
this.ruboBox1.SelectedIndexChanged += new System.EventHandler(this.MyItemSelected);
this.ruboBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.ruboBox1_MeasureItem);
this.ruboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.ruboBox1_DrawItem);
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Size = new System.Drawing.Size(100, 16);
this.label1.Text = "Font Combo Box";
this.ruboBox2.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.ruboBox2.DropDownStyle = System.Windows.Forms.ruboBoxStyle.DropDownList;
this.ruboBox2.ItemHeight = 20;
this.ruboBox2.Location = new System.Drawing.Point(16, 104);
this.ruboBox2.Size = new System.Drawing.Size(264, 26);
this.ruboBox2.SelectedIndexChanged += new System.EventHandler(this.MyItemSelected);
this.ruboBox2.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.ruboBox2_DrawItem);
this.label2.Location = new System.Drawing.Point(24, 80);
this.label2.Text = "Color Combo Box";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(312, 157);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label2,
this.label1,
this.ruboBox1,
this.ruboBox2});
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e) {
colorArray.Add(new SolidBrush(Color.Yellow));
colorArray.Add(new SolidBrush(Color.Black));
colorArray.Add(new SolidBrush(Color.Azure));
colorArray.Add(new SolidBrush(Color.Firebrick));
colorArray.Add(new SolidBrush(Color.DarkMagenta));
comboBox2.Items.Add("A");
comboBox2.Items.Add("B");
comboBox2.Items.Add("C");
comboBox2.Items.Add("D");
comboBox2.Items.Add("E");
fontArray.Add(new Font("Ariel", 15, FontStyle.Bold));
fontArray.Add(new Font("Courier", 12, FontStyle.Italic));
fontArray.Add(new Font("Veranda", 14, FontStyle.Bold));
fontArray.Add(new Font("System", 10, FontStyle.Strikeout));
fontArray.Add(new Font("Century SchoolBook", 15, FontStyle.Underline));
comboBox1.Items.Add("W");
comboBox1.Items.Add("H");
comboBox1.Items.Add("P");
comboBox1.Items.Add("D");
comboBox1.Items.Add("L");
}
private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) {
Graphics g = e.Graphics;
Rectangle r = e.Bounds;
Font fn = null;
if (e.Index >= 0) {
fn = (Font)fontArray[e.Index];
string s = (string)comboBox1.Items[e.Index];
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 2), r);
if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect)) {
e.Graphics.FillRectangle(new SolidBrush(Color.White), r);
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r, sf);
e.DrawFocusRectangle();
} else {
e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Red), r, sf);
e.DrawFocusRectangle();
}
}
}
private void comboBox2_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) {
Graphics g = e.Graphics;
Rectangle r = e.Bounds;
if (e.Index >= 0) {
Rectangle rd = r;
rd.Width = 100;
Rectangle rt = r;
SolidBrush b = (SolidBrush)colorArray[e.Index];
g.FillRectangle(b, rd);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
Console.WriteLine(e.State.ToString());
e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 2), r);
if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect)) {
e.Graphics.FillRectangle(new SolidBrush(Color.White), r);
e.Graphics.DrawString(b.Color.Name, new Font("Ariel", 8, FontStyle.Bold), new SolidBrush(Color.Black), r, sf);
e.DrawFocusRectangle();
} else {
e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);
e.Graphics.DrawString(b.Color.Name, new Font("Veranda", 12, FontStyle.Bold), new SolidBrush(Color.Red), r, sf);
e.DrawFocusRectangle();
}
}
}
private void MyItemSelected(object sender, System.EventArgs e) {
ComboBox cb = null;
if (sender.Equals(comboBox1))
cb = comboBox1;
else
cb = comboBox2;
int x = cb.SelectedIndex;
if (sender.Equals(comboBox1)) {
Console.WriteLine("Item Selected is = " + (string)cb.Items[x]);
} else {
SolidBrush br = (SolidBrush)colorArray[x];
Console.WriteLine("Color Selected is = " + br.Color.Name);
}
}
private void comboBox1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e) {
if (e.Index % 2 == 0) {
e.ItemHeight = 45;
e.ItemWidth = 20;
} else {
e.ItemHeight = 25;
e.ItemWidth = 10;
}
}
}