Questions between checkbox and textbox

José Carlos 866 Reputation points
2020-08-26T21:39:16.487+00:00

Good night friends.

I have 20 checkbox and 20 textbox. The idea is that when the user checks a checkbox, I need to put it in the textbox
corresponding the result of a routine. That is, if the user clicks on checkbox 2, I need to put the result
in textbox2. If I check checkbox 10, I need to put the result in textBox10.

I'm doing this:

            int numjogo;
            if (checkBox1.Checked == true)
            {
                numjogo = 1;
                textBox1.Text = subverificaacertos(numjogo).ToString();
                if (Convert.ToInt32(textBox1.Text) >= 11)
                {
                    textBox1.BackColor = Color.Blue;
                    textBox1.ForeColor = Color.Yellow;
                    cp.Add(numjogo);
                    tempremio = 1;
                }              
            }

            if (checkBox2.Checked == true)
            {
                numjogo = 2;
                textBox2.Text = subverificaacertos(numjogo).ToString();
                if (Convert.ToInt32(textBox2.Text) >= 11)
                {
                    textBox2.BackColor = Color.Blue;
                    textBox2.ForeColor = Color.Yellow;
                    cp.Add(numjogo);
                    tempremio = 1;
                }                
            }

I do this for each checkbox, which is 20.

I was trying to make the code below to replace the 20 codes above.

        int numjogo;
        foreach (Control c in panel16.Controls)
        {
            if (c is CheckBox && (c as CheckBox).Checked)
            {
                numjogo = Convert.ToInt32(c.Text);
                textBox?.Text = subverificaacertos(numjogo).ToString();
                if (Convert.ToInt32(textBox?.Text) >= 11)
                {
                    textBox?.BackColor = Color.Blue;
                    textBox?.ForeColor = Color.Yellow;
                    cp.Add(numjogo);
                    tempremio = 1;
                }
            }
        }

How to replace the textBox above with ? by the textbox corresponding to the clickbox clicked?
I thought of creating a list with the names of the 20 textBox. But I don't even know if that is possible.
And I also confess that I wouldn't know how to do it.

Thank you.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,811 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,514 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2020-08-27T05:29:16.297+00:00

    Hi José Carlos Souza,
    You can use Controls.Find() method with name property to search the corresponding textbox.
    Here is a code example you can refer to.

    int numjogo;
    foreach (Control c in panel1.Controls)
    {
        if (c is CheckBox && (c as CheckBox).Checked)
        {
            numjogo = Convert.ToInt32(c.Text);
            TextBox tb = (TextBox)Controls.Find("textBox" + numjogo, true)[0];
            tb.Text = numjogo.ToString();
            if (Convert.ToInt32(tb.Text) >= 5)
            {
                tb.BackColor = Color.Blue;
                tb.ForeColor = Color.Yellow;
    
            }
        }
    }
    

    Best Regards,
    Daniel Zhang


1 additional answer

Sort by: Most helpful
  1. José Carlos 866 Reputation points
    2020-08-27T12:41:41.473+00:00

    Perfect my friend.
    It worked. I saved 220 lines of code.
    Thank you very much.

    José Carlos

    0 comments No comments