how to call non-static method on form1 from form2 - c#

can sameone provide example code on how to call non-static method on form1 from form2.
form1
public Form1()
{
InitializeComponent();
}
public void prikazi()
{
MessageBox.Show("ok");
}
private void openf2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
}
form2
public Form2()
{
InitializeComponent();
}
private void callMethod_Click(object sender, EventArgs e)
{
// this don't work. If I change to public static void on form1 then it work's but I need non-static example
Form1.prikazi();
}
thanks

It doesn't matter if it's a form class, if you want to access a non-static method, there is no other alternative then to create an instance of the class.
But - It doesn't make sense.. so don't do it
Find other alternatives, like creating the method you need static in a common place, or consider adding this method (or a variation of it) to the form

form1
public Form1()
{
InitializeComponent();
}
public void prikazi()
{
MessageBox.Show("ok");
}
private void openf2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
form2
private Form1 parentForm;
public Form2(Form1 parentForm)
{
this.parentForm = parentForm;
InitializeComponent();
}
private void callMethod_Click(object sender, EventArgs e)
{
parentForm.prikazi();
}
But better learn to bundle reusable code in to a separate class, rather than passing around form instances.

You need to have an instance of the form to call the method.
There are a few ways that you can make this work
1) Pass an action through to the new form
public Form2()
{
InitializeComponent();
}
public Action yourAction {get; set;}
private void callMethod_Click(object sender, EventArgs e)
{
Action instance = yourAction;
if(instance != null)
instance();
}
then in Form 1 you can say
private void openf2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.yourAction = prikazi;
frm.Show();
}
2) You can pass an instance of Form1 into Form 2
So in Form 2 you have:
public Form1 ParentForm {get; set;}
private void callMethod_Click(object sender, EventArgs e)
{
if (ParentForm != null)
ParentForm.prikazi();
}
And Form1 you assign a value to the ParentForm variable
private void openf2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.ParentForm= this;
frm.Show();
}

public partial class Form1 : Form
{
internal static Form1 ViewForm1; // make other form run Public void
public form1()
{
InitializeComponent();
ViewForm1 = this; //Add this
}
public void DoSomething()
{
//Code...
}
}
......................
public partial class Form1 : Form
{
public form2()
{
InitializeComponent();
Form1.ViewForm1.ShowData(); // call public void from form1
}

Related

How do i pass the value from the first form to the second form messageBox ? It will show 0

This is the first form( it contains an OK button and a textbox)
namespace Testt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int dimx;
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
this.Hide();
f2.ShowDialog();
this.Show();
dimx = int.Parse(textBox1.Text);
MessageBox.Show(dimx.ToString());
}
}
}
This is the second form (it contains an OK button + a messageBox when OK is pressed)
namespace Testt
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f=new Form1();
MessageBox.Show(f.dimx.ToString());
}
}
}
I want to write the value of 6 in the textbox press OK, then form2 pops up and when i press OK on the second form it should display 6 not 0..what am i doing wrong?
You could make it so that your form takes dimx as a variable, so it would look like this
public partial class Form2 : Form
{
private int dimX;
public Form2(int dimx)
{
InitializeComponent();
dimX = dimx;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(dimX.ToString());
}
}
alternatively you could pass the form itself, changing
public Form2(int dimx)
into
public Form2(Form1 f1)
You would then also have to replace
private int dimX;
//and
dimX = dimx;
//and
MessageBox.Show(dimX.ToString());
with
private Form1 f;
//and
f = f1;
//and
MessageBox.Show(f.dimx.ToString());
You are creating a NEW form object on every onclick event and that is not the way it works...
private void button1_Click(object sender, EventArgs e)
{
Form1 f=new Form1(); // no that way!!!
MessageBox.Show(f.dimx.ToString());
}
use instead a callback, delegate
So im using this now
Form1:
namespace Testt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int dimx;
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
this.Hide();
f2.ShowDialog();
this.Show();
dimx = int.Parse(textBox1.Text);
//MessageBox.Show(dimx.ToString());
}
}
}
and Form2:
namespace Testt
{
public partial class Form2 : Form
{
private Form1 f;
public Form2(Form1 f1)
{
InitializeComponent();
f=f1;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(f.dimx.ToString());
}
}
}

Changing TextBox Text of Form1 From Form2 in C#

I'm new in C# programming. I have a beginner level question:
How do I change the text property of the textbox1 in my form 2 object using a button in my form1?
Here's my code in form1:
namespace DoubleForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
}
This is in form2:
namespace DoubleForms
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.textBox1.Text = "Test";
}
}
}
When you add a text box or any control for that matter to a Winform using the controls toolbox the control gets added as private so it can't be accessed outside of the class it's created in. Easy enough to fix though just added a public property that lets you get and set the text box value as such
namespace DoubleForms
{
public partial class Form1 : Form
{
// NEW CODE
public string TextBoxText
{
get { return this.textBox1.Text; }
set { this.textBox1.Text = value; }
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
}
Then from Form2 you can just call form1.TextBoxText = "blah blah" to set the value.
Code is creating new Form1 every-time you click the button, which is not you want I believe.
What you need to do is create an event in Form2 and then subscribe to that event in Form1, that way you can listen changes from Form2 and update Form1.
namespace DoubleForms
{
public partial class Form2 : Form
{
public event EventHandler Updated; // define an event handler
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if(Updated != null)
{
Updated(sender, new EventArgs()); //Raise a change.
}
}
}
}
Now in Form1 subscribe to Form2 event.
namespace DoubleForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Updated += (se,ev)=> textBox1.Text = "Test"; // update textbox
frm2.Show();
}
}
}
//this code worked for me
//in form2 put following code prevent form from opening multiple times
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private static Form2 Instance;
public static Form2 GetInstance()
{
if (Instance ==null || Instance.IsDisposed)
{
Instance = new Form2();
}
else
{
Instance.BringToFront();
}
return Instance;
}
// in form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
Form2 form2 = Form2.GetInstance();
form2.textBox1.Text = textBox1.Text;
form2.Show();
}
}
//this code worked for me
//in form2 put following code prevent form from opening multiple times
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private static Form2 Instance;
public static Form2 GetInstance()
{
if (Instance ==null || Instance.IsDisposed)
{
Instance = new Form2();
}
else
{
Instance.BringToFront();
}
return Instance;
}
// in form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button2_Click(object sender, EventArgs e)
{
Form2 form2 = Form2.GetInstance();
form2.textBox1.Text = textBox1.Text;
form2.Show();
}
}

