468,317 Members | 1,538 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,317 developers. It's quick & easy.

Control's, Focus, and Change focus

I've created some custom controls and forms that allow the feature to
temporarily transfer focus to a control that has been entered by the mouse.
Everything seems to work fine but the problem I have is that sometimes I
seem to loose the original "holder" of focus and when the user hits tab
while using "temporary" focus(while the mouse is over a control) it will
change focus to the next control but not update it.

So I have several issues. I could prevent tab(change of focus other than
mouse, I assume tab is the only other method to use this by the user except
possibly alt?) from being used while the mouse is being used on a control.
This, I think, would solve the main probem... but I'm not if this is the
best way.


Heres my code

public class CoolForm : System.Windows.Forms.Form
{
public System.Windows.Forms.Control LastFocusedControl;

public CoolForm()
{
this.ControlRemoved += new
System.Windows.Forms.ControlEventHandler(this.Cont rol_Removed);
this.ControlAdded += new
System.Windows.Forms.ControlEventHandler(this.Cont rol_Added);
}

public new CoolForm FindForm()
{
return (CoolForm)base.FindForm();
}

private void Control_Added(object sender,
System.Windows.Forms.ControlEventArgs e)
{
e.Control.LostFocus += new EventHandler(Control_LostFocus);
e.Control.MouseEnter += new EventHandler(Control_MouseEnter);
e.Control.MouseLeave += new EventHandler(Control_MouseLeave);
}

private void Control_Removed(object sender,
System.Windows.Forms.ControlEventArgs e)
{
e.Control.LostFocus -= new EventHandler(Control_LostFocus);
e.Control.MouseEnter -= new EventHandler(Control_MouseEnter);
e.Control.MouseLeave -= new EventHandler(Control_MouseLeave);
}

private void Control_LostFocus(object sender, System.EventArgs e)
{
if (((System.Windows.Forms.Control)sender).Equals(Las tFocusedControl))
{
LastFocusedControl = this.ActiveControl;
}
}

void Control_MouseLeave(object sender, EventArgs e)
{
if (LastFocusedControl != null)
LastFocusedControl.Focus();
}

void Control_MouseEnter(object sender, EventArgs e)
{
LastFocusedControl = this.ActiveControl;
((System.Windows.Forms.Control)sender).Focus();
}

}}

As you can see, its pretty simple and AFAIK it works except the form's
methodology of focus handling is unaware of this temporary change of
focus(i.e., the using tab or other methods except the mouse) and it causes
some problems. I'm not sure if I have to control those methods too(which is
hard since I don't know all the different means) or if I can somehow get
them to play along nicely with what I want.

Any Ideas?

Thanks,
Jon
Nov 19 '06 #1
4 4002
Jon,

Did you already use the events "Leave" and "Enter" the last one is often
confused as an Enter key, but it is the entrance of the control.

Cor

"Jon Slaughter" <Jo***********@Hotmail.comschreef in bericht
news:Mk*****************@newssvr14.news.prodigy.co m...
I've created some custom controls and forms that allow the feature to
temporarily transfer focus to a control that has been entered by the
mouse. Everything seems to work fine but the problem I have is that
sometimes I seem to loose the original "holder" of focus and when the user
hits tab while using "temporary" focus(while the mouse is over a control)
it will change focus to the next control but not update it.

So I have several issues. I could prevent tab(change of focus other than
mouse, I assume tab is the only other method to use this by the user
except possibly alt?) from being used while the mouse is being used on a
control. This, I think, would solve the main probem... but I'm not if this
is the best way.


Heres my code

public class CoolForm : System.Windows.Forms.Form
{
public System.Windows.Forms.Control LastFocusedControl;

public CoolForm()
{
this.ControlRemoved += new
System.Windows.Forms.ControlEventHandler(this.Cont rol_Removed);
this.ControlAdded += new
System.Windows.Forms.ControlEventHandler(this.Cont rol_Added);
}

public new CoolForm FindForm()
{
return (CoolForm)base.FindForm();
}

private void Control_Added(object sender,
System.Windows.Forms.ControlEventArgs e)
{
e.Control.LostFocus += new EventHandler(Control_LostFocus);
e.Control.MouseEnter += new EventHandler(Control_MouseEnter);
e.Control.MouseLeave += new EventHandler(Control_MouseLeave);
}

private void Control_Removed(object sender,
System.Windows.Forms.ControlEventArgs e)
{
e.Control.LostFocus -= new EventHandler(Control_LostFocus);
e.Control.MouseEnter -= new EventHandler(Control_MouseEnter);
e.Control.MouseLeave -= new EventHandler(Control_MouseLeave);
}

private void Control_LostFocus(object sender, System.EventArgs e)
{
if (((System.Windows.Forms.Control)sender).Equals(Las tFocusedControl))
{
LastFocusedControl = this.ActiveControl;
}
}

void Control_MouseLeave(object sender, EventArgs e)
{
if (LastFocusedControl != null)
LastFocusedControl.Focus();
}

void Control_MouseEnter(object sender, EventArgs e)
{
LastFocusedControl = this.ActiveControl;
((System.Windows.Forms.Control)sender).Focus();
}

}}

