Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Activating current control in UserControl
Message
De
03/11/2008 13:43:58
 
 
À
02/11/2008 19:19:54
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:
01359231
Vues:
42
>>I think the behavior is 'by design' (see https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=115953)
>>If the object in question is a container than 'ActiveControl' returns the container itself (not the control within the container). You probably need to implement the same type of logic for the container itself (i.e track the Container.ActiveControl)
>
>Thank you.
>I added
>
> while (LastFocus is IContainerControl)
> LastFocus = ((IContainerControl)LastFocus).ActiveControl;
>
>and tested with code below but textbox in grid still does not get focus.
>No idea what I'm doing wrong.
>
>
>
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);
>        this.Activated += new EventHandler(Childform_Activated);
>        this.Deactivate += new EventHandler(Childform_Deactivate);
>    }
>
>    void Childform_Activated(object sender, EventArgs e)
>    {
>        if (this.LastFocus != null)
>            this.LastFocus.Focus();
>    }
>
>    void Childform_Deactivate(object sender, EventArgs e)
>    {
>        this.LastFocus = this.ActiveControl;
>        while (LastFocus is IContainerControl)
>            LastFocus = ((IContainerControl)LastFocus).ActiveControl;
>    }
>}
>
>class MyUserControl : UserControl
>{
>    internal MyUserControl()
>    {
>        Height = 100;
>        Controls.Add(new TextBox());
>    }
>}
>
Hi,

I haven't had time to try your code so this is just supposition. Although the drill-down in the Deactivate event may capture the right contol the Activated event won't find the reference

Maybe any nested ContainerControl needs to implement the same logic as the form itself (i.e store current control when focus moves away and restore focus to it when the ContainerControl itself gets focus)....
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform