>You could have a control variable that gets set to the ActiveControl when the Form loses focus, and then sets the focus back to that control when the Form gets focus again. I *think* (not sure and don't have time to test right now), you can use the Activate/Deactivate events to do this.
Thank you. This works for normal TextBoxes. However this does not work for grids.
I tried code below for grids.
Grid in EditOnenter mode seems to destroy TextBox windows handle when it loses focus. In Re-activating calling Focus() does not have any effect: Focus() returns false since there is no handle and does not set focus.
I have some VirualMode grids in other forms which automatically restore focus. No ides why code below does not work.
How to restore current control in DataGridView ?
Probably we need to activate specific textbox in some special way, maybe
remember and set cell coordinates directly?
Andrus.
using System.Windows.Forms;
using System.Collections.Generic;
public class Test
{
static void Main()
{
Application.Run(new MainForm());
}
}
class MainForm : Form
{
public MainForm()
{
WindowState = FormWindowState.Maximized;
IsMdiContainer = true;
Form frm = new Childform();
frm.MdiParent = this;
frm.Show();
Form frm2 = new Childform();
frm2.MdiParent = this;
frm2.Show();
frm2.Left = 2000;
}
}
class Childform : Form
{
DataGridView grid;
public Childform()
{
grid = new DataGridView();
grid.Columns.Add(new DataGridViewTextBoxColumn());
grid.EditMode = DataGridViewEditMode.EditOnEnter;
Controls.Add(grid);
}
protected override void OnClick(System.EventArgs e)
{
base.OnClick(e);
grid.Focus();
}
protected override void OnLoad(System.EventArgs e)
{
base.OnLoad(e);
grid.Focus();
}
}
Andrus