473,387 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Force UserControl to activate last active control

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
7 14584
"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
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
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
Hi Andrus
Form contains ToolStripContainer.
Just change

activeControl = toolStripContainer1.ActiveControl;

in ChildForm_Deactivate event.

Walter
Oct 30 '08 #5
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert Neville | last post by:
How do you return the last Active Control when the control is on a tab control? Screen.ActiveControl does not seem to work. Here's the statement that my code uses. Set ctlOld =...
2
by: Georges Heinesch | last post by:
Hi. Is it possible to change the position of the cursor inside an active control? In my example ... Me!txtControl.Text = Me!txtControl.Text .... puts the cursor to the left side of the...
3
by: Hank | last post by:
On one of my forms I display a histogram representing the status of jobs in our factory. It's configured as a stacked bar which I create using labels of different background colors. 75 labels are...
6
by: Woody Splawn | last post by:
I know that I can do the following to identify the parent name of an active control Dim sParentName As String = ActiveControl.Parent.Name But how do I identify the name of the active control?...
4
by: Volker Jobst | last post by:
Hi, Is there an event of windows.forms.form which informs me that the active control will be changed before it will be changed? Something like: Private Sub Form_Validating(ByVal sender As...
4
by: amnonevo | last post by:
Any equivalent to VB's PreviousControl in VB.NET?
0
by: Mark Chen | last post by:
Hello : My problem is my active control will download to IE6 sp2, but not download to IE6 sp1. On IE6 sp1, ther is no popup to ask for download. Mark
1
by: techCrazy | last post by:
How do I go to last 'active' tab? :tabp goes to current tab - 1. tabp *:tabp* *:tabprevious* *gT* :tabN *:tabN* *:tabNext* gT Go to the previous tab page. Wraps around from the...
2
by: Randy | last post by:
I have a form on which various controls are added dynamically depending on actions of the user. One of these actions allows the user to click a delete button that will remove various other...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.