Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Can someone help me annotate this code, I don't fully understand it

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 796
    Answer it

    This was the only piece of code that I could find to clear all the radio buttons in my form but I don't know how to explain it. I've only been learning basic C# and can't find a clear explanation for the code or how it works.

    public IEnumerable<Control> GetAll(Control control, Type type) 
            {
                var controls = control.Controls.Cast<Control>(); 
                return controls.SelectMany(ctrls => GetAll(ctrls, type)).Concat(controls).Where(c => c.GetType() == type); 
            }
    private void Reset&#95;button&#95;Click(object sender, EventArgs e)
            {
                if (MessageBox.Show("Are you sure you want to reset the test?", "Reset?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    progressBar1.Increment(-100);
                    student&#95;name&#95;txtbox.Clear(); 
    
                    var cntls = GetAll(this, typeof(RadioButton));  
                    foreach (Control cntrl in cntls) 
                    {
                        RadioButton rbtn = (RadioButton)cntrl; 
                        if (rbtn.Checked) 
                        {
                            rbtn.Checked = false; 
                        }
                    }
                }
            }
    

 1 Answer(s)

  • Hi beccahalford,

    As far as the functionality of this code is concerned, it is a code snippet from some kind of online test engine. The code written is for resetting the answer radio buttons to blank if the test taker wants to.

    public IEnumerable GetAll(Control control, Type type)
    {
    var controls = control.Controls.Cast();
    return controls.SelectMany(ctrls => GetAll(ctrls, type)).Concat(controls).Where(c => c.GetType() == type);
    }
    

    Above is the method which gets the control type, in this particular case-The radio buttons.

    Now comes the click method which is bound to execute on a "Reset" button click.

        progressBar1.Increment(-100);//For progress bar running
        student_name_txtbox.Clear();//Some student name is cleared from textbox.Again I dont know the reference.
        var cntls = GetAll(this, typeof(RadioButton)); //Above described GetAll Method used to get all radio buttons by type. Consider the "typeof" syntax.
        foreach (Control cntrl in cntls)
        {
        RadioButton rbtn = (RadioButton)cntrl;
        if (rbtn.Checked)
        {
        rbtn.Checked = false;
        }
        }
    

    Above is the foeach loop for clearing the checks of radio buttons, and that's all.

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: