472,982 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 4237
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Lyn | last post by:
Hi, I have a text control on a form which is bound to table field StartDate which is in Date format. When updating the table record via the form, any data entered into the StartDate control is...
2
by: Elliot M. Rodriguez | last post by:
Is it possible to change a control's focus at runtime? I'm sure you can.... I have a form with 2 textbox controls that cause postbacks. They are located in the middle of my form. When a...
0
by: MattB | last post by:
I have a page with several user controls on it. Some of the UC's have multiple text boxes that get auto-populated via the OnTextChanged event. When this happens, I lose my focus (where the...
2
by: Tom Shelton | last post by:
I was goofing around tonight and decided to write a little IP address control. I had written a simple one a long time ago, but I decided I should try to write one that was half way correct :) ...
0
by: LCAdeveloper | last post by:
Another newbie question I'm afraid. When I use the .Focus() or .Select() methods to set the initial control that has focus on a form, try as I might I cannot get the control to visually indicate...
3
by: vivela | last post by:
hi, I hope you could help me out on this one,cause I have been trying for 4 days to solve it,but couldn't quite get it right: I have an ASP textbox that should change the value in a dropdownlist....
3
by: Nathan Laff | last post by:
I have a custom Control that is a label, however I inherit from Control... 1) I override onClick and do a DrawFocusRectangle, that works great. How do I clear the focus rectangle once something...
3
by: kelvin.koogan | last post by:
I have a number of controls on a tab page. I want to validate them all when the user tries to leave the tab. I then want to highlight the first control which fails validation. How can I do this? I...
2
by: bobh | last post by:
Hi All In AccessXP I have a tab control with 5 tabs and I went to requery a combobox if and only if the user is moving from one perticular tab. I have this in place right now (On Change event of...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.