using System.Windows.Forms; >using System.Collections.Generic; >using System; > >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; > Control LastFocus = null; > > public Childform() > { > ToolStripContainer tc = new ToolStripContainer(); > > grid = new DataGridView(); > grid.Columns.Add(new DataGridViewTextBoxColumn()); > grid.EditMode = DataGridViewEditMode.EditOnEnter; > grid.Top = 120; > grid.Height = 300; > Controls.Add(tc); > tc.ContentPanel.Controls.Add(new MyUserControl()); > > tc.ContentPanel.Controls.Add(grid); > } > > protected override void OnActivated(EventArgs e) > { > base.OnActivated(e); > if (this.LastFocus != null) > { > grid.Focus(); > this.LastFocus.Focus(); > } > } > > protected override void OnDeactivate(EventArgs e) > { > base.OnDeactivate(e); > this.LastFocus = this.ActiveControl; > while (LastFocus is IContainerControl) > LastFocus = ((IContainerControl)LastFocus).ActiveControl; > } >} > >class MyUserControl : UserControl >{ > internal MyUserControl() > { > Height = 100; > Controls.Add(new TextBox()); > } >} >