This question already has an answer here:
Interaction between forms — How to change a control of a form from another form?
(1 answer)
Closed 5 years ago.
I have 2 forms, and the second form is meant to be active WHILE the first form is still active.
I need to pass the string value from textBox1.Text on Form1 to textBox1.Text on Form2.
How would such me done without reopening Form1 from Form2?
Is this what you meant?
Form1 form1 = new Form1();
Form1 form2 = new Form2();
form1.Show();
form2.Show();
form2.Button1_Click += delegate {form2.textbox1.Text = form1.textbox1.Text};
How about this?
class Form1 : Form
{
void ButtonOpenForm2_Click(object s, EventArgs ea)
{
this.form2 = new Form2(this);
this.form2.Show();
}
}
class Form2 : Form
{
public Form2(Form1 form1)
{
form1.textbox1.TextChanged += delegate {this.textbox1.Text = form1.textbox1.Text};
}
}
static void Main()
{
Form1 form1 = new Form1();
form1.Show();
}
public class Form2 : Form
{
//This property will hold the text, so populate the textbox from it
string TextProperty {get;set;}
public form2(string textFromForm1)
{
TextProperty = textFromForm1;
}
}
Now on form1:
Form2 form2 = new Form2(textbox1.Text);
form2.ShowDialog();
Related
In Form1 I have one DataGridView and multiple textboxes. When I click A button in Form2 I need to save the data from DataGridView and multiple textboxes to Database. How to Implement in C sharp Windows Application
Form1 Button Click event. I opened Form2
public sealed partial class form1 : Form
{
private static form1 instance = null;
public static form1 Instance
{
get
{
if (instance == null)
{
instance = new form1();
}
return instance;
}
}
private void button1_Click(object sender, EventArgs e)
{
textbox2.Text=100;
form2 CO = new form2();
CO.Show();
}
}
I want to attach textboxes data and Datagridview content to
object SO and Call InsertSale function .textboxes and datagridview are in form1
This is Button Click Event in Form 2
private void button1_Click(object sender, EventArgs e)
{
clsSale SO = new clsSale();
SO.Totamount = Convert.ToDecimal(form1.Instance.textBox2.Text);
SO.InserSale(SO);
}
If Form2 wants to access the Form1 properties.
Pass ParentForm instance to the ChildForm constructor. Add a public method in the parent form to update its properties from child form.
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
}
public void SetTextBoxValue(string val)
{
this.textBox1.Text = val;
}
private void CreateForm2()
{
var form2 = new Form2(this);
form2.Show();
}
}
public partial class Form2: Form
{
private Form1 form1;
public Form2(Form1 frm1)
{
InitializeComponent();
form1= frm1;
form1.SetTextBoxValue("Value from Form2");
}
}
Create a global class, such as Global.cs, in the project. Then declare the following variables:
public static Form1 frm1
public static Form2 frm2
Declare a variable of the Form class - Form frm1 or Form frm2 etc.
Now access the variables from any form as follows:
Global.frm1 = new Form1() // - for the Home Form1
Global.frm1.ShowDialog();
Global.frm2 = new Form2() // - for the Home Form1
Global.frm2.ShowDialog();
If you want to access a control in any form, just extend them as follows:
frm1.txtBox1.Text
frm2.button1.Click() // etc.
I have 2 windowsForms lets call them form1 and form2. form1 consists of a picturebox and a button. When I click on the button form2 opens. Now form2 consists of a grid and depending on the position on which I have clicked on the grid the x and y coordinates are returned to form1. Based on these coordinates I add an image to the pictureBox. But the image isn't getting added! Is there something I am missing here?
Code inside the mouseDownEvent in form2
Form1 f1 = new Form1();
f1.openImage(x,y);
Code in form1
internal void openImage(int x, int y)
{
string ogFileName = "r" + x.ToString() + "c" + y.ToString();
string imageFilePath = ogFileName + "." + extension;
MessageBox.Show(imageFilePath); //I can see the correct path here
pictureBox1.Image = Image.FromFile(imageFilePath);
}//extension is a static variable declared outside this function.
Form1 f1 = new Form1();
This line only creates a new instance of Form1
What you can do is adding a Form1 variable which will reference your current form.
You initialize it in constructor and in the button click you pass it to Form2
public partial class Form1 : Form
{
Form1 form1; // form1 will store the reference of Form1
public Form1()
{
form1 = this; // We initialize form1 in the constructor
InitializeComponent();
}
// button to open form2
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(form1); // We open form2 with form1 as parameter
form2.Visible = true;
}
public void openImage(int x, int y)
{
}
}
Now in the Form2 you just have to add a Form1 variable that will be initialized in the constructor.
Then you can use it as now it represents the current instance of Form1
public partial class Form2 : Form
{
Form1 form1; // Reference to form1
public Form2(Form1 form1)
{
this.form1 = form1; // We initialize form1
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// we call openimage from form1
form1.openImage(130, 140);
}
}
I have tested this example with a label and it works fine so I think there should be no problem with a picturebox
I use Form.Show() to open a new form
Like this:
public Form2()
{
Form1 fm = new Form1();
fm.Show();
}
How do I get Form2's name when Form1 has control?
What I am doing is create a public variable(ex.pFormname) in From1
and set pFormname = Form2 before the show common like this:
public Form2()
{
Form1.pFormname = "Form2";
Form1 fm = new Form1();
fm.Show();
}
Is there any other way except creating a public variable?
And is there any resource release should I do after closing Form1?
public Form2()
{
Form1 fm = new Form1();
Form1.pFormname = this.Text; // this = Form2, Text = the arbitrary name of the form.
fm.Show();
}
This requires you to have a public instance variable or property called pFormname in Form1 class. the 'this' keyword represents the current instance of Form2, and Text is the property which contains the name of the Form.
If you want to access other properties on the parent form, you could pass it as a reference when you instantiate Form1.
public Form2()
{
Form1 fm = new Form1(this); // this = form2.
fm.Show();
}
In Form1 you would have something like this:
public partial class Form1 : Form
{
private Form2 _parentForm;
public Form1(Form2 parentForm)
{
_parentForm = parentForm;
InitializeComponent();
}
}
And you would call public variables/properties/methods like this:
_parentForm.SomeMethod();
You could use the forms Owner property and pick that up in your instance later. For example in the constructor of Form2:
public Form2()
{
Form1 fm = new Form1 {Owner=this};
fm.Show();
}
You use the Owner name in Form1 like this
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
}
void Form1_Load(object sender, EventArgs e)
{
label1.Text += Owner.Name;
}
}
If you want to find the name of the parent form , you can do so by using
string s = ParentForm.Name; // s will contain the name of the parent form
Hope this helps..
If you want to find out , the child ..you can do so by:
MdiChildren property.
this will help you to find out , which child forms(Form 2) are contained by currently opened form(Form1).
Refer to this for how to use this exactly:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.mdichildren(v=vs.110).aspx
The best thing you can do in that situation is to have a private field in Form1 and set it equal to Form2 when you instantiate it like this.
class Form1 : Form
{
private Form _parentForm;
public Form1(Form f)
{
_parentForm = f;
}
}
and when you instantiate Form1 do like this
Form1 fm = new Form1(this) // in the constructor of Form2;
after this you will have the reference to the Form2 in the Form1's _parentForm variable and you can easuily access any of its properties for instance;
MessageBox.Show(_parentForm.Name) // in Form1 will show the name of Form2
or you can just have a string which you set the this.Name when you instantiate Form1 in Form2's constructor
class Form1 : Form
{
private string _parentFormName;
public Form1(string name)
{
_parentForm = f;
}
}
Form1 fm = new Form1(this.Name) // in the constructor of Form2;
and then you can directly call that variable like this
MessageBox.Show(_parentFormName);
Hello everyone I'm new to c#, using visual c# 2010 and I'm trying to learn the basics.
I've created two forms (Form1 and Form2)
each form has a text box and a button
my experiment( form1 to form2) is as follows:
I've declared a string
public string deneme;
I made both text boxes public, and in form 1 button I write the following:
deneme= textbox1.text;
Form2 frm2 = new Form2();
form2.show();
form2.textbox1.text= deneme;
when I do this it works and I see my input on form2 textbox. What I want to do is;
to press the button on form1 and open form2, then write something on the text box and display that input on form1 text box, I use the same method but it returns nothing.. what an I doing wrong?
and I'm sorry for my bad english
The simple way is a property of the Form2
You can add a property to the form2
public string Result{get;set;}
and check it in external code
form1.textbox1.text= form2.Result;
deneme= textbox1.text;
Form2 frm2 = new Form2(dename);
form2.show();
In form2, do this in the Initialize method:
Initialize(string name)
{
form2.textbox1.text= name;
}
Check these out for more than just a single value passed to another form, but to also handle passing values back/forth and also hooking into events... Not advanced stuff, but does have some step-by-step samples too
Prior posting here
Hello all I have solved my issue, here is what I have done !
In the second form, I have write the following codes
Form1 mymainform;
public Form2(Form1 m)
{
InitializeComponent();
afrm1 = m;
}
and in the button click I write the following
mymainform.Textboxnameinform1.text = textboxnameinform2.text;
this.close();
and in Form1, where I have the button to call form2 I write the following codes
Form2 frm2 = new Form2(this);
frm2.Show();
if anyone is having the same difficulty and couldn't figure it out, please don't hesitate to contact me I'll be happy if I can help you
//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();
}
}
I am trying to return some data from Form2 to Form1, everything seems fine, I got the data and so, but, when I try to pass my data to a textbox, it doesn't changes the text. Only if I open a new instance of Form1, on Form2 it works. Why this happen? Can't I send the text to the old instance?
I'm using this code;
Form1 (Main Form)
public void updateText(string data)
{
MessageBox.Show(data);
txtGood.Text = data;
}
Form2 SecondaryForm = new Form2();
SecondaryForm.ShowDialog();
Form2 (Second Form with user data)
Form1 MainForm = new Form1();
MainForm.updateText(data);
MainForm.ShowDialog();
this.Close();
So, my question is, how can I pass the data values to the old instance of the main form? without having to create a new instance and show a new instance. Is there a way to do this?
This is because you're creating a instance of Form1 in your Form2 code. What you want to do is setup Form2's parentForm to be the instance of the Form1 that created it.
public partial class Form1 : Form
{
public void CreateForm2()
{
Form2 form2 = new Form2(this);
form2.ShowDialog();
}
public string MyTextboxText
{
get { return txtMyTextbox.Text; }
set { txtMyTextbox.Text = value; }
}
}
public partial class Form2 : Form
{
private Form1 parentForm;
public Form2(Form1 parentForm)
{
this.parentForm = parentForm;
}
public void myButtonClick()
{
parentForm.MyTextboxText = "Hello";
}
}
This code is just an example, probably wont compile as-is.
What you can do is pass the reference of MainForm(Form1) to second Form(Form2). Then instead of creating MainForm again use the reference to update the textbox.
//pass reference to form2
Form2 SecondaryForm = new Form2(mainForm);
SecondaryForm.ShowDialog();
//in the constructor of Form2 save the reference of Form1
Form1 form1 = null
Form2(Form1 mainForm)
{
form1 = mainForm;
}
//then instead of creating a new MainForm again just use reference of Form1
form1.updateText(data);
this.Close()
main form:
private void Button2_Click(object sender, EventArgs e) {
frmCustomersRecord rec = new frmCustomersRecord(this);
rec.ShowDialog();
rec.GetData();
}
child form:
public partial class frmCustomersRecord : Form
{
public frmCustomersRecord()
{
//blank contructor (Instance of an class)
}
frmCustomerDetails cd;
public frmCustomersRecord(frmCustomerDetails parentForm) : this()
{
this.cd = parentForm;
}
//call the methods using child form object
}