473,554 Members | 2,922 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ne w Mycontrols());
}
}

class Mycontrols : UserControl
{
public Mycontrols()
{
TextBox tb1 = new TextBox();
tb1.Text = "TextBox1";
TextBox tb2 = new TextBox();
Controls.Add(tb 1);
tb2.Top = 100;
tb2.Text = "TextBox2";
tb2.Select();
Controls.Add(tb 2);
}
}

Oct 30 '08 #1
7 14607
"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_Deact ivate(object sender, EventArgs e)
{
activeControl = ActiveControl;
}

private void ChildForm_Activ ated(object sender, EventArgs e)
{
if (activeControl != null)
activeControl.F ocus();
}

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 activeUserContr ol = null;

public ChildForm ()
{
InitializeCompo nent();
}

private void ChildForm _Deactivate(obj ect sender, EventArgs e)
{
activeControl = ActiveControl;
if (activeControl is IContainerContr ol)
{
activeUserContr ol =
((IContainerCon trol)activeCont rol).ActiveCont rol;
}
else activeUserContr ol = null;
}

private void ChildForm _Activated(obje ct sender, EventArgs e)
{
if (activeControl != null)
activeControl.F ocus();
if (activeUserCont rol != null)
activeUserContr ol.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 ToolStripContai ner.
ToolStripContai ner contents panel contain UserControl and DataGridView.

For TextBox in UserControl in Activated event probably

if (activeControl != null)
activeControl.F ocus();

activates ToolStripContai ner and

if (activeUserCont rol != null)
activeUserContr ol.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 ToolStripContai ner
?

Andrus.

Oct 30 '08 #4
Hi Andrus
Form contains ToolStripContai ner.
Just change

activeControl = toolStripContai ner1.ActiveCont rol;

in ChildForm_Deact ivate event.

Walter
Oct 30 '08 #5
one last more general approach:

Control activeControl = null;

private void ChildForm_Deact ivate(object sender, EventArgs e)
{
activeControl = this.ActiveCont rol;
while (activeControl is IContainerContr ol)
activeControl =
((IContainerCon trol)activeCont rol).ActiveCont rol;
}

private void ChildForm_Activ ated(object sender, EventArgs e)
{
if (activeControl != null)
activeControl.F ocus();
}

Walter

Oct 30 '08 #6
Walter,
one last more general approach:
Thank you.
I tried it in the following form:

Form contains ToolStripContai ner.
ToolStripcontai ner body contains UserControl and editable DataGridView
having editmode EditOnEnter
DataGridView has DataGridViewTex tBoxColumn 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()
{
InitializeCompo nent();
this.Load += new EventHandler(Fo rm1_Load);
}

void Form1_Load(obje ct sender, EventArgs e)
{
InitializeFocus Events(this);
}

private void InitializeFocus Events(Control control)
{
control.GotFocu s += new EventHandler(Co ntrol_GotFocus) ;
control.LostFoc us += new EventHandler(Co ntrol_LostFocus );
foreach (Control ctl in control.Control s)
{
InitializeFocus Events(ctl);
}
return;
}

void Control_GotFocu s(object sender, EventArgs e)
{
this.focusedCon trol = sender as Control;
}

void Control_LostFoc us(object sender, EventArgs e)
{
this.focusedCon trol = sender as Control;
}

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

void Form1_GotFocus( object sender, EventArgs e)
{
this.ActiveCont rol = this.focusedCon trol;
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.Ad d(new DataGridViewTex tBoxColumn());
grid.EditMode = DataGridViewEdi tMode.EditOnEnt er;
Controls.Add(gr id);
}
}

Oct 31 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
10051
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 = Screen.ActiveControl The code line work when all controls are placed on the form page rather than a tab control. This code should pass the last active control to...
2
5537
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 entered data. However I
3
2537
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 arranged in, up to, 15 vertical bars representing different customers. When the user clicks on one of the labels I display a list box with details...
6
17326
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? Str = activeControl.Name does not seem to work.
4
7838
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 Object, ByVal e As ...) Handles Form."EVENT HERE" dim Ok as Boolean = False ... If Not (ok = True) Then
4
5964
by: amnonevo | last post by:
Any equivalent to VB's PreviousControl in VB.NET?
0
1021
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
1530
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 first one to the last one.
2
2095
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 controls. I was trying to accomplish this Click procedure by capturing the active contold using activecontrol.name and then removing it, but as soon as I...
0
7780
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7530
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7862
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6119
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5414
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5136
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3539
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1994
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1111
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.