Pass method to new form's control - c#

So i have 2 forms: Form1 and Form2
In Form1 i have this code:
pubilc class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Button1_Click(object sender, EventArgs e)
{
Form2 newForm = new Form2(); //Here i want to pass MyFunction
}
public void MyFunction()
{
MessageBox.Show("This is passed function from " + this.Text);
}
}
And in Form2 i have this:
public class Form2 : Form
{
public Form2() //Here i want to receive Form1.MyFunction
{
InitializeComponent();
this.Button1.Click += new System.EventHandler(passedFunction);
}
}
In code above you can get my point. Having form with controls to which i would assign Eventhandlers with passed function while creating that form.
I haven't tried anything since i do not have idea how to pass method to form.

Try this:
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Button1_Click(object sender, EventArgs e)
{
Form2 newForm = new Form2(MyFunction); //Here i want to pass MyFunction
}
public void MyFunction(object sender, EventArgs e)
{
MessageBox.Show("This is passed function from " + this.Text);
}
}
public class Form2 : Form
{
public Form2(EventHandler passedFunction) //Here i want to receive Form1.MyFunction
{
InitializeComponent();
this.Button1.Click += passedFunction;
}
}

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 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 to call non-static method on form1 from form2

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
}

How to enable button in Form 1 from Form 2 using public get set?

I have two buttons in Form 1, one is "ShowForm2" button and one is "button1" button.
Button 1 is disable by default. And when I click "ShowForm2" button, Form 2 will show.
So, what I want is, when I click the "button2" in form 2, it will enable the "button1" in form 1.
So, I try to code like this in my form2 class:
public partial class Form2 : Form
{
bool enable_form1_button1;
public bool Enable_form1_button1
{
get { return this.enable_form1_button1; }
set { this.enable_form1_button1 = value; }
}
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
enable_form1_button1 = true;
}
}
Then in my Form1 class, I am expecting to get the "enable_form1_button1 = true" to pass into form 1 and enable my form 1 button1. But how to do this?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btb_Showfrm2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
button1.Enabled = frm2.Enable_form1_button1; // I put it here, and it just does not seems right
}
}
Well what you can do is, expose the button as a property and send a reference of your current form to the form that needs to take control:
Form1
public partial class Form1 : Form
{
public Button BtnShowForm2
{
get { return btnShowForm2; }
set { btnShowForm2 = value; }
}
private void btnShowForm2_Click(object sender, EventArgs e)
{
// pass the current form to form2
Form2 form = new Form2(this);
form.Show();
}
}
Then in Form2:
public partial class Form2 : Form
{
private readonly Form1 _form1;
public Form2(Form1 form1)
{
_form1 = form1;
InitializeComponent();
}
private void btnEnabler_Click(object sender, EventArgs e)
{
// access the exposed property here <-- in this case disable it
_form1.BtnShowForm2.Enabled = false;
}
}
I hope this is what you're looking for.
Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
Form2 oFrm2 = new Form2();
oFrm2.evtFrm += new ShowFrm(oFrm2_evtFrm);
oFrm2.Show();
}
void oFrm2_evtFrm()
{
button1.Enabled = true;
}
}
Form2.cs
public delegate void ShowFrm();
public partial class Form2 : Form
{
public event ShowFrm evtFrm;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (evtFrm != null)
{
evtFrm();
}
}
}
you can send a refrence of your form1 to your form2 in the from2 constructor for example :
and then in button clicked event handler in form2 call a public method in form1 to enable the button :
this could be your form1 code :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
public void enableButton()
{
button2.Enabled = true;
}
this could be your form2 code :
public partial class Form2 : Form
{
private Form1 frm;
public Form2(Form1 frm)
{
InitializeComponent();
this.frm = frm;
}
private void button1_Click(object sender, EventArgs e)
{
frm.enableButton();
}
If you'll only have one instance of Form1 you can also get away with having a static Button member in Program which references the button you want to enable from Form2.
Form1.cs:
using System;
using System.Windows.Forms;
namespace StackOverflow_2013_05_19_Form1_Form2_buttons
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button2.Enabled = false;
Program.button = button2;
}
private void button1_Click(object sender, EventArgs e)
{ new Form2().Show(); }
private void button2_Click(object sender, EventArgs e) { }
}
}
Form2.cs:
using System;
using System.Windows.Forms;
namespace StackOverflow_2013_05_19_Form1_Form2_buttons
{
public partial class Form2 : Form
{
public Form2()
{ InitializeComponent(); }
private void button1_Click(object sender, EventArgs e)
{ Program.button.Enabled = true; }
}
}
Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace StackOverflow_2013_05_19_Form1_Form2_buttons
{
static class Program
{
public static Button button;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
I would recommend you to use event/delegates to perform this task. This is the standard way of doing so.
Again passing reference in constructor or making static func/var/properties are just workaround and Called as bad programming.

Categories

Resources