Cannot set focused cell in constructor
Brought to you by:
davideicardi
Don't know exactly if this is a bug or by design:
After setting grid properties in a form constructor (or in OnLoad event handler as you do in various samples) it's impossible to set the focused cell, to make the default selected cell when the form is created.
How to reproduce:
Try in form sample 14, in the onload event handler add:
grid1.Selection.Focus(5, 0)
Logged In: YES
user_id=1515232
Originator: NO
Unfortunately this is a limitation of the Windows Forms model. The problem is that you cannot set the focus on a control (any windows forms control) before the OnLoad. I suggest to use the first OnActivated event.
For now I set the bug status to closed because I don't think that there is a way to solve this problem.
Logged In: YES
user_id=1346654
Originator: YES
I'm aware of the windows forms behaviour (you can solve it also putting code in the OnShow event handler), but I also think it would be very useful to have a way to set the cell that would be initially focused when the form gets open.
Logged In: YES
user_id=1515232
Originator: NO
Ok, I will think if I can find a solution.
(maybe storing in a variable the last cell that the user want to focus and use it when the grid receive the focus....)
Logged In: YES
user_id=1346654
Originator: YES
Hi,
I solved the problem by creating a method SetFocusedCell with this code:
private void SetSelectedCell(int row, int col)
{
if (grid.CanFocus)
grid.Selection.Focus(new SourceGrid.Position(row, col));
else
{
grid.Selection.Clear();
grid.Selection.Add(new SourceGrid.Position(row, col));
}
}
Just an idea, but I find this fits my needs.
Many thank for the help,
Frank