How can I make that approach's opposite - c#

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

Related

Pass method to new form's control

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

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());
}
}
}

C# - Setting a Value in a Button then passing it into a ListView

I have two forms, form1 and form2.
In form1, there is are two buttons, button1 and button2.
In form2, there is a listview, ListView1.
button1 should hold a string value called "Vanilla".
When button2 is pressed it opens form2.
On form2, in listview1 it should show "Vanilla" in the first column.
Form1
public partial class form1 : Form
{
public static string buttonValue = "";
public form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
buttonValue = "Vanilla";
}
public void button2_Click(object sender, EventArgs e)
{
form2 form2 = new form2();
form2.Show();
this.Hide();
}
Form2
public partial class form2 : Form
{
public form2()
{
InitializeComponent();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
You can design the second form as bellow:
public partial class form2 : Form
{
public form2()
{
InitializeComponent();
}
private string _passedValue = "";
public form2(string passedValue)
{
InitializeComponent();
_passedValue = passedValue;
listView1.Items.Add(_passedValue);
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
You can pass the value stored in the first button using the bellow code.
public void button2_Click(object sender, EventArgs e)
{
form2 form2 = new form2(buttonValue);
form2.Show();
this.Hide();
}

How do I pass text from child to main form via a user control?

I am new to c#. I have the following in my project in windows forms:
Form1 with button and textbox.
User control with a buttton.
Form2 with button and textBox.
As shown in the screenshot: In form1, I click "Show User Control1" User Control1 pops up. Then in User Control1 I click Show Form2 form2 pops up.
In Form2 I enter values in textBox and when click "Send to textbox in form1" I want this text to be inserted into the textbox in Form1.
My question is: How can I send text from form2 to textbox in form1 via user control1?
I just need to know some steps to follow or some code if it is possible to achieve this.
Please help me. Thank you
Form1:
public partial class Form1 : Form
{
UserControl1 UC1 = new UserControl1();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Controls.Add(UC1); //add a userControl
UC1.Visible = true;
}
}
User Control1:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// I want to send text to form1 when this button is clicked
}
}
You can do it by trigger event and event handler.
In Form2,
public delegate void SendTextF2(string YourStringFromTextBox);
public partial class Form2 : Form
{
public event SendTextF2 UISendTextHandlerF2;
public Form2(TextBox s)
{/*unchange*/}
private void button1_Click(object sender, EventArgs e)
{
if(UISendTextHandlerF2!=null)
UISendTextHandlerF2(textBox1.Text);
}
}
In UserControl1,
//New
public delegate void SendTextUC(string YourStringInTextBox);
public partial class UserControl1 : UserControl
{
//New
public event SendTextUC UISendTextHandlerUC;
public UserControl1(TextBox r)
{
InitializeComponent();
this.r = r;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(r);
frm2.Show();
//Add event handler
frm2.UISendTextHandlerF2 += SendText123;
}
//Event Handler for the event trigger in Form2
void SendText123(string YourStringFromTextBox)
{
//Trigger Event
if(UISendTextHandlerUC!=null)
UISendTextHandlerUC(YourStringFromTextBox);
}
}
In Form1,
public partial class Form1 : Form
{
UserControl1 UC1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (UC1 == null)
{
UC1 = new UserControl1(textBox1);
//Add event handler
UC1.UISendTextHandlerUC += FinallyWeGetTheString;
}
Controls.Add(UC1);
UC1.Visible = true;
}
//New
void FinallyWeGetTheString(string YourStringFromTextBox)
{
textBox1.Text = YouStringFromTextBox;
}
}
Add those line to your code:
public partial class Form1 : Form
{
UserControl1 UC1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (UC1 == null)
{
UC1 = new UserControl1(textBox1);
}
Controls.Add(UC1);
UC1.Visible = true;
}
}
User Control:
public partial class UserControl1 : UserControl
{
TextBox r;
public UserControl1(TextBox r)
{
InitializeComponent();
this.r = r;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(r);
frm2.Show();
}
}
And Form2:
public partial class Form2 : Form
{
TextBox s;
public Form2(TextBox s)
{
InitializeComponent();
this.s = s;
}
private void button1_Click(object sender, EventArgs e)
{
String str = textBox1.Text;
s.Text = str;
}
}

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
}

Categories

Resources