Click here to Skip to main content
15,905,874 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

I have 50+ radio button in Form [window application]. How do i get which radio button is checked on button click? I don't want to add individual CheckedChanged event for all radio button.
Sergey Alexandrovich Kryukov 16-Nov-11 0:51am    
Why? Do you think that you have to write 50 lines of code for that? :-)

1 solution

Of course, add the handler of the event CheckedChanged to all radio buttons. You need only one line of code to do this; and it should be only one method for all those radio buttons. Don't even play with the idea of doing it through designer, do it in code. Designer is for manual work, slow and unreliable, code is for writing real abstraction and reuse. Also, you should not write a switch of 50+ cases. Put all data it Tag of each instance of the control and use it to avoid checking what instance is it.

That said, you should not even use the designer for add 50+ controls. Is such cases, always do it in code, and get strings (texts for the control) from some data file which you can embed in the application as a .resx resource. Only in this case you can add an event handler(s) to each one in one line (loop). What, VB.NET? Sorry, some three-five lines no matter how many radio boxed do you use.

By the way, 50+ radio boxes is already too many for good UI design. Use something like just one ListBox instead; this will be even easier. And you will need only one instance of event and event handler. Win! :-)

Share this answer
NikulDarji 16-Nov-11 1:12am    
Nice Solution.....:) And agree with u he should use ListBox...
Sergey Alexandrovich Kryukov 16-Nov-11 4:25am    
Thank you for your understanding and voting, Nikul.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900