473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 4273
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.