如何将文本框的数据保存到列表框中的选定项目?

时间:2019-06-03 06:12:37

标签: c# .net winforms

这里我创建了一个二维数组,文本框中的数据必须存储在列表框中的selecteditem中。它无法正常工作,请帮我解决这段代码

public partial class Form1 : Form
{
   public TextBox[] tb { get; private set; }
   public TextBox[] t { get; private set; }
   public TextBox[] t1 { get; private set; }
  // TextBox[][] textBoxes = { tb, t, t1 };

    public Form1()
    {
      InitializeComponent();
      TextBox[][] textBoxes = { tb, t, t1 };
      for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                 textBoxes[i][j] = new TextBox();
             }
        }
    }

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

   private void button1_Click_1(object sender, EventArgs e)
    {
        int index = listBox1.SelectedIndex;
        textBoxes [index][0].Text = textBox1.Text;
        textBoxes[index][1].Text = textBox2.Text;
        textBoxes[index][2].Text = textBox3.Text;
        textBoxes[index][3].Text = textBox4.Text;
        textBoxes[index][4].Text = textBox5.Text;

    }
}

1 个答案:

答案 0 :(得分:0)

使用此:

int pos = listBox1.Items.IndexOf(listBox1.SelectedItem.ToString());
if (pos != -1) listBox1.Items[pos] = textBox1.Text;