|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis control came out of my need to create a
ImplementationThe Therefore, my custom list portion appears either on the
protected override void OnDropDown(EventArgs e) {
base.OnDropDown(e);
DoDropDown();
}
private void DoDropDown() {
if (!dropdown.Visible) {
Rectangle rect = RectangleToScreen(this.ClientRectangle);
dropdown.Location = new Point(rect.X, rect.Y + this.Size.Height);
int count = dropdown.List.Items.Count;
if (count > this.MaxDropDownItems) {
count = this.MaxDropDownItems;
} else if (count == 0) {
count = 1;
}
dropdown.Size = new Size(this.Size.Width,
(dropdown.List.ItemHeight + 1) * count);
dropdown.Show(this);
}
}
protected override void OnKeyDown(KeyEventArgs e) {
if (e.KeyCode == Keys.Down) {
OnDropDown(null);
}
// Make sure that certain keys or combinations are not blocked.
e.Handled = !e.Alt && !(e.KeyCode == Keys.Tab) &&
!((e.KeyCode == Keys.Left) || (e.KeyCode == Keys.Right) ||
(e.KeyCode == Keys.Home) || (e.KeyCode == Keys.End));
base.OnKeyDown(e);
}
protected override void OnKeyPress(KeyPressEventArgs e) {
e.Handled = true;
base.OnKeyPress(e);
}
Now, the list portion, being a In order to achieve this, I had to catch the protected override void OnDeactivate(EventArgs e) {
base.OnDeactivate(e);
CCBoxEventArgs ce = e as CCBoxEventArgs;
if (ce != null) {
CloseDropdown(ce.AssignValues);
} else {
CloseDropdown(true);
}
}
Using the CodeThe code comes as a demo project which you can run and see as an example of usage. In order to use the Alternatively, you can very easily use it by manually writing code like shown below to declare and create an instance of a private CheckedComboBox ccb = new CheckedComboBox();
// If more than 5 items, add a scroll bar to the dropdown.
ccb.MaxDropDownItems = 5;
// Make the "Name" property the one to display, rather than the ToString()
// representation of the item.
ccb.DisplayMember = "Name";
// Set a separator for how the checked items will appear in the Text portion.
ccb.ValueSeparator = ", ";
In order to add items, use the private string[] coloursArr = { "Red", "Green", "Black",
"White", "Orange", "Yellow",
"Blue", "Maroon", "Pink", "Purple" };
for (int i = 0; i < coloursArr.Length; i++) {
CCBoxItem item = new CCBoxItem(coloursArr[i], i);
ccb.Items.Add(item);
}
... where the public class CCBoxItem {
private int val;
public int Value {
get { return val; }
set { val = value; }
}
private string name;
public string Name {
get { return name; }
set { name = value; }
}
public CCBoxItem() {
}
public CCBoxItem(string name, int val) {
this.name = name;
this.val = val;
}
public override string ToString() {
return string.Format("name: '{0}', value: {1}", name, val);
}
}
You can also programmatically check an item or set its // Check first item (index == 0)
ccb.SetItemChecked(0, true);
// Set the CheckState of the 2nd item in the list to Indeterminate.
ccb.SetItemCheckState(1, CheckState.Indeterminate);
The most interesting events you would probably be catching in your application are when the drop down portion closes ( Note that the // Add the handlers to the CheckedComboBox
this.ccb.DropDownClosed += new System.EventHandler(this.ccb_DropDownClosed);
ccb.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.ccb_ItemCheck);
// Handler implementation
private void ccb_DropDownClosed(object sender, EventArgs e) {
txtOut.AppendText("DropdownClosed\r\n");
txtOut.AppendText(string.Format("value changed: {0}\r\n", ccb.ValueChanged));
txtOut.AppendText(string.Format("value: {0}\r\n", ccb.Text));
// Display all checked items.
StringBuilder sb = new StringBuilder("Items checked: ");
// Checked items can be found via the CheckedItems property.
foreach (CCBoxItem item in ccb.CheckedItems) {
sb.Append(item.Name).Append(ccb.ValueSeparator);
}
sb.Remove(sb.Length-ccb.ValueSeparator.Length, ccb.ValueSeparator.Length);
txtOut.AppendText(sb.ToString());
txtOut.AppendText("\r\n");
}
private void ccb_ItemCheck(object sender, ItemCheckEventArgs e) {
CCBoxItem item = ccb.Items[e.Index] as CCBoxItem;
txtOut.AppendText(string.Format("Item '{0}' is about to be {1}\r\n",
item.Name, e.NewValue.ToString()));
}
In the code above, you can see that the checked items are available via the HistoryIn version 2.0, I have included some minor fixes/enhancements as follows:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||