Creating SpinComboBoxEdit control

by Miha Markič 24. July 2007 11:13

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.

Tags:

DevExpress | .net

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Miha Markic

About me
Righthand
 
Microsoft MVP
 
Developer Express' DXSquad
INETA Country Leader for Slovenia
INETA Country Leader for Slovenia

Slovene Developer Users Group Lead
Friends of Red-Gate
LLBLGenPro Partner

Miha currently works as a free lance consultant and software developer specialized in .net area.
He graduated in Computer and information science at the University of Ljubljana, Slovenia. He has accumulated experience in various programming languages such as Java, Visual Basic 3-6 (MCP), Visual C++, Delphi, C# and VB.Net through years.
He has experience in practically all (technical) stages of project development, including planning, framework development, user interface, business processes, as well as testing and documenting. He has worked on big and small projects in Slovenia and abroad (e.g. participated in completing level 3 IS for the Nucor steel plant, Hertford, USA).
Currently he enjoys programming in .net environment using C#. Since 2000 he has been active in Developer Express' DX Squad and has been ECDL trainer and tester. He also gives lectures on conferences and other events in Slovenia.

Month List

Tag cloud

Most comments

Paulius Paulius
1 comments
us United States
Meh Meh
1 comments
us United States
bart dm bart dm
1 comments
nl Netherlands

RecentComments

Comment RSS