Refresh a form after making edits on second [dupli

2019-03-06 17:06发布

This question already has an answer here:

hey everyone I am currently trying to refresh a form once changes are done on a second. On my first form I press a button "create" that will open another form, form2. This second form will have input fields and allows you to input values that populate comboboxes on the first form. On the second form there is a button "update" I would like the fist form to refresh once update is pressed on the first.

I know there is this.refresh();, but I'm not sure if this is useful for me. I am trying to something along the lines of:

On form2 -

Private void Form2UpdateButton_Click
{
  //do update stuff
  Form1_load.Refresh();
}

or maybe

private void Form2UpdateButton_Click
    {
    //do update stuff
    Form1.close();
    Form1.Open();
    }

I am still pretty new to C# and interacting 2 forms together is a rather complex concept to me so please let me know if I am going about this the wrong way. My refresh may be in the wrong spot, but I think this is what I want.

标签: c# forms refresh
3条回答
Emotional °昔
2楼-- · 2019-03-06 17:29

Recommendations:

Your second form should be created with a reference to first one, ie,

Form1:

public void RefreshParameters()
{
  // ... do your update magic here
}

private void openForm2(..)
{
   // Pass your current form instance (this) to new form2
   var aForm = new Form2(this);
   aForm.Show(); // show, run, I don't remember... you have it
}

Form2:

// Here you will store a reference to your form1
form1 daddy = null;

// A new constructor overloading default one to pass form1
public Fomr2(Form1 frm):base()
{
  daddy = frm; // Here you store a reference to form1!
}

public void UpdateDaddy()
{
  // And here you can call any public function on form1!
  frm.RefreshParameters();
}
查看更多
倾城 Initia
3楼-- · 2019-03-06 17:43

One way is to pass a reference of Form1 to Form2, like this:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void buttonLaunchForm_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.LauncherForm = this;

        form2.Show();
    }

    public void RefreshFormData()
    {
         // Refresh
    }
}

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public Form1 LauncherForm { set; get; }

    private void buttonUpdate_Click(object sender, EventArgs e)
    {
        // do your normal work then:

        LauncherForm.RefreshFormData();
    }
}

The above technique is called "Property-Injection";

查看更多
贼婆χ
4楼-- · 2019-03-06 17:44

Create an own event on form2 that triggers when the button gets clicked. This way you can just form2.OnUpdateClicked += yourMethod. Like this:

public partial class Form1 : Form
{
    private void CreateForm2()
    {
        Form2 frm2 = new Form2();
        // Hook the event of form2 to a method
        frm2.PropertyUpdated += Form2Updated;
    }

    private void Form2Updated(object sender, EventArgs e)
    {
        // this will be fired
    }
}

public partial class Form2 : Form
{
    // On form2 create an event
    public event EventHandler PropertyUpdated;

    private void Form2UpdateButton_Click()
    {
        // If not null (e.g. it is hooked somewhere -> raise the event
        if(PropertyUpdated != null)
            PropertyUpdated(this, null);
    }
}
查看更多
登录 后发表回答