Skip to main content Skip to footer

Change cursor type based on position in cell

Recently, a customer inquired on whether it was possible to change the cursor type depending on its location in a cell. The scenario was regarding a combobox cell type. The cursor needed to remain a spreadsheet cursor and only turn into a pointer if it was hovering over the drop down button. Also, it would only drop down by single click if the button is selected and drop down by double click if it's selected anywhere else in the cell. By default, the cursor will turn into a pointer and drop down by single click regardless of where the user clicks in it. The feature is achieved simply by creating your own custom cell type. Firstly, you would need to create your own subclass and have it inherit the existing combobox cell type. By overriding the IsReservedLocation, you can define whether a certain position in a cell should have special meaning to the editor control. If this method returns null, then it recognizes that the cursor is in a normal location (double click to enable edit mode.) If it returns non-null, then the location is in a special area (single click to enable edit mode.) As you can see below, we designated the button as the special area. The normal area will maintain the default spreadsheet cursor and hovering over the special area will turn it into a pointer.


    public class MyComboType : FarPoint.Win.Spread.CellType.ComboBoxCellType  
    {  
        public override object IsReservedLocation(Graphics g, int x, int y, Rectangle rc, FarPoint.Win.Spread.Appearance appearance, object value, float zoomFactor)  
        {  
            Rectangle rectButton = new Rectangle((rc.X + rc.Width - SystemInformation.VerticalScrollBarWidth), rc.Y, SystemInformation.VerticalScrollBarWidth, rc.Height);  

            if (rectButton.Contains(new Point(x, y)))  
            {  
                return this;  
            }  
            return null;  
        }  
    }  

If you need to use a different cursor type other than the spreadsheet cursor and the pointer, the GetReservedCursor method can be overwritten. The value from the IsReservedLocation method will pass to it, which then you can apply the necessary logic.


public override Cursor GetReservedCursor(object o)  
{  
if (o != null)  
return Cursors.Arrow;  
else  
return null;  
}  

MESCIUS inc.

comments powered by Disqus