As you can see, its pretty simple and AFAIK it works except the form's
methodology of focus handling is unaware of this temporary change of
focus(i.e., the using tab or other methods except the mouse) and it causes
some problems. I'm not sure if I have to control those methods too(which
is hard since I don't know all the different means) or if I can somehow
get them to play along nicely with what I want.

Any Ideas?

Thanks,
Jon

Nov 19 '06 #2

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uA**************@TK2MSFTNGP02.phx.gbl...
Jon,

Did you already use the events "Leave" and "Enter" the last one is often
confused as an Enter key, but it is the entrance of the control.
I think I tried but the code was never called when I put breakpoints on it.

Only thing I can think of is to keep a "state" that represents when there is
temprorary focus going on. i.e., when the mouse enters a control I would set
it as true and when it left as false. I think this might work but haven't
try it yet. I'll go back and play with it some more and see what happens.

Thanks,
Jon
Nov 19 '06 #3
Jon,

A not unusual approach in my idea, especially if you try to get the mouse
clicks independent from the control.

Cor

"Jon Slaughter" <Jo***********@Hotmail.comschreef in bericht
news:8Q*******************@newssvr21.news.prodigy. com...
>
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uA**************@TK2MSFTNGP02.phx.gbl...
>Jon,

Did you already use the events "Leave" and "Enter" the last one is often
confused as an Enter key, but it is the entrance of the control.

I think I tried but the code was never called when I put breakpoints on
it.

Only thing I can think of is to keep a "state" that represents when there
is temprorary focus going on. i.e., when the mouse enters a control I
would set it as true and when it left as false. I think this might work
but haven't try it yet. I'll go back and play with it some more and see
what happens.

Thanks,
Jon

Nov 19 '06 #4
What behaviour do you want when Tabbing to the next Control?
If you simply want to keep the control under the mouse focused then simply
add to your existing LostFocus method:

private void Control_LostFocus(object sender, System.EventArgs e)
{
Control focusedControl = (Control)sender;
if (focusedControl.Equals(LastFocusedControl))
{
LastFocusedControl = this.ActiveControl;
}
if (focusedControl.Bounds.Contains(this.PointToClient (MousePosition)))
focusedControl.Focus();
}

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:Mk*****************@newssvr14.news.prodigy.co m...
I've created some custom controls and forms that allow the feature to
temporarily transfer focus to a control that has been entered by the
mouse. Everything seems to work fine but the problem I have is that
sometimes I seem to loose the original "holder" of focus and when the user
hits tab while using "temporary" focus(while the mouse is over a control)
it will change focus to the next control but not update it.

So I have several issues. I could prevent tab(change of focus other than
mouse, I assume tab is the only other method to use this by the user
except possibly alt?) from being used while the mouse is being used on a
control. This, I think, would solve the main probem... but I'm not if this
is the best way.


Heres my code

public class CoolForm : System.Windows.Forms.Form
{
public System.Windows.Forms.Control LastFocusedControl;

public CoolForm()
{
this.ControlRemoved += new
System.Windows.Forms.ControlEventHandler(this.Cont rol_Removed);
this.ControlAdded += new
System.Windows.Forms.ControlEventHandler(this.Cont rol_Added);
}

public new CoolForm FindForm()
{
return (CoolForm)base.FindForm();
}

private void Control_Added(object sender,
System.Windows.Forms.ControlEventArgs e)
{
e.Control.LostFocus += new EventHandler(Control_LostFocus);
e.Control.MouseEnter += new EventHandler(Control_MouseEnter);
e.Control.MouseLeave += new EventHandler(Control_MouseLeave);
}

private void Control_Removed(object sender,
System.Windows.Forms.ControlEventArgs e)
{
e.Control.LostFocus -= new EventHandler(Control_LostFocus);
e.Control.MouseEnter -= new EventHandler(Control_MouseEnter);
e.Control.MouseLeave -= new EventHandler(Control_MouseLeave);
}

private void Control_LostFocus(object sender, System.EventArgs e)
{
if (((System.Windows.Forms.Control)sender).Equals(Las tFocusedControl))
{
LastFocusedControl = this.ActiveControl;
}
}

void Control_MouseLeave(object sender, EventArgs e)
{
if (LastFocusedControl != null)
LastFocusedControl.Focus();
}

void Control_MouseEnter(object sender, EventArgs e)
{
LastFocusedControl = this.ActiveControl;
((System.Windows.Forms.Control)sender).Focus();
}

}}

As you can see, its pretty simple and AFAIK it works except the form's
methodology of focus handling is unaware of this temporary change of
focus(i.e., the using tab or other methods except the mouse) and it causes
some problems. I'm not sure if I have to control those methods too(which
is hard since I don't know all the different means) or if I can somehow
get them to play along nicely with what I want.

Any Ideas?

Thanks,
Jon

Nov 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Lyn | last post: by
2 posts views Thread by Elliot M. Rodriguez | last post: by
reply views Thread by MattB | last post: by
2 posts views Thread by Tom Shelton | last post: by
3 posts views Thread by Nathan Laff | last post: by
3 posts views Thread by kelvin.koogan | last post: by
2 posts views Thread by bobh | last post: by
1 post views Thread by howard w | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.