Re: Problem optaining focus when calling select in UserControl
- From: "daniel" <none@xxxxxxxx>
- Date: Thu, 24 Nov 2005 17:52:36 +0100
You just gave me the hint for the solution!
I went on the feedback page you mentioned and found an article:
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=50bca15b-ad19-435c-abe6-62ff00ec0cfc
Somebody describes a focus-bug when using Windows Classic style.
So I called "Application.EnableVisualStyles();" in my main().
This was the hint! If you use XP-style Controls, everything works as
expected :-)
Incredible! Thanks again.
Check it out yourself, I'd like to know, if you get to the same conclusion.
"Jelle van der Beek" <JellevanderBeek@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:6D6626CE-15C4-439F-9882-43E3B8E84463@xxxxxxxxxxxxxxxx
> Interesting, I'll have to look into this some other time. If you're
> confident
> that this is a bug, which I'm not quite sure of, then you can file a bug
> using product support:
>
> http://lab.msdn.microsoft.com/productfeedback/
>
> I also filed a couple of bugs there...
>
> It seems to be the only way to get in contact with the product teams.
> Unfortunately I never see Microsoft employees post in the newsgroups...
>
>
> "daniel" wrote:
>
>> Hi Jelle,
>> thanks again! Just great, what you figured out! I myself have made a few
>> tests. There's a tool in codeproject which helps quite a bit and which I
>> often use for such puposes: ".net Control Inspector" ->
>> http://www.codeproject.com/csharp/controlinspector.asp
>>
>> Using the tool, I made the scenario, where I called SetFocus() to my
>> UserControl (call it A) in the GotFocus-event of a TextBox (B).
>>
>> So it looks something like that:
>> class Form1
>> {
>> MyUserControl ctrlA;
>> TextBox txtB
>>
>> _txtB.GotFocus += new EventHandler(_txtB_GotFocus);
>>
>> void _txtB_GotFocus(object sender, EventArgs e)
>> {
>> ctrlA.SetFocus(); //I'd like to call here ctrlA.Select()
>> }
>> }
>>
>> Just for the background: I wan't to dynamically reorder the Taborder
>> depending on circumstances..
>>
>> So what happens is quite strange: As expected the UserControl gets Enter,
>> then it's inner TextBox gets the Enter ...
>> UserControl loses Focus, the inner Textbox gets GotFocus - and also txtB
>> gets GotFocus!!
>> So at the end we see ctrlA really having the visible focus, while
>> technically both controls have it, because they both
>> got a GotFocus, while nobody in between gets a LostFocus.
>>
>> I guess, it really a bug and they messed it up with the eventhandling
>> again
>> :-(
>>
>> Is there any other way how to find out? I think about calling in a TSC.
>>
>> Have a nice evening and thanks again for your profound research on this
>> matter!
>> Daniel
>>
>>
>>
>> "Jelle van der Beek" <JellevanderBeek@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
>> message news:B9787EEA-2B9E-4422-8081-E877AFDE5EC2@xxxxxxxxxxxxxxxx
>> > Hi Daniel,
>> >
>> > I haved digged a lot deeper now and I am starting to wonder if this is
>> > not
>> > a
>> > bug anyhow. I did not find an alternative solution than just simply
>> > overriding the Select method and using SelectNextControl to handle the
>> > selection yourself. I can tell you what I found out so far. I hope
>> > someone
>> > else may shed some more light on it.
>> >
>> > Here goes:
>> >
>> > The difference between Focus and Select seems that Focus is a more
>> > low-level
>> > method that directly uses Windows functions to send the WM_FOCUS
>> > message
>> > to a
>> > control. This will not always work, for instance in the Load function,
>> > since
>> > CanFocus will return false there. CanFocus directly checks the
>> > IsWindowVisible and IsWindowEnabled functions, while CanSelect checks
>> > the
>> > Control.Enabled and Control.Visible properties, which do return true in
>> > the
>> > OnLoad method. Selecting and Focusing internally will both use the
>> > WM_FOCUS
>> > messages. I suggest to read Jeffrey Tan's response on 'Giving a textbox
>> > the
>> > focus', here in this newsgroup.
>> >
>> > The Control class uses a selection scheme which tries to find the
>> > containing
>> > control (a control that implements IContainerControl) in the parenting
>> > chain.
>> > The ActiveControl property is set to the containercontrol.
>> > ContainerControl.ActiveControl will eventually set the focus.
>> >
>> > It seem that Select is a more robust mechanism, since it does not
>> > depend
>> > on
>> > the WS_VISIBLE style that must be set on a Window. That makes me wonder
>> > why
>> > focusing is exposed at all, but it will probably have some purpose.
>> >
>> > The reason why I think that your specific problem might be a bug, is
>> > that
>> > I
>> > found something odd while testing:
>> > Upon calling Select on the usercontrol, the WM_FOCUS message is not
>> > retrieved (I captured WM_FOCUS in the usercontrol). However, when I
>> > give
>> > another program the focus, and switch back to my testapp, the WM_FOCUS
>> > is
>> > suddenly send and my textbox gets the focus! I think that's fairly odd!
>> >
>> >
>> >
>> > "daniel" wrote:
>> >
>> >> Hi Jelle,
>> >> thanks a lot for the fast reply. Yes, the behaviour is odd and i don't
>> >> know,
>> >> if I just missed something or if it's really a bug.
>> >> I know that with setting the focus with Focus() it's working. But as
>> >> far
>> >> as
>> >> I know you're not supposed to do that and should use Select() instead.
>> >> We
>> >> had a lot of problems setting the focus manually between controls and
>> >> it
>> >> ended in a "message mess".
>> >> A bug is possible. In framework 1.0 there were a lot of bugs in this
>> >> area.
>> >> But I'm not sure, if I just don't know a switch and everything works
>> >> fine
>> >> (like the property CanSelect or something similar). As I mentioned,
>> >> Panel
>> >> works just fine. It inherits from another branch, though.
>> >> If you have the connections to find out more, I'd really appreciate it
>> >> a
>> >> lot!
>> >> Anyway, thanks for your help so far.
>> >>
>> >> Daniel
>> >>
>> >>
>> >>
>> >> "Jelle van der Beek" <JellevanderBeek@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote
>> >> in
>> >> message news:9A6F8DE0-A4FB-46FC-A8C2-29409F8A7635@xxxxxxxxxxxxxxxx
>> >> >I actually know several ways how to solve your problem, but as I was
>> >> >testing
>> >> > it, I had the feeling this wasn't quite how it is really supposed to
>> >> > work...
>> >> > I'm going to test a bit further. Anyway, here's already a few
>> >> > workarounds:
>> >> >
>> >> > 1. Calling Focus instead of Select actually selects the first child
>> >> > control
>> >> > on the usercontrol. I saw that the UserControl captures the WM_FOCUS
>> >> > message
>> >> > and cycles around the child controls. I am really curious what the
>> >> > difference
>> >> > is between focusing and selecting.
>> >> >
>> >> > 2. You can override the Select method in your user control to
>> >> > redirect
>> >> > selection to a child control using Control.Select or by using
>> >> > Control.SelectNextControl.
>> >> >
>> >> >
>> >> >
>> >> >
>> >> >
>> >> > "daniel" wrote:
>> >> >
>> >> >> Hi all,
>> >> >> I have a simple scenario:
>> >> >> I have a UserControl (control1) which hosts a TextBox. When I call
>> >> >> "control1.Select()", I expect to get the focus in the hosted
>> >> >> TextBox.
>> >> >> But
>> >> >> it
>> >> >> doesn't work - the only thing I can see, is that the UserControl
>> >> >> gets
>> >> >> a
>> >> >> Enter-even.
>> >> >> If I host a Textbox in a Panel and then select the panel,
>> >> >> everything
>> >> >> works
>> >> >> as expected.
>> >> >>
>> >> >> What do I have to do, so that my UserControl shows the correct
>> >> >> behaviour?
>> >> >> Anybody has a hint?
>> >> >>
>> >> >> Thanks in advance, I'm desperate :-(
>> >> >>
>> >> >> Daniel
>> >> >>
>> >> >> PS: I'm using .net framework 2.0, but I guess we have the same
>> >> >> behaviour
>> >> >> in
>> >> >> ..net 1.1
>> >> >>
>> >> >>
>> >> >>
>> >>
>> >>
>> >>
>>
>>
>>
.
- References:
- Problem optaining focus when calling select in UserControl
- From: daniel
- Re: Problem optaining focus when calling select in UserControl
- From: daniel
- Re: Problem optaining focus when calling select in UserControl
- From: Jelle van der Beek
- Re: Problem optaining focus when calling select in UserControl
- From: daniel
- Re: Problem optaining focus when calling select in UserControl
- From: Jelle van der Beek
- Problem optaining focus when calling select in UserControl
- Prev by Date: Re: Problem optaining focus when calling select in UserControl
- Next by Date: Re: Problem optaining focus when calling select in UserControl
- Previous by thread: Re: Problem optaining focus when calling select in UserControl
- Next by thread: Re: Problem optaining focus when calling select in UserControl
- Index(es):
Relevant Pages
|