Passing a Method to a second form
-
Hi all, I am having problems passing a method from form 1 to form 2. I have on form 1 a dgv and a button. When the form 1 button is clicked it takes me to form 2. On form 2 I have the same set up, a dgv and a button. However, the button on Form 2, when clicked should call a method from form 1 then closes form 2. When I run the windows form application I get no errors. The method on form 1 that I try to call from form 2 is public. Any help is much appreciated. Thank you! Code: Form 1 public void UpdateGridView() { // Method code } form 2 public void MapFormUpdate_Click(object sender, EventArgs e) { Form1 MainForm = new Form1(); MainForm.UpdateGridView(); this.Close(); }
-
Hi all, I am having problems passing a method from form 1 to form 2. I have on form 1 a dgv and a button. When the form 1 button is clicked it takes me to form 2. On form 2 I have the same set up, a dgv and a button. However, the button on Form 2, when clicked should call a method from form 1 then closes form 2. When I run the windows form application I get no errors. The method on form 1 that I try to call from form 2 is public. Any help is much appreciated. Thank you! Code: Form 1 public void UpdateGridView() { // Method code } form 2 public void MapFormUpdate_Click(object sender, EventArgs e) { Form1 MainForm = new Form1(); MainForm.UpdateGridView(); this.Close(); }
-
You made a new form to call a method on and then throw it away. I would advice you not to ask why that does not do what you want, I'm sure several people will laugh at you and/or 1-vote you.
-
Hi all, I am having problems passing a method from form 1 to form 2. I have on form 1 a dgv and a button. When the form 1 button is clicked it takes me to form 2. On form 2 I have the same set up, a dgv and a button. However, the button on Form 2, when clicked should call a method from form 1 then closes form 2. When I run the windows form application I get no errors. The method on form 1 that I try to call from form 2 is public. Any help is much appreciated. Thank you! Code: Form 1 public void UpdateGridView() { // Method code } form 2 public void MapFormUpdate_Click(object sender, EventArgs e) { Form1 MainForm = new Form1(); MainForm.UpdateGridView(); this.Close(); }
Form1 MainForm = new Form1();
is not going to work as it's a new instance of Form1, not the one that you started with. The recommended way of doing this is to raise a custom event in form2, that Form1 subscribes to, so Form1 calls its own method.using System;
using System.Windows.Forms;public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void buttonShowForm2\_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); form2.DoUpdate += new EventHandler(form2\_DoUpdate); form2.Show(); } void form2\_DoUpdate(object sender, EventArgs e) { // Do your thing here Console.Write("Update"); }
}
using System;
using System.Windows.Forms;public partial class Form2 : Form
{
public event EventHandler DoUpdate;public Form2() { InitializeComponent(); } void buttonUpdate\_Click(object sender, EventArgs e) { OnDoUpdate(EventArgs.Empty); } protected virtual void OnDoUpdate(EventArgs e) { EventHandler eh = DoUpdate; if (eh != null) eh(this, e); Close(); }
}
If you need to pass data along with the event, create your own class derived from EventArgs and pass an instance of that instead of EventArgs.Empty. You will need to change the EventHandler to EventHandler<YourEventArgs> See my Events Made Simple[^] article for more details.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
I tried it without the Form1 MainForm = new Form1(); code but it gives an error NullReferenceException. How can I get around this?
You want to call it on the original form, not a new one. Usually you only have 1 instance of the main form anyway, so you could use the singleton pattern (makes managers and Java programmers happy) or just pass the instance of form1 to form2 when you make form2. (or use an event, see Davey's post) There is a difference between a class and an instance of it. I'm sure you know, but it seems like you need to be reminded of that. But no offense..
-
You want to call it on the original form, not a new one. Usually you only have 1 instance of the main form anyway, so you could use the singleton pattern (makes managers and Java programmers happy) or just pass the instance of form1 to form2 when you make form2. (or use an event, see Davey's post) There is a difference between a class and an instance of it. I'm sure you know, but it seems like you need to be reminded of that. But no offense..
-
Hi all, I am having problems passing a method from form 1 to form 2. I have on form 1 a dgv and a button. When the form 1 button is clicked it takes me to form 2. On form 2 I have the same set up, a dgv and a button. However, the button on Form 2, when clicked should call a method from form 1 then closes form 2. When I run the windows form application I get no errors. The method on form 1 that I try to call from form 2 is public. Any help is much appreciated. Thank you! Code: Form 1 public void UpdateGridView() { // Method code } form 2 public void MapFormUpdate_Click(object sender, EventArgs e) { Form1 MainForm = new Form1(); MainForm.UpdateGridView(); this.Close(); }
Hej bwood2020 You Migth whant to use a static resource that can laungh that form1 passes an event to
static class EventHandler
{
static Action Event; // the global state
public static void SetGlobalEvent(Action a) // Setts the event
{
Event = a;
}
public static void Execute_Event() // Execute the event
{
Event();
}} class Form1 { public Form1() { EventHandler.SetGlobalEvent(Event); // Sett the event } public void Event() // The events that going to be executet { var x = 2 + 2; } } class Form2 { public void Onclick() { EventHandler.Execute\_Event(); // Execute the event this.Destroy(); // Destroy your form ( or close ) } }
There migth be a bether way of doing this if form1 creates form2 you can pass in "this" from form1 as a parameter that way form2 whill be able to call the method on that object Hopes this helps Patrik
-
Hej bwood2020 You Migth whant to use a static resource that can laungh that form1 passes an event to
static class EventHandler
{
static Action Event; // the global state
public static void SetGlobalEvent(Action a) // Setts the event
{
Event = a;
}
public static void Execute_Event() // Execute the event
{
Event();
}} class Form1 { public Form1() { EventHandler.SetGlobalEvent(Event); // Sett the event } public void Event() // The events that going to be executet { var x = 2 + 2; } } class Form2 { public void Onclick() { EventHandler.Execute\_Event(); // Execute the event this.Destroy(); // Destroy your form ( or close ) } }
There migth be a bether way of doing this if form1 creates form2 you can pass in "this" from form1 as a parameter that way form2 whill be able to call the method on that object Hopes this helps Patrik
Patrik.karlin wrote:
if form1 creates form2 you can pass in "this" from form1 as a parameter
... but now you're starting to couple unrelated classes - not a great idea. The easiest, sure - but best avoided.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Form1 MainForm = new Form1();
is not going to work as it's a new instance of Form1, not the one that you started with. The recommended way of doing this is to raise a custom event in form2, that Form1 subscribes to, so Form1 calls its own method.using System;
using System.Windows.Forms;public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void buttonShowForm2\_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); form2.DoUpdate += new EventHandler(form2\_DoUpdate); form2.Show(); } void form2\_DoUpdate(object sender, EventArgs e) { // Do your thing here Console.Write("Update"); }
}
using System;
using System.Windows.Forms;public partial class Form2 : Form
{
public event EventHandler DoUpdate;public Form2() { InitializeComponent(); } void buttonUpdate\_Click(object sender, EventArgs e) { OnDoUpdate(EventArgs.Empty); } protected virtual void OnDoUpdate(EventArgs e) { EventHandler eh = DoUpdate; if (eh != null) eh(this, e); Close(); }
}
If you need to pass data along with the event, create your own class derived from EventArgs and pass an instance of that instead of EventArgs.Empty. You will need to change the EventHandler to EventHandler<YourEventArgs> See my Events Made Simple[^] article for more details.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Patrik.karlin wrote:
if form1 creates form2 you can pass in "this" from form1 as a parameter
... but now you're starting to couple unrelated classes - not a great idea. The easiest, sure - but best avoided.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)DaveyM69 wrote:
... but now you're starting to couple unrelated classes - not a great idea. The easiest, sure - but best avoided.
.... Yea Sure .. maybe the logic should be in the model.... form1 can subscribe to a datachanged event and form2 youst update's the data in the model... then they whill be completle seperated.
-
Patrik.karlin wrote:
if form1 creates form2 you can pass in "this" from form1 as a parameter
... but now you're starting to couple unrelated classes - not a great idea. The easiest, sure - but best avoided.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -