C#,如何将命令行放入数组中?

时间:2018-01-11 18:09:23

标签: c# arrays richtextbox

我有两组richTextBox,每组有150个richTextBox,我用来将输出发送到richTextBox,如下所示:

richTextBox0A.Text = Buffer[0].ToString("X2"); //Show Hex value
richTextBox0B.Text = Convert.ToString(Buffer[0], 2).PadLeft(8, '0');//Show binary value

richTextBox1A.Text = Buffer[1].ToString("X2");
richTextBox1B.Text = Convert.ToString(Buffer[1], 2).PadLeft(8, '0');

richTextBox2A.Text = Buffer[2].ToString("X2");
richTextBox2B.Text = Convert.ToString(Buffer[2], 2).PadLeft(8, '0');
.
.
.
.
richTextBox149A.Text = Buffer[149].ToString("X2");
richTextBox149B.Text = Convert.ToString(Buffer[149], 2).PadLeft(8, '0');

是否可以像下面这样执行上述命令:

for (int i=0; i < 150; i++) {
    richTextBox0<i>A.Text = Buffer[i].ToString("X2");
    richTextBox1<i>B.Text = Convert.ToString(Buffer[i], 2).PadLeft(8,'0');
}

或者将它们放在数组中并循环数组内容?

1 个答案:

答案 0 :(得分:3)

for (int i=0; i < 150; i++)
{
    var rtbA = (this.Controls.Find("richTextBox" + i + "A", true).FirstOrDefault() as RichTextBox);
    var rtbB = (this.Controls.Find("richTextBox" + i + "B", true).FirstOrDefault() as RichTextBox);

    if (rtbA == null || rtbB == null)
    {
        continue;
    }

    rtbA.Text = Buffer[i].ToString("X2");
    rtbB.Text = Convert.ToString(Buffer[i], 2).PadLeft(8,'0');
}