Я новичок в программировании на 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;
}
`