By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,201 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,201 IT Pros & Developers. It's quick & easy.

Force UserControl to activate last active control

P: n/a
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);
}
}

Oct 30 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Andrus" wrote:
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
Oct 30 '08 #2

P: n/a
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

Oct 30 '08 #3

P: n/a
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.

Oct 30 '08 #4

P: n/a
Hi Andrus
Form contains ToolStripContainer.
Just change

activeControl = toolStripContainer1.ActiveControl;

in ChildForm_Deactivate event.

Walter
Oct 30 '08 #5

P: n/a
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

Oct 30 '08 #6

P: n/a
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.

Oct 31 '08 #7

P: n/a
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);
}
}

Oct 31 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.