J
Jon Slaughter
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.Control_Removed);
this.ControlAdded += new
System.Windows.Forms.ControlEventHandler(this.Control_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(LastFocusedControl))
{
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
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.Control_Removed);
this.ControlAdded += new
System.Windows.Forms.ControlEventHandler(this.Control_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(LastFocusedControl))
{
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