2

I am kind of C# novice and I am trying to concatenate a string in C# to display the checked result in a textbox followed by a button click. I was able to get the desired output but the code seems it has not followed the DRY principle in SE.

private void button1_Click(object sender, EventArgs e)
        {
            String result1, result2="";

            if (radioButton1.Checked )
            {
                result1 = radioButton1.Text;
            }
            else if (radioButton2.Checked)
            {
                result1 = radioButton1.Text;
            }
            else if (radioButton3.Checked)
            {
                result1 = radioButton3.Text;
            }


            if (checkBox1.Checked)
            {
                result2 = checkBox1.Text;
            }
            if (checkBox2.Checked)
            {
                if (result2 != "")
                {
                    result2 = result2 + "," + checkBox2.Text;
                }
                else
                result2 = checkBox2.Text;
            }
            if (checkBox3.Checked)
            {
                if (result2 != "")
                {
                    result2 = result2 + "," + checkBox3.Text;
                }
                else
                result2 = checkBox3.Text;
            }

            textBox1.Text="You like to shop from "+ result1    
                           +"for clothing styles like "+result2;
       }

I am sure there should be a lot clever way of doing this and it would be highly appreciated if someone could provide me a better solution.

2
  • 1
    if there is no problem with your code and you want to optimize it, probably it will be good idea to post it on code review platform of stack overflow. codereview.stackexchange.com Commented May 16, 2015 at 12:07
  • 1
    I was not aware about that thanks for letting me know. Commented May 16, 2015 at 18:34

2 Answers 2

1

This could be written in a single line (if your checkboxes are all contained in the controls collection of the form

result2 = string.Join(",", this.Controls.OfType<CheckBox>()
                .Where(x => x.Checked)
                .Select(c => c.Text));
Sign up to request clarification or add additional context in comments.

2 Comments

This didn't work for me. For some reason result2 is getting assigned to an empty string all the time even though the check boxes are selected. I assume my checkboxes are all contained in the controls collection of the form.
Well, this is strange because I have tested it. Are you sure about the checkboxes? If they are inside a groupbox, a panel or a tabpage you need to use the controls collection of that control. IE this.groupBox1.Controls.OfType<CheckBox>()......
1

How about using String.Join and String.Format?

Something like

private void button1_Click(object sender, EventArgs e)
    {
        String result1, result2="";

        if (radioButton1.Checked )
        {
            result1 = radioButton1.Text;
        }
        else if (radioButton2.Checked)
        {
            result1 = radioButton1.Text;
        }
        else if (radioButton3.Checked)
        {
            result1 = radioButton3.Text;
        }


        List<string> choices = new List<string>();
        if (checkBox1.Checked)
        {
            choices.Add(checkBox1.Text);
        }
        if (checkBox2.Checked)
        {
            choices.Add(checkBox2.Text);
        }
        if (checkBox3.Checked)
        {
            choices.Add(checkBox3.Text);
        }

        textBox1.Text=String.Format("You like to shop from {0} for clothing styles like {1}", result1, String.Join(",",choices.ToArray()));
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.