Can't get focus away from combo box!

O

O.o

Hi all,

I am new to c#, and not a programmer by profession, so I'm sort of
bluffing my way through things at the moment. I have been tasked with
developing an app, and one of the main form controls is a combo box
that always has focus, even when other controls are in use. I tried
the Focus() function, but it only works from within the control. There
must be a way of making it so that when you, say, click another
control, or a blank area of the form, it kills focus to the combo box,
but I can't figure it out. The obvious question is: why doesn't this
happen automatically? But I'll settle for any band-aid solution...

Cheers,

O.o
 
J

Jeff Johnson

I am new to c#, and not a programmer by profession, so I'm sort of
bluffing my way through things at the moment. I have been tasked with
developing an app, and one of the main form controls is a combo box
that always has focus, even when other controls are in use. I tried
the Focus() function, but it only works from within the control. There
must be a way of making it so that when you, say, click another
control, or a blank area of the form, it kills focus to the combo box,
but I can't figure it out. The obvious question is: why doesn't this
happen automatically? But I'll settle for any band-aid solution...

It DOES happen automatically. There must be something going on that you're
not aware of which is explicitly setting focus to the combo box.

Just to make sure we're on the same page here, what kind (style) of combo
box is this? Simple, DropDown, or DropDownList? How do you know it has
focus? (I just want to be absolutely positive that you really know what
"focus" means, although it sounds like you do.)
 
O

O.o

It DOES happen automatically. There must be something going on that you're
not aware of which is explicitly setting focus to the combo box.

Just to make sure we're on the same page here, what kind (style) of combo
box is this? Simple, DropDown, or DropDownList? How do you know it has
focus? (I just want to be absolutely positive that you really know what
"focus" means, although it sounds like you do.)

Hi Jeff,

It is a DropDown combo box. I say it still has focus, because the text
is _always_ highlighted, and when I turn the mouse wheel, it scrolls
through the list values, *even while I am using another control or
navigating the menu strip* (unless a dialog opens, in which case,
focus immediately reverts to the combo box when the dialog closes).
For instance, I have a scroll bar. If I drag the scroll bar while
turning the mouse wheel, the values in both controls change
simultaneously.

D-'X
 
O

O.o

I also put a watch on the control's "ContainsFocus" flag, and it is
_always_ "true".

I should also add: the real issue is that the mouse wheel is supposed
to do a dynamic zoom and repaint the graphics area, an event which is
never triggered.
 
J

Jeff Johnson

It is a DropDown combo box. I say it still has focus, because the text
is _always_ highlighted, and when I turn the mouse wheel, it scrolls
through the list values, *even while I am using another control or
navigating the menu strip* (unless a dialog opens, in which case,
focus immediately reverts to the combo box when the dialog closes).
For instance, I have a scroll bar. If I drag the scroll bar while
turning the mouse wheel, the values in both controls change
simultaneously.

First test: create a new project and slap a combo box and a couple of other
controls on there. See if this new combo box exhibits the same behavior. If
not, there's something specific to the first project that's making the combo
behave that way. (Have you searched the code of the original project for
calls to Focus()?)

Also, is this defintely a run-of-the-mill combo box? When you select it in
the IDE and look at the Properties window, the drop-down combo should have
the control's name followed by its class name (you may need to widen the
tool window to see the whole name). Is it "System.Windows.Forms.ComboBox"?
 
O

O.o

First test: create a new project and slap a combo box and a couple of other
controls on there. See if this new combo box exhibits the same behavior. If
not, there's something specific to the first project that's making the combo
behave that way. (Have you searched the code of the original project for
calls to Focus()?)

It has the same behavior, unless the other control is also a combo or
list box. There are no calls to "Focus()" in any solution files.
Is it "System.Windows.Forms.ComboBox"?

Yes.
 
T

Tom P.

It has the same behavior, unless the other control is also a combo or
list box. There are no calls to "Focus()" in any solution files.


Yes.

My first question is what kind of controls are you sending the focus
to? A pictureBox can't accept Focus. Neither can a label or the Form.
Put a button on your form (it doesn't have to do anything, this is
just a test) and now see if you can click on the button and loose
focus from the combobox.

It may simply be that nothing else on the form can accept focus.

Tom P.
 
J

Jeff Johnson

My first question is what kind of controls are you sending the focus
to? A pictureBox can't accept Focus. Neither can a label or the Form.
Put a button on your form (it doesn't have to do anything, this is
just a test) and now see if you can click on the button and loose
focus from the combobox.
It may simply be that nothing else on the form can accept focus.

Yup, that's exactly where I was going next. I should have been more specific
when I said "a couple of other controls" above. My bad....

To go back to part of the original question which I glossed over:
There must be a way of making it so that when you, say, click another
control, or a blank area of the form, it kills focus to the combo box,
but I can't figure it out.

As far as "the blank area of the form" is concerned, no, that will NOT
remove the focus from a control. You may be used to the way browsers work,
because if you tab around and start highlightling links and then you click a
"non-hot" area of the page, the focus rectangle WILL disappear (or a combo
box will get unfocused), but it doesn't work that way in WinForms.
 
O

O.o

My first question is what kind of controls are you sending the focus
to? A pictureBox can't accept Focus. Neither can a label or the Form.
Put a button on your form (it doesn't have to do anything, this is
just a test) and now see if you can click on the button and loose
focus from the combobox.

It may simply be that nothing else on the form can accept focus.

Tom P.

OK, so a button or another list/text/combo box will steal the focus.
The scrollbar will too, but only if I do so explicitly, which seems
very odd. Why do I have to set the focus with one of its own events,
which I presume can only be triggered if it already has focus?

Anyway, the behavior I would like is: when the mouse is not
manipulating either of those controls (say, if it's not hovering over
them, or if the main form or some other non-control area has been
clicked), the mousewheel controls the zoom function. There has to be a
way of overriding the focus, otherwise, I'd have to draw my graphics
on a giant button, or something equally stupid.
 
T

Tom P.

OK, so a button or another list/text/combo box will steal the focus.
The scrollbar will too, but only if I do so explicitly, which seems
very odd. Why do I have to set the focus with one of its own events,
which I presume can only be triggered if it already has focus?

Anyway, the behavior I would like is: when the mouse is not
manipulating either of those controls (say, if it's not hovering over
them, or if the main form or some other non-control area has been
clicked), the mousewheel controls the zoom function. There has to be a
way of overriding the focus, otherwise, I'd have to draw my graphics
on a giant button, or something equally stupid.

It's not about stealing the focus, it's about receiving the focus.
There are controls that are meant to have focus because a user can do
something to them. Other controls don't get focus because normally the
user doesn't do anything to them. Think of it like this, the user has
to tell the program what they want to scroll with the mouse wheel.
They can do so much with it that they need to tell us what they need.

With this in mind, might I suggest placing a scroll bar, or maybe an
NumericUpDown control on your form in such a place as to reflect that
this is the "Zoom Level" and then let it control the zoom.

I can tell you, as someone that has done graphic zooms before, the
alternative is code that requires knowledge of overriding and message
capture and it's not something you want to get into on the first time
out. But if you feel up to it I will help as much as I can.

Tom P.
 
J

Jeff Johnson

OK, so a button or another list/text/combo box will steal the focus.
The scrollbar will too, but only if I do so explicitly, which seems
very odd. Why do I have to set the focus with one of its own events,
which I presume can only be triggered if it already has focus?

Your terminology is a bit screwy. You don't set focus with an EVENT, you set
it with a METHOD: Focus(). Events are things that happen outside of your
sphere of control. Methods are thing you execute under your sphere of
control.

Here's the whole concept for focus: it exists for the purpose of the
KEYBOARD. That's it. If everything were done with a mouse there would be no
need for focus, because the mouse can go anywhere and do anything. But when
you press a key, Windows has to know who the recipient of that keystroke is,
and its solution is that the recipient is the object that has "focus."

There's a reason you have to explicitly set the focus to a scroll bar, and
I'll explain. (Much of this is subjective on my part.) Scroll bars natively
have the ability to accept focus. When they get focus, the scroll thumb (aka
scroll box) blinks. BLINKS!! Blinking is a heinous thing. Most people
despise it. But programmers who were either lazy, uninformed, or didn't give
a damn didn't go out of their way to make sure the scroll bar didn't get
focus, and we were left with lots of programs with blinky scroll bars. So MS
made scroll bars not get focus by default in later versions programming
languages.
Anyway, the behavior I would like is: when the mouse is not
manipulating either of those controls (say, if it's not hovering over
them, or if the main form or some other non-control area has been
clicked), the mousewheel controls the zoom function. There has to be a
way of overriding the focus, otherwise, I'd have to draw my graphics
on a giant button, or something equally stupid.

The Control class (and therefore everything derived from it) has an
OnMouseWheel() event raiser method. Create a class derived from ComboBox and
override this method. In it, decide whether to pass on the wheel event to
the base combo box [by calling base.OnMouseWheel(e)] or do your zooming. I
can't guarantee it'll work, but it should, since that's the very reason for
providing protected Onxxx() event raisers in the first place.
 
O

O.o

Yeah. OK. I put in a slider control, and it works passably now. Thanks
to you both for the help, and have a great weekend.

Cheers,

Steve
 

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

Similar Threads


Top