Reputation: 11
I'm quite new to C#, and I'm making a form that needs to reload itself when I press a button, but with different inputs.
public partial class Edit_Desloc : Form
{
public Edit_Desloc(string id_desloc, string proj, string data, string horas)
{
....
}
}
I'm using this :
Edit_Desloc edit_desloc = new Edit_Desloc(list[0][0], list[4][0], list[1][0], list[3][0]);
edit_desloc.Show();
this.Close();
but it's not really good.
Can't I reload the form instead of close it and open again?
Upvotes: 1
Views: 195
Reputation: 125257
Refactor your code and create a LoadData
function having the same parameters that you have in constructor and move the logic to the constructor. Then call the method whenever you need.
Let's say you have the following code:
public partial class Edit_Desloc : Form
{
public Edit_Desloc(string id_desloc, string proj, string data, string horas)
{
InitializeComponent();
/* some other initialization based on parameters*/
}
}
Refactor it to the following:
public partial class Edit_Desloc : Form
{
public Edit_Desloc(string id_desloc, string proj, string data, string horas)
{
InitializeComponent();
LoadData(id_desloc, proj, data, horas);
}
public void LoadData(string id_desloc, string proj, string data, string horas)
{
/* some other initialization based on parameters*/
}
}
Then call LoadData
whenever you need by passing parameters.
Upvotes: 3