By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,980 Members | 937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,980 IT Pros & Developers. It's quick & easy.

How to create user control which also acts like a radio button?

P: n/a
Hi,

I am creating a user control which has a radio button and few other
controls. I would like the user control to behave like a radio button, in
the sense when added to a container radio button on only one of the user
controls should be selected at any given point of time. I am going to use
this control in different places. What is the best way of designing such a
user control?

Thanks
SG
Jun 15 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Fri, 15 Jun 2007 12:14:07 -0700, Gugale at Lincoln
<gu**************@gmail.comwrote:
I am creating a user control which has a radio button and few other
controls. I would like the user control to behave like a radio button, in
the sense when added to a container radio button on only one of the user
controls should be selected at any given point of time. I am going to use
this control in different places. What is the best way of designing such
a
user control?
Well, it seems to me that one way would be to derive from the RadioButton
control and just override the OnPaint() method. I don't know if there's a
way to inherit RadioButton but disable its own drawing while still
allowing its OnPaint() method to be called. If there is, that's the way
to go, overriding the method yourself so that you can draw the control
however you like, while still calling the base method to ensure event
handlers are called. If I had to guess, I'd guess that it could be
disabled by setting the appropriate style for the control
(Control.SetStyle(ControlStyles.UserPaint)).

If there's not a way to do that, then you need an alternative. If
performance isn't critical (and it's probably not, in a GUI) you could
just set the control to use double-buffering and draw on top of whatever
the base control draws (using double-buffering will avoid the user seeing
the base control's image first followed by your own). If you want to
eliminate the base control from drawing altogether, then you have to not
call the base OnPaint() method, which _might_ be okay as long as you make
sure to call the event handlers yourself (in case anyone adds a handler to
the Paint event).

Of course, you can always code your own control from scratch. It
shouldn't be that hard: just make sure you override the OnFocus() and
OnClick() methods and when those happen, the instance for those methods
would select itself while unselecting all same-typed instances contained
by the same container containing that instance. There might be a few
subtle things you need to double-check to make sure you get right, which
would be the one reason I'd suggest looking into just overriding the
painting behavior of the existing RadioButton control class. But
practically speaking, I doubt doing it from scratch would be all that
hard, and might actually easier, depending on what you're actually able to
do with respect to disabling the base control's painting (see above).

Pete
Jun 15 '07 #2

P: n/a
Thanks Peter!

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 15 Jun 2007 12:14:07 -0700, Gugale at Lincoln
<gu**************@gmail.comwrote:
>I am creating a user control which has a radio button and few other
controls. I would like the user control to behave like a radio button, in
the sense when added to a container radio button on only one of the user
controls should be selected at any given point of time. I am going to use
this control in different places. What is the best way of designing such
a
user control?

Well, it seems to me that one way would be to derive from the RadioButton
control and just override the OnPaint() method. I don't know if there's a
way to inherit RadioButton but disable its own drawing while still
allowing its OnPaint() method to be called. If there is, that's the way
to go, overriding the method yourself so that you can draw the control
however you like, while still calling the base method to ensure event
handlers are called. If I had to guess, I'd guess that it could be
disabled by setting the appropriate style for the control
(Control.SetStyle(ControlStyles.UserPaint)).

If there's not a way to do that, then you need an alternative. If
performance isn't critical (and it's probably not, in a GUI) you could
just set the control to use double-buffering and draw on top of whatever
the base control draws (using double-buffering will avoid the user seeing
the base control's image first followed by your own). If you want to
eliminate the base control from drawing altogether, then you have to not
call the base OnPaint() method, which _might_ be okay as long as you make
sure to call the event handlers yourself (in case anyone adds a handler to
the Paint event).

Of course, you can always code your own control from scratch. It
shouldn't be that hard: just make sure you override the OnFocus() and
OnClick() methods and when those happen, the instance for those methods
would select itself while unselecting all same-typed instances contained
by the same container containing that instance. There might be a few
subtle things you need to double-check to make sure you get right, which
would be the one reason I'd suggest looking into just overriding the
painting behavior of the existing RadioButton control class. But
practically speaking, I doubt doing it from scratch would be all that
hard, and might actually easier, depending on what you're actually able to
do with respect to disabling the base control's painting (see above).

Pete

Jun 15 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.