【问题标题】:How to display the array in structured format?如何以结构化格式显示数组?
【发布时间】:2019-05-22 06:00:47
【问题描述】:

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

//这里在列表框中点击了一个item,那么文本框中的item就可以存储在listbox selected index中了

    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


    【解决方案1】:

    您可以为数组使用for 循环。

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

    【讨论】:

      【解决方案2】:

      这是我使用的代码中的 sn-p。

      将您的值加载到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);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-14
        • 1970-01-01
        • 1970-01-01
        • 2016-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-26
        相关资源
        最近更新 更多