Skip to main content

How to use a button as an editor?

Comments

1 comment

  • Dapfor Team

    Hello,

    1. Windows has a serious limitation in number of GDI handlers per application (about 10 000). Each control consumes a handle and of you create a real control per cell - the application will rapidly reach the limit and will crash. The single solution is to graphically emulate the control for inactive cells and create a real control when the user starts editing in the cell. The control will be destroyed when the user ends editing. Below we’ll provide an example demonstrating how to implement this feature:

    internal class ButtonEditor : UITypeEditorEx
    {
        public override bool GetPaintCellSupported()
        {
            return true;
        }

        public override void PaintCell(PaintCellEventArgs e)
        {
            base.PaintCell(e);

    //Do your custom painting
            ControlPaint.DrawButton(e.Graphics, e.Cell.VirtualBounds, ButtonState.Normal);
        }

        public override StopEditReason EditCell(IGridEditorService service, Cell cell, StartEditReason reason)
        {
    //Create a control
            Button button = new Button();

    //Add desired callbacks
            button.Click += delegate
            {
    //When the user clicks on the button - stop editing
                service.CloseCellControl(StopEditReason.CloseControl);

                //Set a new value here
                //cell.Value = _new_value;
            };

    //Attach newly created control to the grid. (The control becomes visible here)
            return service.CellEditControl(button, cell.VirtualBounds, reason);
        }
    }

     

    2. Focusing is a grid's attribute: only single row/column can be focused. Selection is a row's attribute. In other words multiple rows can be selected. Disable selection to get the wanted result:

    _grid.Selection.Enabled = false;

     

    Best regards,

    Dapfor

     

     

    0

Please sign in to leave a comment.

Powered by Zendesk