471,571 Members | 1,014 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Is it possible to know when TextBox loses focus to which object it

Hi,

Is it possible to know when TextBox loses focus to which object it lost it?
what i mean is when the LostFocus Event occur is it possible to know which
object got the focus instead?

thanks,
Nov 17 '05 #1
3 2055
Hi Gidi,

I believe you will need to traverse all the controls to see which one has the Focused property set.

private void textBox_LostFocus(object sender, EventArgs e)
{
foreach(Control c in this.Controls)
{
if(c.Focused)
label1.Text = c.Name;
}
}

Of course, if you got any container controls with child controls inside you will need to do it recursively or, when creating the Form, add all 'focusable' controls to a list of your own.
On Thu, 07 Jul 2005 10:18:04 +0200, Gidi <sh*****@hotmail.com.dontspam> wrote:
Hi,

Is it possible to know when TextBox loses focus to which object it lost it?
what i mean is when the LostFocus Event occur is it possible to know which
object got the focus instead?

thanks,


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2
better would be to use the ActiveControl property of
System.Windows.Forms.Form class.

"Morten Wennevik" <Mo************@hotmail.com> schrieb im Newsbeitrag
news:op.stjcfbvjklbvpo@stone...
Hi Gidi,

I believe you will need to traverse all the controls to see which one has the Focused property set.
private void textBox_LostFocus(object sender, EventArgs e)
{
foreach(Control c in this.Controls)
{
if(c.Focused)
label1.Text = c.Name;
}
}

Of course, if you got any container controls with child controls inside you will need to do it recursively or, when creating the Form, add all
'focusable' controls to a list of your own.

On Thu, 07 Jul 2005 10:18:04 +0200, Gidi <sh*****@hotmail.com.dontspam> wrote:
Hi,

Is it possible to know when TextBox loses focus to which object it lost it? what i mean is when the LostFocus Event occur is it possible to know which object got the focus instead?

thanks,


--
Happy coding!
Morten Wennevik [C# MVP]

Nov 17 '05 #3

Well, the ActiveControl property is good if you use TAB to change between Controls. However, if the user left-clicks on another control, the ActiveControl will not be updated before LostFocus is triggered.

On Thu, 07 Jul 2005 14:48:26 +0200, cody <de********@gmx.de> wrote:
better would be to use the ActiveControl property of
System.Windows.Forms.Form class.

"Morten Wennevik" <Mo************@hotmail.com> schrieb im Newsbeitrag
news:op.stjcfbvjklbvpo@stone...
Hi Gidi,

I believe you will need to traverse all the controls to see which one has

the Focused property set.

private void textBox_LostFocus(object sender, EventArgs e)
{
foreach(Control c in this.Controls)
{
if(c.Focused)
label1.Text = c.Name;
}
}

Of course, if you got any container controls with child controls inside

you will need to do it recursively or, when creating the Form, add all
'focusable' controls to a list of your own.


On Thu, 07 Jul 2005 10:18:04 +0200, Gidi <sh*****@hotmail.com.dontspam>

wrote:
> Hi,
>
> Is it possible to know when TextBox loses focus to which object it lost it? > what i mean is when the LostFocus Event occur is it possible to know which > object got the focus instead?
>
> thanks,
>


--
Happy coding!
Morten Wennevik [C# MVP]



--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mike | last post: by
3 posts views Thread by - Steve - | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

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.