When the AcceptChange button of Form2 is pressed, the value of the corresponding column in ListBox in Form1 needs to be modified. Therefore, you can consider passing the ListBox control in Form1 into Form2 at the same time. All modification work is completed in Form2. According to this idea, the code of Form2 is as follows:
publicpartial class Form2 : Form
{
private string text;
private ListBox lb;
private int index;
//The constructor receives three parameters: select line text, ListBox control, select line index
public Form2(string text,ListBox lb,int index)
{
= text;
= lb;
= index;
InitializeComponent();
this. = text;
}
private void btnChange_Click(object sender, EventArgs e)
{
string text = this.;
(index);
(index, text);
();
}
}
This is what I wrote when new form 2 in Form1:
public partial class Form1 :Form
{
int index = 0;
string text = null;
public Form1()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgse)
{
if (this. != null)
{
text = this.();
index = this.;
//Construct Form2 to pass parameters at the same time
Form2 form2 = new Form2(text, listBox1, index);
();
}
}
OK, the benefits of doing this are intuitive, and you can pass whatever you need. The disadvantages are also obvious. If there are 100 controls that need to be modified in Form 1, will you pass 100 parameters in the construction? Besides, if other forms still need to be popped Form2, Form2 will be useless and can only be used for Form1, unless the overloaded constructor is written, it is not conducive to code reuse.