Force UserControl to activate last active control

A

Andrus

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) ?

Andrus.


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

Walter Frank

Andrus said:
How to force UserControl to forward focus to child its current child control
(TextBox2) ?

Hi Andrus,

remember the active control in your child form and focus it on activation:

private Control activeControl = null;

private void ChildForm_Deactivate(object sender, EventArgs e)
{
activeControl = ActiveControl;
}

private void ChildForm_Activated(object sender, EventArgs e)
{
if (activeControl != null)
activeControl.Focus();
}

you have to do the same in your UserControl!

Walter
 
W

Walter Frank

finished the example, because needed it myself:

public partial class ChildForm : Form
{
Control activeControl = null;
Control activeUserControl = null;

public ChildForm ()
{
InitializeComponent();
}

private void ChildForm _Deactivate(object sender, EventArgs e)
{
activeControl = ActiveControl;
if (activeControl is IContainerControl)
{
activeUserControl =
((IContainerControl)activeControl).ActiveControl;
}
else activeUserControl = null;
}

private void ChildForm _Activated(object sender, EventArgs e)
{
if (activeControl != null)
activeControl.Focus();
if (activeUserControl != null)
activeUserControl.Focus();
}
}


Walter
 
A

Andrus

Walter,
finished the example, because needed it myself:

Thank you.
I tried your code with the following form:

Form contains ToolStripContainer.
ToolStripContainer contents panel contain UserControl and DataGridView.

For TextBox in UserControl in Activated event probably

if (activeControl != null)
activeControl.Focus();

activates ToolStripContainer and

if (activeUserControl != null)
activeUserControl.Focus();

activates UserControl.

In this case, this code does not set focus to proper TextBox. I tried also
to set TextBox in DataGridView as active control. It does not set focus to
DataGrdiView control also.

How to use this code with Usercontrol and DataGridView in ToolStripContainer
?

Andrus.
 
W

Walter Frank

one last more general approach:

Control activeControl = null;

private void ChildForm_Deactivate(object sender, EventArgs e)
{
activeControl = this.ActiveControl;
while (activeControl is IContainerControl)
activeControl =
((IContainerControl)activeControl).ActiveControl;
}

private void ChildForm_Activated(object sender, EventArgs e)
{
if (activeControl != null)
activeControl.Focus();
}

Walter
 
A

Andrus

Walter,
one last more general approach:

Thank you.
I tried it in the following form:

Form contains ToolStripContainer.
ToolStripcontainer body contains UserControl and editable DataGridView
having editmode EditOnEnter
DataGridView has DataGridViewTextBoxColumn added in code.

When Editable TextBox in this DataGridView has focus, it is not focused on
form re-activate.
Focus() returns false for textbox.
Stepping into Focus() source code shows that this is caused probably because
TextBox does not have handle created: probably on deactivate
DataGridView releases TextBox because it has mode which creates edit control
only when column is activated.

Any ides how to fix this without changing datagridview edit modes?
Maybe we should activate grid container control first like in your first
code ?

An alternate, untested approach may be:

Control focusedControl;

public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}

void Form1_Load(object sender, EventArgs e)
{
InitializeFocusEvents(this);
}

private void InitializeFocusEvents(Control control)
{
control.GotFocus += new EventHandler(Control_GotFocus);
control.LostFocus += new EventHandler(Control_LostFocus);
foreach (Control ctl in control.Controls)
{
InitializeFocusEvents(ctl);
}
return;
}

void Control_GotFocus(object sender, EventArgs e)
{
this.focusedControl = sender as Control;
}

void Control_LostFocus(object sender, EventArgs e)
{
this.focusedControl = sender as Control;
}

void Form1_LostFocus(object sender, EventArgs e)
{
return;
}

void Form1_GotFocus(object sender, EventArgs e)
{
this.ActiveControl = this.focusedControl;
return;
}

Andrus.
 
A

Andrus

I created testcase for it:

1. Run code.
2. Enter some data to grid
3. Click other form caption
4. Click original form caption
5. Enter some characters

Observed: entered characters are ignored

Expected: entered characters must appear in last cell before form activation

How to fix ?

Andrus.

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()
{
var grid = new DataGridView();
grid.Columns.Add(new DataGridViewTextBoxColumn());
grid.EditMode = DataGridViewEditMode.EditOnEnter;
Controls.Add(grid);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top