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

G

Gugale at Lincoln

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
 
P

Peter Duniho

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
 
G

Gugale at Lincoln

Thanks Peter!

Peter Duniho said:
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
 

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

Top