Creating SpinComboBoxEdit control

I saw a question about how to extend [DevEx]' ComboBoxEdit control to include spin down and up buttons in their forums. The buttons should cycle through available items. I thought I could easily create such a control and here it is in all its glory:

image

Note the two added spin buttons at the right side.

Now, let's test it. Here is my test data:

image

Clicking on left spin button will set the first value:

image

Next click on the same button will move the selection forward:

image

and so on.

And here is the required code

using System; using System.Collections.Generic; using System.Text; using DevExpress.XtraEditors.Repository; using System.Drawing; using DevExpress.XtraEditors.Registrator; using DevExpress.XtraEditors; using System.ComponentModel; using DevExpress.XtraEditors.Controls; using System.Collections; namespace Righthand.Editors { public class RepositoryItemSpinComboEdit : RepositoryItemComboBox { static RepositoryItemSpinComboEdit() { } public const string SpinComboEditName = "SpinComboEdit"; public override string EditorTypeName { get { return SpinComboEditName; } } public override void CreateDefaultButton() { base.CreateDefaultButton(); Buttons.Clear(); Buttons.Add(new EditorButton(ButtonPredefines.Combo)); Buttons.Add(new EditorButton(ButtonPredefines.SpinLeft)); Buttons.Add(new EditorButton(ButtonPredefines.SpinRight)); } public static void RegisterSpinComboEdit() { //Icon representing the editor within a container editor's Designer Image img = null; try { img = null; // your image; } catch { } EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(SpinComboEditName, typeof(SpinComboEdit), typeof(RepositoryItemSpinComboEdit), typeof(DevExpress.XtraEditors.ViewInfo.ComboBoxViewInfo), new DevExpress.XtraEditors.Drawing.ButtonEditPainter(), true, img)); } } public class SpinComboEdit: ComboBoxEdit { static SpinComboEdit() { RepositoryItemSpinComboEdit.RegisterSpinComboEdit(); } public SpinComboEdit() { Properties.ButtonClick += new ButtonPressedEventHandler(Properties_ButtonClick); } void Properties_ButtonClick(object sender, ButtonPressedEventArgs e) { if (e.Button.Index == 1) MoveSelection(1); else if (e.Button.Index == 2) MoveSelection(-1); } private void MoveSelection(int index) { int? indexOfItem = null; if (EditValue != null) { indexOfItem = Properties.Items.IndexOf(EditValue); } int newIndex = -1; if (indexOfItem.HasValue) newIndex = Math.Max(Math.Min(indexOfItem.Value+index, Properties.Items.Count - 1), 0); else if (index > 0 && Properties.Items.Count > 0) newIndex = 0; EditValue = Properties.Items[newIndex]; } public override string EditorTypeName { get { return RepositoryItemSpinComboEdit.SpinComboEditName; } } [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public new RepositoryItemSpinComboEdit Properties { get { return base.Properties as RepositoryItemSpinComboEdit; } } } }

Most of the code is required to derive my SpinComboEdit control out of [DevEx]' provided ComboBoxEdit control (see 7.2 help file topic Editor Features/01.Editor Structure/Custom Editors - or previous version help file, just this link won't work).

Points of interests are CreateDefaultButton() method where I create default buttons, SpinComboEdit() constructor where I attach to Properties.ButtonClick event and MoveSelection(int index) method where I actually move the selection forth and back. It should be pretty straightforward to understand.

I hope you'll find this sample useful and take note that this code can and should be further improved as it was a quickly built with demo purposes in mind.

Loading