首页 > 解决方案 > 如何以结构化格式显示数组?

问题描述

我是 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;
        }
    `

标签: c#winforms

解决方案


这是我使用的代码片段。

将您的值加载到您希望文本框所在的表单中DataTables 并添加一个。tableLayoutPanel

调用SetTextboxes函数datatable(或者你可以在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);
    }
}

推荐阅读