Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Activating current control in UserControl
Message
From
02/11/2008 09:25:01
 
 
To
01/11/2008 19:49:02
General information
Forum:
ASP.NET
Category:
Forms
Environment versions
Environment:
C# 3.0
OS:
Vista
Network:
Windows 2008 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01358315
Message ID:
01358957
Views:
30
Bonnie,

>I don't know Andrus, what I proposed works for me in your scenario. Your mistake I think is using grid.Focus(), which is not what I had suggested to begin with. I said to use "ActiveControl", which would apparently, in the case of a grid, be the cell you were in.

Than you.
I modified and tried your code below but TextBox in grid is still not activated on form re-activation.

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;
    }
}

class MyUserControl : UserControl
{
    internal MyUserControl()
    {
        Height = 100;
        Controls.Add(new TextBox());
    }
}
Andrus
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform