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

private void ActivateHandler(object sender, EventArgs e)
{
    if (this.LastControl != null)
        this.LastControl.Focus();
}
private void DeactivateHandler(object sender, EventArgs e)
{
    this.LastControl = this.ActiveControl;
}
This is off the top of my head, but it should give you some ideas.

~~Bonnie




>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);
>    }
>}
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform