469,963 Members | 1,188 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

Custom Control questions

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 else on the parent is clicked? I
tried to override OnLostFocus and invalidate but that doesn't seem to do it.

2) the control, like i said is a label, how do i override the hotkey
behavior. I.e, if my text is &Help, when I do Alt-H on the parent, i want to
respond to the hotkey, but i can't quite figure it out.

Thanks

May 4 '07 #1
3 1988
On Fri, 04 May 2007 08:52:31 -0700, Nathan Laff <re******@hotmail.com>
wrote:
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 else on the parent is
clicked? I tried to override OnLostFocus and invalidate but that doesn't
seem to do it.
Are you actually getting focus when you get the OnClick event? And are
you calling the base methods in your overrides?

It seems to me that it makes more sense to override the OnGotFocus event
handler, just as you override the OnLostFocus event handler. Then you can
track when focus changes and redraw accordingly. If I recall, the base
Control class already handles basic focus-changing issues, so all you
really need is notification when focus changes.

Overriding OnClick, you may be drawing your focus rectangle without
actually having focus. Also, using the OnClick method to deal with focus
changes leaves you open to missing focus changes for other reasons (tab
key, for example).

I find it a little odd that you want your label to even have the focus;
that's unusual behavior for a label. So that's something to think about
too. :)
2) the control, like i said is a label, how do i override the hotkey
behavior. I.e, if my text is &Help, when I do Alt-H on the parent, i
want to respond to the hotkey, but i can't quite figure it out.
Override the OnKeyPress event handler and if you get Alt-H, set the focus
to the next control in the tab order.

That said, since you're basically doing a custom label, you might want to
consider inheriting from the Label control and just overriding the
behavior you want to change (like redrawing and allowing focus on a
label), and you could automatically get things like the Text and
UseMnemonic properties already in a Label control.

Pete
May 4 '07 #2
Thanks. I'll try that.

I'm not inheriting from a Label because it's a transparent control overtop
of an image.
i'm drawing a focus rectangle around it, because it's like a clickable
dropdown menu. i don't want a button there :)

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 04 May 2007 08:52:31 -0700, Nathan Laff <re******@hotmail.com>
wrote:
>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 else on the parent is
clicked? I tried to override OnLostFocus and invalidate but that doesn't
seem to do it.

Are you actually getting focus when you get the OnClick event? And are
you calling the base methods in your overrides?

It seems to me that it makes more sense to override the OnGotFocus event
handler, just as you override the OnLostFocus event handler. Then you can
track when focus changes and redraw accordingly. If I recall, the base
Control class already handles basic focus-changing issues, so all you
really need is notification when focus changes.

Overriding OnClick, you may be drawing your focus rectangle without
actually having focus. Also, using the OnClick method to deal with focus
changes leaves you open to missing focus changes for other reasons (tab
key, for example).

I find it a little odd that you want your label to even have the focus;
that's unusual behavior for a label. So that's something to think about
too. :)
>2) the control, like i said is a label, how do i override the hotkey
behavior. I.e, if my text is &Help, when I do Alt-H on the parent, i
want to respond to the hotkey, but i can't quite figure it out.

Override the OnKeyPress event handler and if you get Alt-H, set the focus
to the next control in the tab order.

That said, since you're basically doing a custom label, you might want to
consider inheriting from the Label control and just overriding the
behavior you want to change (like redrawing and allowing focus on a
label), and you could automatically get things like the Text and
UseMnemonic properties already in a Label control.

Pete
May 4 '07 #3
On Fri, 04 May 2007 10:54:04 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
[...]
>2) the control, like i said is a label, how do i override the hotkey
behavior. I.e, if my text is &Help, when I do Alt-H on the parent, i
want to respond to the hotkey, but i can't quite figure it out.

Override the OnKeyPress event handler and if you get Alt-H, set the
focus to the next control in the tab order.
I'm sorry...I'm a little tired today, and that answer was wrong in at
least two ways.

First, you won't get key events unless you have focus. The correct place
to handle the event is in the parent form, not the control itself.
Second, you can't get "Alt" information in the KeyPress event. What you
really want is the KeyDown event.

Other than those rather significant errors, I think the answer is fine. :)

Pete
May 4 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Bill Merrill | last post: by
19 posts views Thread by Jamey Shuemaker | last post: by
3 posts views Thread by shapper | last post: by
2 posts views Thread by =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= | last post: by
4 posts views Thread by =?Utf-8?B?UmljaEI=?= | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.