How to reLoad a form from another form

I have two forms, Form1 and Form2. I use ShowDialog() on Form2 from Form1. How can I run Form1Load() from Form2? Specifically, I want to refresh Form1 from Form2.
Firstly, make sure you assigning the Owner property on Form2 before showing it. This allows you to access the current instance of Form1.
class Form1 : Form
{
public void Method()
{
var form2 = new Form2();
form2.Owner = this;
form2.ShowDialog();
}
}
From Form2 you can this use this.Owner to access the instance of Form1 and call any public methods or access any public properties. (Make sure the load event handler is public)
class Form2 : Form
{
public void Method()
{
this.Owner.form1_Load(null,null); //assuming you don't use these params.
}
}
In your form1_Load() I would recommend putting in a this.Refresh() to "refresh" the form. The refresh doesn't update some things that have data stored, it only repaints the form, so in the load event you will have to manually "refresh" things.
Create an instance of Form1 and then use the Refresh method or your Form1_Load method with that instance.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void ShowMessage(string message)
{
MessageLabel.Text = message;
}
private void ShowForm2(object sender, EventArgs e)
{
Form2 Form2Copy = new Form2(this);
Form2Copy.ShowDialog();
}
}
and
public partial class Form2 : Form
{
Form1 Form1Copy;
public Form2(Form1 Parent)
{
InitializeComponent();
Form1Copy = Parent;
}
public void Button_Click(Object sender, EventArgs e)
{
Form1Copy.ShowMessage("Hello from Form2!");
}
}
Pass in Form1 to the ShowDialog() method of your Form2 instance:
private void Form1_Load(object sender, EventArgs e)
{
this.LoadEventCode();
}
public void LoadEventCode()
{
this.Text = DateTime.Now.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog(this); // <-- pass in Form1
}
Now over in Form2, cast the .Owner property to Form1 and do what you need:
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = (Form1)this.Owner;
f1.LoadEventCode();
}

How can I make that approach's opposite

How can I passing values from a Form to another one directly? A receiver Form will be showing itself on screen and listening passing values which will be sending from a main form.
I know a way to do that with delegate and event but mine is not my desired one.
I need that with opposite way. Below is what I can do those code lines. This able to do only Form2 passes value to Form1 (main form). I need this approach's opposite. So, Form1 will be sender Form2 will be receiver, and transmit in real-time while they are showing itself on the screen.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.IdentityUpdated += new Form2.IdentityUpdateHandler(Form2_ButtonClicked);
f.Show();
}
private void Form2_ButtonClicked(object sender, IdentityUpdateEventArgs e)
{
textBox1.Text = e.FirstName;
}
}
public partial class Form2 : Form
{
public delegate void IdentityUpdateHandler(object sender, IdentityUpdateEventArgs e);
public event IdentityUpdateHandler IdentityUpdated;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
string sFirstName = txtFirstName.Text;
IdentityUpdateEventArgs args = new IdentityUpdateEventArgs(sFirstName);
IdentityUpdated(this, args);
}
}
public class IdentityUpdateEventArgs : System.EventArgs
{
private string mFirstName;
public IdentityUpdateEventArgs(string sFirstName)
{
this.mFirstName = sFirstName;
}
public string FirstName
{
get { return mFirstName; }
}
}
try this way
public partial class Form1 : Form
{
public delegate void IdentityUpdateHandler(object sender, EventArgs e);
public event IdentityUpdateHandler IdentityUpdated;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
IdentityUpdated(this, new EventArgs());
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
Form1 form1 = (Form1)Application.OpenForms["Form1"];
form1.IdentityUpdated += Form1OnIdentityUpdated;
}
private void Form1OnIdentityUpdated(object sender, EventArgs eventArgs)
{
MessageBox.Show("received");
}
}

How can i run Button1.PerformClick() in Form1 with a Button on Form2 when two forms are opened

I have 2 Form and i want run Button1.PerformClick() in form1 with button2 in form2, when two forms are opened !!!
I dont want make a instance Form1.
Thanks friends
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonShowForm2_Click(object sender, EventArgs e)
{
Form2 F2 = new Form2();
F2.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
//Run my method...
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void ClickButton1Form1_Click(object sender, EventArgs e)
{
Form1 //Run my method... |Form1.Button1.PerformClick()
}
}
Form2 needs a reference to an instance of Form1 and needs to invoke the functionality on that instance.
Create a method on Form1 which invokes this functionality:
public void PerformButton1Click()
{
Button1.PerformClick();
}
Require an instance of Form1 on the constructor for Form2:
private Form1 Form1Instance { get; set; }
public Form2(Form1 form1Instance)
{
InitializeComponent();
Form1Instance = form1Instance;
}
Pass a reference to the existing instance when constructing an instance of Form2:
Form2 F2 = new Form2(this);
F2.ShowDialog();
Then you can invoke that functionality on that instance:
private void ClickButton1Form1_Click(object sender, EventArgs e)
{
Form1Instance.PerformButton1Click();
}

Categories

Resources