Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7694333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:17:45+00:00 2026-05-31T21:17:45+00:00

Just learning C#, radiobuttons and checkboxes. No urgency. The code works to display the

  • 0

Just learning C#, radiobuttons and checkboxes. No urgency.
The code works to display the names of the checked controls but it doesn’t seem to be an elegant solution.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TVC
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "you clicked" + compute();
        }


        string compute()
        {
            string result = "";
            object o=label1;

            while (((Control)o).TabIndex!=7)
            {
                if ((o is RadioButton)||(o is CheckBox))
                {

                    if ((o is RadioButton)&&((RadioButton)o).Checked)

                    result += " "+((RadioButton)o).Text;

                    if ((o is CheckBox)&&((CheckBox)o).Checked)

                    result += " "+((CheckBox)o).Text;

                }

                o = GetNextControl((Control)o, true);
            }



            return result;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

The checboxes and radiobuttons tabindexes are counted from 1to 6, the label is 0 and the button is 7 so that GetNextControl works. Is there a nicer code that would work ?enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:17:46+00:00Added an answer on May 31, 2026 at 9:17 pm

    I just tested this and verified that it works. It uses recursion and the newer dynamic keyword as it appears that RadioButton and CheckBox inherit from ButtonBase, which does not have the Checked property, otherwise you could cast down to that. The dynamic allows me to avoid that since I already know the control types.

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = compute(this.Controls);
        }
        private string compute(Control.ControlCollection controls)
        {
            string result = String.Empty;
            foreach (Control control in controls)
            {
                if (control.Controls != null)
                    result += compute(control.Controls);
                if (control is RadioButton || control is CheckBox)
                {
                    dynamic checkControl = control;
                    if (checkControl.Checked)
                    {
                        result += checkControl.Text + ";";
                    }
                }
            }
            return result;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just learning about SVG, and it seems great but I'm not sure about
Im just learning the ropes with javascript and jQuery but cant get my head
I'm just learning about SproutCore now, seems great. But I can't find a good
Im just learning javascript and I'm just wondering why this doesn't work. I've created
I'm just learning codeigniter, and I seem to have run into something that is
Just learning php and looking into someone else's code. I'm not sure what is
btw i am just learning so bear with me ive got all my code
I'm just learning how to code for the iPhone using Objective-C, and I don't
just learning Java and I know this may sound stupid but I have to
Just learning Automator here, but I'm trying to run a convert command on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.