Skip to main content Skip to footer

Custom ComboBoxCell in Spread for WinForms

Spread for WinForms supports multiple celltypes like CheckBoxCellType, NumberCellType, CurrencyCellType, ButtonCellType, DateTimeCellType ,ComboBoxCellType etc. All of these celltypes are customizable. In this post I am going to tell you how you can create a custom ComboBox CellType. We will create a custom combobox cell which will have the following features:

  1. Enter the edit mode on single click and also by pressing enter key.
  2. Will show the Arrow cursor only when the mouse pointer is exactly over the dropdown button and not over the complete cell.

All graphical celltypes with Spread, have few methods that can be overridden when customizing its default behavior. Each cell type with Spread is a class which can be inherited and modified as per requirement. Here we will override the StartEdit method for ComboBoxCellType so that the editing behavior can be changed to what we are looking for. In StartEdit method try to get the CellRectangle with GetCellRectangle method. Check for Mouse click and cast the EventArgs from StartEdit to MouseEventArgs. If it is found to be not null we start editing the ComboBox cell. If is it not a MouseClick then it starts editing on Enter key press. Please have a look at the code snippet below:


   public class MyComboType : FarPoint.Win.Spread.CellType.ComboBoxCellType  
   {  
      private FarPoint.Win.Spread.FpSpread ss = null;  
      public MyComboType(FarPoint.Win.Spread.FpSpread ss) { this.ss = ss; }  
      public override void StartEditing(EventArgs e, bool selectAll, bool autoClipboard)  
      {  
         Rectangle rc = ss.GetCellRectangle(ss.GetActiveRowViewportIndex(),        ss.GetActiveColumnViewportIndex(), ss.ActiveSheet.ActiveRowIndex, ss.ActiveSheet.ActiveColumnIndex);  
         MouseEventArgs me = e as MouseEventArgs;  
         if (me != null)  
         {  
            Rectangle rectButton;  
            rectButton = new Rectangle(rc.X + rc.Width - 14, rc.Y, 14, rc.Height);  
            if (!rectButton.Contains(new Point(((MouseEventArgs)e).X, ((MouseEventArgs)e).Y)))  
            {  
            // don't drop the list  
            return;  
            }  
         }  
         else  
         {  
         // start editing with "enter" key  
            if (e==null)  
            {  
            me = new MouseEventArgs(MouseButtons.Left, 1, rc.X + 1, rc.Y + 1, 0);  
            base.StartEditing(me, selectAll, autoClipboard);  
            return;  
            }  
         }  
         base.StartEditing(e, selectAll, autoClipboard);  
      }  
   }  

CustomComboBox CustomComboBox Please download application to have a better understanding of above implementation: Download VB.Net Sample _ Download C# Sample_

MESCIUS inc.

comments powered by Disqus