Control's, Focus, and Change focus

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
 
C

Cor Ligthert [MVP]

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
 
J

Jon Slaughter

Cor Ligthert said:
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
 
C

Cor Ligthert [MVP]

Jon,

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

Cor
 
M

Mick Doherty

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top