Button Editor Bounds
Hi ,
I have a custom editor for one of my cells. It acts like a button. I am trying to perform certain action only when the button's mouse up event is triggered and the mouse is on top of the button. The problem I'm having is that I'm not able to access the bounds of the button or the cell. Can you please tell me how I can access this.
My code is below,
System.Windows.Forms.Button button = new System.Windows.Forms.Button();
button.BackColor = Color.Salmon;
button.MouseUp += delegate
{
//this.EditingCell is null
if (this.EditingCell.VisibleBounds.Contains(System.Windows.Forms.Cursor.Position))
{
System.Diagnostics.Debug.WriteLine("Test");
}
service.CloseCellControl(StopEditReason.UserStop);
};
-
Dear Deepak,
To edit cells, you should create a custom editor. There are two parts in this editor:
1. Painting methods. This editor can paint cell when the editor is not active. This can save GDI handles (especially if you have a lot of rows) and also it gives an impression that grid has controls in each cell.
2. When the user clicks on the cell, the grid calls the UITypeEditorEx.EditCell(...) method. Here you can create a real control, subscribe for its events and turn it in the subroutine.
Please find an example how to do it:
class MyEditor : UITypeEditorEx
{
public override bool GetPaintCellSupported()
{
return true;
}
public override void PaintCell(PaintCellEventArgs e)
{
//Draw some background
ControlPaint.DrawButton(e.Graphics, e.Cell.VirtualBounds, ButtonState.Normal);
//Prevent the grid from default background painting
e.Parts ^= e.Parts & PaintPart.Background;
}
public override StopEditReason EditCell(IGridEditorService service, Cell cell, StartEditReason reason)
{
StopEditReason stopReason;
//Create a real control
using(Button btn = new Button())
{
btn.BackColor = Color.Coral;
//Subscribe for button's event
btn.MouseUp += delegate
{
//Stop editing
service.CloseCellControl(StopEditReason.UserStop);
};
//Start editing in a subroutine. (This method waits for the user ends editing)
stopReason = service.CellEditControl(btn, cell.VirtualBounds, reason);
//Get here a value from the control
//cell.Value = _value_from_control_;
}
return stopReason;
}
}How to setup the editor:
grid.Headers[0]["Name"].Editable = true;
grid.Headers[0]["Name"].Editor = new MyEditor();Best regards,
Dapfor
0 -
Sorry, I should have probably been more specific with my question. I have handled the mouse up of the button editor but need to know if the current location of the mouse is above the button itself. Whats the best way to accomplish this ?
Thanks,
0 -
Hello,
The method System.Windows.Forms.Cursor.Position gets mouse location in screen coordinates. To use grid's hit tests, you should transform them to grid ones:
if (this.EditingCell.VisibleBounds.Contains(grid.PointToClient(System.Windows.Forms.Cursor.Position)))
{
...
}Best regards,
Dapfor
0 -
I tried out the code you provided, it does seems that this.EditingCell is always null. Is there another way to lookup the current cell ?
Thanks,
Deepak
0 -
Dear Deepak,
Grid.EditingCell is null until the service.CellEditControl(...) is called. This method creates a co-routine (like Form.ShowModal()) where it processes windows messages and dispatches them to the grid and to your control. When the user closes the control, the grid stops the co-routine. At this moment you can get all modified values from the control which no more has a GDI handle after the modal loop has been stopped. Note, this control isn't yet disposed! So, while the control exists, Grid.EditingCell is not null.
Best regards,
Dapfor
0 -
The .Net Grid implements exactly the same behavior as the standard PropertyControl.
Regards,
Dapfor
0
Please sign in to leave a comment.
Comments
6 comments