如何以结构化格式显示数组?

时间:2019-05-22 06:00:47

标签: c# winforms

我是C#编程的新手,我已经在c#(winforms)中进行了编码,以得到如下输出:如果单击列表框中的项目,则该项目应显示在文本框中,我已经对其进行了编码,有点忙于实施进一步。

public partial class Form1 : Form
{
    TextBox[] tb = new TextBox[5];
    TextBox[] t = new TextBox[5];
    TextBox[] t1 = new TextBox[5];
    int[] tblist = new int[5];


    public Form1()
    {
        InitializeComponent();
        tb[0] = new TextBox();
        tb[1] = new TextBox();
        tb[2] = new TextBox();
        tb[3] = new TextBox();
        tb[4] = new TextBox();
        t[0] = new TextBox();
        t[1] = new TextBox();
        t[2] = new TextBox();
        t[3] = new TextBox();
        t[4] = new TextBox();
        t1[0] = new TextBox();
        t1[1] = new TextBox();
        t1[2] = new TextBox();
        t1[3] = new TextBox();
        t1[4] = new TextBox();

    } //how can I simplify this by not assigning new to every textbox that i had created

//此按钮单击用于将项目保存在列表框中所选项目的文本框中 这里如何减少代码:列表框选择的索引不同,但是功能保持不变。

    private void button1_Click(object sender, EventArgs e) 
    {
        if (listBox1.SelectedIndex == 0)
        {
            tb[0].Text = textBox1.Text;
            tb[1].Text = textBox2.Text;
            tb[2].Text = textBox3.Text;
            tb[3].Text = textBox4.Text;
            tb[4].Text = textBox5.Text;
        }
        if (listBox1.SelectedIndex == 1)
        {
            t[0].Text = textBox1.Text;
            t[1].Text = textBox2.Text;
            t[2].Text = textBox3.Text;
            t[3].Text = textBox4.Text;
            t[4].Text = textBox5.Text;
        }
        if (listBox1.SelectedIndex == 2)
        {
            t1[0].Text = textBox1.Text;
            t1[1].Text = textBox2.Text;
            t1[2].Text = textBox3.Text;
            t1[3].Text = textBox4.Text;
            t1[4].Text = textBox5.Text;
        }
    }

///在此处单击列表框中的一个项目,这样文本框中的项目便可以存储在列表框中选定的索引中

    private void listBox1_Click(object sender, EventArgs e)
    {
        if (listBox1.SelectedIndex == 0)
        {

             textBox1.Text = tb[0].Text;
            textBox2.Text = tb[1].Text;
            textBox3.Text = tb[2].Text;
            textBox4.Text = tb[3].Text;
            textBox5.Text = tb[4].Text;
        }

       if (listBox1.SelectedIndex == 1)
        {   textBox1.Text = t[0].Text;
            textBox2.Text = t[1].Text;
            textBox3.Text = t[2].Text;
            textBox4.Text = t[3].Text;
            textBox5.Text = t[4].Text;
        }
        if (listBox1.SelectedIndex == 2)
        {
            textBox1.Text = t1[0].Text;
            textBox2.Text = t1[1].Text;
            textBox3.Text = t1[2].Text;
            textBox4.Text = t1[3].Text;
            textBox5.Text = t1[4].Text;
        }
    `

2 个答案:

答案 0 :(得分:0)

您可以为阵列使用for循环。

for(var i = 0; i < tb.Length; i++)
{
    tb[i] = new TextBox();
    t[i] = new TextBox();
    t1[i] = new TextBox();
}

答案 1 :(得分:0)

这是我使用的代码的片段。

将您的值加载到DataTables中 并将tableLayoutPanel添加到您希望文本框进入的表单中。

使用datatable调用 SetTextboxes 函数(或者您可以在此处发送list,只需更改参数并稍微循环一下即可。

这将很快将文本框动态添加到表单中。

class SurroundingClass
{
    private void SetTextboxes(datatable DT)
    {
        //Clear the previous textboxes
        pnlLayoutExpenses.Controls.clear();
       //loop through table and create new textboxes
        foreach (DataRow row in DT.Rows)
            formAddTextbox(row("dataTableColumnWhichHoldsTextboxText"));
    }



    private void formAddTextbox(string fieldname)
    {

        Integer elementCount = 0;

        TextBox txtYourField = new TextBox();
        txtYourField.Width = 100;
        txtYourField.Height = 20;
        //txtYourField.ReadOnly = true;
        txtYourField.Text = fieldname;
        txtYourField.tag = elementCount; 


        // Use tableLayoutPanel
        pnlLayoutExpenses.SetCellPosition(txtType, new TableLayoutPanelCellPosition(0, elementCount));

        pnlLayoutExpenses.Controls.Add(txtType);
    }
}