Forcing Usercontrol to forward focus to its current child control
-
I have UserControls in MDI child forms containing TextBoxes and other controls. When user re-activates form, I need that Control which was last activated is activated again. Currently *first* control is activated always. To reproduce: 1. Run code. 2. Make TextBox2 as current TextBox by selecting its text 3. Activate other form 4. Activate previous form by clicking in form title bar Observed: TextBox1 receives focus Expected: TextBox2 should receive focus How to force UserControl to forward focus to child its current child control (TextBox2) ?
using System.Windows.Forms; 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 { public Childform() { Controls.Add(new Mycontrols()); } } class Mycontrols : UserControl { public Mycontrols() { TextBox tb1 = new TextBox(); tb1.Text = "TextBox1"; TextBox tb2 = new TextBox(); Controls.Add(tb1); tb2.Top = 100; tb2.Text = "TextBox2"; tb2.Select(); Controls.Add(tb2); } }
Andrus