473,732 Members | 2,204 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 14644
"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
10066
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 the calling module. Then the following line requests the tabindex from the control.
2
5547
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
2549
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 about the data in the bar. I only want to know which stack and which bar was clicked so I can...
6
17345
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
7851
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
5968
by: amnonevo | last post by:
Any equivalent to VB's PreviousControl in VB.NET?
0
1041
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
1543
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
2108
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 click on the Delete button, the active controls changes to the delete button, not the control that...
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9447
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.