Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Activating current control in UserControl
Message
From
01/11/2008 19:49:02
 
 
To
01/11/2008 18:26:59
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:
01358916
Views:
33
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.

In the code you showed below,get rid of the OnClick and OnLoad code in the Child form and use the Activating/Deactivate, as I suggested. Here's how it should look:
class Childform : Form
{
    DataGridView grid;
    Control LastFocus = null;

    public Childform()
    {
        grid = new DataGridView();
        grid.Columns.Add(new DataGridViewTextBoxColumn());
        grid.EditMode = DataGridViewEditMode.EditOnEnter;
        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;
    }
} 
I've tried this and it works for me.

~~Bonnie


>>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.
>
>
>Thank you. This works for normal TextBoxes. However this does not work for grids.
>I tried code below for grids.
>Grid in EditOnenter mode seems to destroy TextBox windows handle when it loses focus. In Re-activating calling Focus() does not have any effect: Focus() returns false since there is no handle and does not set focus.
>I have some VirualMode grids in other forms which automatically restore focus. No ides why code below does not work.
>How to restore current control in DataGridView ?
>
>Probably we need to activate specific textbox in some special way, maybe
>remember and set cell coordinates directly?
>
>Andrus.
>
>
using System.Windows.Forms;
>using System.Collections.Generic;
>
>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;
>
>    public Childform()
>    {
>        grid = new DataGridView();
>        grid.Columns.Add(new DataGridViewTextBoxColumn());
>        grid.EditMode = DataGridViewEditMode.EditOnEnter;
>        Controls.Add(grid);
>    }
>
>
>    protected override void OnClick(System.EventArgs e)
>    {
>        base.OnClick(e);
>        grid.Focus();
>    }
>
>    protected override void OnLoad(System.EventArgs e)
>    {
>        base.OnLoad(e);
>        grid.Focus();
>    }
>} 
>
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform