-1

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.

asdfsa
  • 1
  • You could write a listener on your form1 to watch a specific event. – Adam Jul 19 '16 at 13:41
  • Could you please elaborate on what a listener is – asdfsa Jul 19 '16 at 13:46
  • would that listener watch for the update button to be pressed on form2 and that would allow me to do this.refesh on form1? But how would form1 watch the update button when the update button is only on form2? – asdfsa Jul 19 '16 at 13:48
  • Showed off a simple way. Are you in the need of passing a string or somethign like that? – C4d Jul 19 '16 at 13:49
  • As asked, your question is really too broad. But you will find a variety of useful answers at the marked duplicate to address your question. I can tell you for sure that the `Refresh()` method has _nothing_ to do with what you're trying to do, and is in fact a method that any well-written Winforms program will never use. If after reviewing the relevant documentation and existing answers on Stack Overflow you still need help, please post a new question. Be sure to include a good [mcve] and explain precisely what you've tried and what you need help with. See also [ask]. – Peter Duniho Jul 19 '16 at 14:26

3 Answers3

0

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);
    }
}
C4d
  • 3,183
  • 4
  • 29
  • 50
0

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();
}
Jorge Rojas
  • 485
  • 3
  • 10
0

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";

Zein Makki
  • 29,485
  • 6
  • 52
  • 63