Customizing Combo Boxes
.Net comes with the Combo Box control, but that Combo Box does not have any features of adding disabled items in to it. If you want to disable some items from the Combo Box you need to do the following,
- Have a custom class that defines the Combo Box items
- Create an item of this class and add it to the Combo Box
- Handle the paint event of the Combo Box so that you can print (draw) as you wish over the Combo Box
- Handle the SelectedIndexChanged event of the Combo Box to decide on the behavior of the Combo Box
- Override the ToString() of the custom class so that you can decide what to return when the Combo Box control.Add(item) is called
Explanation:
- Handling the paint event of any control under the OwnerDrawn mode will allow the user to choose what to paint when it is rendered on the screen. Using such a facility we can draw the unavailable items in Gray color there by giving the user a feel that the item is disabled.
- Handling the behavior is tricky. This can be done by holding a class level variable which can store the Index value of the previously clicked available function. Such a variable will be altered whenever the user clicks on an available function and remains the same in the other case.
For example,
using System.Windows.Forms; private ComboBox myFunctions = new ComboBox(); <In yourFormComboBox class> myFunctions.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable; myFunctions.DrawItem += new DrawItemEventHandler(Functions_DrawItem); myFunctions.SelectedIndexChanged += new EventHandler(Functions_SelectedIndexChanged); private int myPreviouslySelectedIndex; //initialise it to 0 in the constructor of this class //adding the items to the ComboBox ComboBoxFunction aComoboBoxFunction = new ComboBoxFunction("SUMMATION"); aComboBoxFunction.IsAvailabel = true; aComoboBoxFunction = new ComboBoxFunction("DIFFERENCE"); aComboBoxFunction.IsAvailabel = true; aComoboBoxFunction = new ComboBoxFunction("MULTIPLICATION"); aComboBoxFunction.IsAvailabel = false; private void Function_DrawItem(object sender, DrawItemEventArgs args) { //handling the appearance of the ComboBox int aCurrentIndex = args.Index; //gets the index value of the current ComboBox item that is drawn in the screen ComboBoxFunction aFunction = myFunctions[index] as ComboBoxFunction; //type casting to the custom object if(aFunction.IsAvailabel) { args.Graphics.DrawString(aFunction.ToString(), new Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.Black, new Point(args.Bounds.X, args.Bounds.Y)); } else { args.Graphics.DrawString(aFunction.ToString(), new Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.Gray, new Point(args.Bounds.X, args.Bounds.Y)); } } private void Function_SelectedIndexChanged(object sender, EventArgs args) { //handling the behavior of the ComboBox ComboBoxFunction aFunction = myFunctions.SelectedItem as ComboBoxFunction; if(aFunction != null) { if(aFunction.IsAvailable) { myPreviouslySelectedIndex = myFunctions.SelectedIndex; } else { //myPreviouslySelectedIndex remains the same myFunctions.SelectedIndex = myPreviouslySelectedIndex; } } } class ComboBoxFunction { private string myID; private string myValue; private bool myIsAvailable; private static System.Resources.ResourceManager myResource = new System.Resources.ResourceManager("ComboBoxExample.FormComboBox", typeof(FormComboBox).Assembly); public string ID { get { return ID ; } } public string Value { get { return myValue; } } public bool IsAvailabel { get { return value; } set { myIsAvailable = value; } } public ComboBoxFunction(string theID) { myID = theID; if(myID != null) { myValue = myResource.GetString(ID); } myIsAvailabel = false; } public override string ToString() { return myValue; } }
The above snippet will gives us a NLS (Native Language Supported) Combo Box with items SUMMATION (value of summation from the string table) enabled (black in color), DIFFERENCE enabled (again black in color) and MULTIPLICATION disabled (gray in color)
Please,continue
Cialisoi
July 17, 2008 at 6:40 pm