maddo7
maddo7

Reputation: 4963

C# Access Method of Form1 on Form2

I have 2 Forms in my project. Form1 is the main Form. There I have a button to open Form2, a ListView and a method to call a url and feed the ListView with data it gets from the url.

Form2 has a textbox for the url and a button "Ok".

When I press the button on Form1, it opens Form2, no problem. How can I manage it to pass the url to the method in Form1 if I enter a url into the textbox of Form2 and press Ok?

if I do

Form1 form1 = new Form1();
form1.method();

it will obviously open a new instance of Form1, execute the method there and fill the listbox there but not on my original Form1.

I found various solutions to the problem online but either they were too complicated or they didn't work.

How can I put it that it actually executes the method on the Form1 that I already have and therefore load the correct listbox with the values?

Many thanks

Upvotes: 0

Views: 3611

Answers (4)

MGE
MGE

Reputation: 912

Have a look:

//In Form1 opening Form2
Form2 frm = new Form2();
frm.Owner = this;
frm.Show();

//Example to call methods in FORM1 from FORM2
private void button1_Click(object sender, EventArgs e)
{
    Form1 frmParent = (Form1)this.Owner;
    frmParent.testeFunction();
    frmParent.InsertInGrid(textBox1.Text);
}

So, basically you need to create one function in Form1 to call from Form2 (passing Parameters). I hope this helps.

Upvotes: 1

tozlu
tozlu

Reputation: 4865

It creates a new instance because you are calling new.

You can loop over Application.OpenForms and check form names. After finding Form1, you can easily call its public method;

Form1(Application.OpenForms[0] as Form1).method();

Upvotes: -1

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112259

Define a property on Form2 to allow the access of the result

// In Form2
public string Url { get { return urlTextBox.Text; } }

In Form1

var form2 = new Form2();
form2.ShowDialog(this);
string url = form2.Url;

Note: unless Show() the ShowDialog() method waits until form2 is closed.

The ShowDialog argument this is the owner of the form to be opened. It binds form2 to form1 and makes it always appear on top of form1.

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

Define event on Form2 and raise it when url entered:

public class Form2 : Form
{
   public event EventHandler UrlEntered;

   private void ButtonOK_Click(object sender, EventArgs e)
   {
       if (UrlEntered != null)
           UrlEntered(this, EventArgs.Empty);
   }

   public string Url { get { return urlTextBox.Text; } }
}

Subscribe to that event on Form1:

Form2 form2 = new Form2()
form2.UrlEntered += Form2_UrlEntered;
form2.Show();

Handle this event:

private void Form2_UrlEntered(object sender, EventArgs e)
{
   Form2 form2 = (Form2)sender;
   string url = form2.Url;
   // use it
}

Also you can define event of type EventHandler<UrlEnteredEventArgs> with custom event argument, which will provide entered url to subscribers.

Upvotes: 4

Related Questions