Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Activating current control in UserControl
Message
From
30/10/2008 08:31:07
 
 
To
All
General information
Forum:
ASP.NET
Category:
Forms
Title:
Activating current control in UserControl
Environment versions
Environment:
C# 3.0
OS:
Vista
Network:
Windows 2008 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01358315
Message ID:
01358315
Views:
77
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
Next
Reply
Map
View

Click here to load this message in the networking platform