Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Activating current control in UserControl
Message
De
30/10/2008 08:31:07
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Formulaires
Titre:
Activating current control in UserControl
Versions des environnements
Environment:
C# 3.0
OS:
Vista
Network:
Windows 2008 Server
Database:
MS SQL Server
Divers
Thread ID:
01358315
Message ID:
01358315
Vues:
76
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
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform