*i can't figure out how to allow form1 to be passed to form3 and form2 to be able to pass to form3 too. PLEASE HELP!
it has this error:
does not contain a constructor that takes '1' argument on the ff lines:
f3 = new Form3(this); on Form1
f3 = new Form3(this); on Form2
Here are the codes:
Base Form(choose to form1 or form2):
namespace WindowsFormsApplication1
{
public partial class baseform : Form
{
Form1 f1;
Form2 f2;
public baseform()
{
InitializeComponent();
f1 = new Form1(this);
f2 = new Form2(this);
}
private void baseform_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
f1.Show();
}
private void button1_Click(object sender, EventArgs e)
{
f2.Show();
}
}
}
Form1:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
baseform bf;
Form3 f3;
public Form1(baseform bf1)
{
InitializeComponent();
f3 = new Form3(this);
bf = bf1;
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Form2:
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
baseform bs;
Form3 f3;
public Form2(baseform bs1)
{
InitializeComponent();
f3 = new Form3(this);
bs = bs1;
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Form3:
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
Form1 f1;
Form2 f2;
public Form3(Form1 f1a, Form2 f2a)
{
InitializeComponent();
f1 = f1a;
f2 = f2a;
}
private void Form3_Load(object sender, EventArgs e)
{
}
}
}
Well you're only constructor in class Form3 has two arguments:
public Form3(Form1 f1a, Form2 f2a)
but in both places you are trying to call it with only one:
f3 = new Form3(this);
So, either call it with two arguments (perhaps null?) or create a constructor (or two) that takes one argument. In former case you'll need to have:
f3 = new Form3(this, null);
when calling from Form1 and:
f3 = new Form3(null, this);
when calling from Form2
Plainly speaking, the error is pretty clear: Form3 does not have constructor that take one argument. It can take only 2 arguments.
However, you problem is deeper than that. The most simple workaround to your problem will be to make the fields static, then add extra method to initialize them:
public partial class Form3 : Form
{
static Form1 f1;
static Form2 f2;
public Form3()
{
InitializeComponent();
}
public static void InitForms(Form1 f1a, Form2 f2a)
{
f1 = f1a;
f2 = f2a;
}
private void Form3_Load(object sender, EventArgs e)
{
//use f1 and f2...
}
}
Then have this:
public baseform()
{
InitializeComponent();
f1 = new Form1(this);
f2 = new Form2(this);
Form3.InitForms(f1, f2);
}
Your are having cyclic references here. I suggest instead of constructor parameters use public properties to let each form know about other forms. Before doing anything with such a reference check if it is assigned (!=null). You need to create all forms first and then assign their properties to establish references.
public partial class Form3 : Form
{
public Form1 f1 { get;set;}
public Form2 f2 { get;set;}
...
private void button1_Click(object sender, EventArgs e)
{
if(f1 != null)
f1.Show();
}
}
Then do that:
Form f1 = new Form1();
Form f2 = new Form2();
Form f3 = new Form3();
f3.f1 = f1;
f3.f2 = f2;
Follow that pattern with other forms too.
You try to create a form3 with constructor with 1 paramentrs.
f3 = new Form3(this);
But your constructor in Form3 is defined for 2 parameters
public Form3(Form1 f1a, Form2 f2a)
chnage your code to this.(given for for form3 and form2)
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public Form1 f1{get;set;}
public Form2 f2{get;set;}
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
}
}
}
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
baseform bs;
Form3 f3;
public Form2(baseform bs1)
{
InitializeComponent();
f3 = new Form3();
//and same for for form 2
f3.f2=this;
bs = bs1;
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
//This code is placed where you want to Switch Back to Form 1, so
// wherever in Form2 that you determine you have completed using Form2, such as
// a submit button or whatever...
private void submitButton_Click(object sender, EventArgs e)
{
Form1 switchBack = (Form1)Application.OpenForms["Form1"];
switchBack.childFormMethodCall(submitItemTextBox.Text);
}
//Then in Form1.cs, define the Method you wish to call from the second Form
//In my case, I did a simple method called childFormMethodCall with a single passed
//in parameter, a string
//I then set the string that was Passed in from Form2 as the Text for richTextBox1
//richTextBox1 is located on Form1
internal void childFormMethodCall(string p)
{
richTextBox1.Text = p;
}
Related
Im trying to change the text in Form1 when pushing the button on Form2
Form 2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.textCh = "Text has been changed";
}
}
Form 1:
public partial class Form1 : Form
{
public string textCh {
get
{
return this.textCh;
}
set
{
this.label1.Text = value;
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
}
}
When I'm pushing button nothing happens, the text remain the same.
Another method of passing Form1 to Form2 via the Show() method and the .Owner property:
// In Form1
Form2 f2 = new Form2();
f2.ShowDialog(this); // <-- pass Form1 via "this"
Then, in Form2, you CAST .Owner to type Form1:
// In Form2
Form1 f1 = this.Owner as Form1;
if (f1!=null && !f1.IsDisposed)
{
f1.textCh = "Text has been changed";
}
There are several way to do this here 2 examples
This example use references whitout any use of events
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm = new Form2(this);
frm.Show();
}
}
here the Form2 is created passing the Form1 as parameter
public partial class Form2 : Form
{
Form1 _parent;
public Form2(Form1 parent)
{
_parent = parent;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var lbl = (Label)_parent.Controls.Find("label1", false).First();
lbl.Text = "new text";
_parent.Update();
}
}
Than the Form2 use that for set the value wanted.
This example use events
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm = new Form2();
frm.UpdateLabelEvent += Frm_UpdateLabelEvent;
frm.Show();
}
private void Frm_UpdateLabelEvent(string str)
{
label1.Text = str;
}
}
and here the code of Form2
public partial class Form2 : Form
{
public event Action<string> UpdateLabelEvent;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
UpdateLabelEvent("new string value");
}
}
Nothing changes in your Form1 because you are creating a new Form1 first and change the text there. The new Form1 is never shown, so you see no changes.
Solution
private void button1_Click(object sender, EventArgs e)
{
//Form1 f1 = new Form1(); GET rid of this line
f1.textCh = "Text has been changed";
}
you need to make sure off course that f1 is known in form2, if you dont know how here is a simple way to do that
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.f1 = this;
f2.ShowDialog();
}
public Form2()
{
public Form1 f1 { get; set; }
...
I would like to ask for help. I would like to achieve that in Form1 in the panel I open Form2 in which there is a button, when the user presses in the panel on Form1 opens Form3
Here I am in the Form1 code where the user presses the button that is in the window, and thus Form2 opens in the panel
{
public partial class Form1 : Form
{
private Form activeForm;
public Form1()
{
InitializeComponent();
}
public void OpenSlaveForm(Form podrizenyForm, object btnSender)
{
if (activeForm != null)
{
activeForm.Close();
}
activeForm = podrizenyForm;
podrizenyForm.TopLevel = false;
podrizenyForm.FormBorderStyle = FormBorderStyle.None;
podrizenyForm.Dock = DockStyle.Fill;
this.pnlMain.Controls.Add(podrizenyForm);
this.pnlMain.Tag = podrizenyForm;
podrizenyForm.BringToFront();
podrizenyForm.Show();
string nazev = podrizenyForm.Text.ToUpper();
}
private void btnForm1_Click(object sender, EventArgs e)
{
OpenSlaveForm(new Forms.Form2(), sender);
}
}
}
Here I am in Form2 and I try to open Form3 through the button in the Form1 panel
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnForm2_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.OpenSlaveForm(new Forms.Form3(), sender);
}
}
Thank you in advance for any help
You are creating a new Form1 instance when you press the button in Form2, that adds the new Form3 to that new instance instead in the one you are seeing on the screen.
You need to pass a reference to the current Form1 instance to Form2:
public partial class Form2 : Form
{
Form1 mainInstance;
//Add a new constructor which accepts a reference to Form1
public Form2(Form1 MainInstance)
{
mainInstance = MainInstance;
InitializeComponent();
}
public Form2()
{
InitializeComponent();
}
private void btnForm2_Click(object sender, EventArgs e)
{
//Now use that instance to create Form3
mainInstance.OpenSlaveForm(new Forms.Form3(), sender);
}
}
Now, change the button call in Form1:
private void btnForm1_Click(object sender, EventArgs e)
{
//Use the new constructor in order to pass the current instance
OpenSlaveForm(new Forms.Form2(this), sender);
}
I have to form Form1 and Form2
Form1 source
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.SetBtn = "teste test";
frm.Show();
}
public string setLb
{
set
{
label1.Text = value;
}
}
}
}
Form2 source
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.setLb = "test test";
}
public string SetBtn
{
set
{
button1.Text = value;
}
}
}
}
I try to set text of label in Form1 and text of button on Form2
I use same method to set value but only work when set value from Form1 to Form2. Button1.Text on Form2 changed to teste test but nothing happend on Form1
You need to pass a reference to your Form1 instance to the Form2 instance.
You can do that like this:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this); // <---- Pass a reference to this form to Form2
frm.SetBtn = "teste test";
frm.Show();
}
public string setLb
{
set
{
label1.Text = value;
}
}
}
}
And you would need to change the Form2 implementation a little aswell:
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
private Form1 other;
//Empty constructor for the designer
public Form2()
{
InitializeComponent();
}
public Form2(Form1 other)
{
this.other = other;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
other.setLb = "test test";
}
public string SetBtn
{
set
{
button1.Text = value;
}
}
}
}
Use Constructor method , here is example
In form2
public Form2(string strTextBox)
{
InitializeComponent();
label1.Text=strTextBox;
}
In form1 click event
private void label1_Click(object sender, System.EventArgs e)
{
Form2 frm=new Form2(textBox1.Text);
frm.Show();
}
For more information , reference Passing Data Between Forms !
In Form2 you have to add a parameter for the Form1.
i.e.
// Form1
...
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.SetBtn = "teste test";
frm.Show();
}
...
// Form2
public Form2(Form frm1)
{
InitializeComponent();
frm1.setLb = "test test";
}
public partial class Form1 : Form
{
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
Form2 frm = new Form2(this);
frm.Show();
}
}
public partial class Form2 : Form
{
Form _frm;
public Form2(Form frm)
{
_frm = frm;
InitializeComponent();
}`enter code here`
private void button1_Click(object sender, EventArgs e)
{
Form1 formVariable = (Form1)_frm;
formVariable.textBox.Text = "Hello";
}
}
I am having a problem with updating a textbox inside a form in c#, while loading a second form.
I have two forms in my application. form1 loads first then it loads form2.
When form2 loads it should update the the textbox.txt in form1 with some text (in this case: F2:Running), indicating that it has been loaded.
Any kind of help is appreciated, here's the current code:
namespace EditingBox {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
namespace EditingBox {
public partial class Form1: Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
Form2 F2 = new Form2();
Form1 F1 = new Form1();
F2.Show();
textBox1.Select();
textBox1.Refresh();
}
public void textBox1_TextChanged(object sender, EventArgs e) {
}
private void label1_Click(object sender, EventArgs e) {
}
}
}
namespace EditingBox {
public partial class Form2: Form {
public Form2() {
InitializeComponent();
Form1 F1 = new Form1();
F1.textBox1.Select();
F1.textBox1.Text = "F2:Running";
F1.textBox1.Refresh();
}
private void Form2_Load(object sender, EventArgs e) {
Form1 F1 = new Form1();
F1.textBox1.Select();
F1.textBox1.Text = "F2:Running";
F1.textBox1.Refresh();
}
}
}
You need to pass the Form1 this instance from the original form whenever you create it. Currently:
Form1 F1 = new Form1(); is creating a new instance of form1, not the instance which is displayed. Hence all you need to do is add a Form1 form1 to the constructor of form2 and call that constructor whenever you display it:
public Form2(Form1 F1)
{
InitializeComponent();
F1.textBox1.Select();
F1.textBox1.Text = "F2:Running";
F1.textBox1.Refresh();
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 F2 = new Form2(this);
F2.Show();
textBox1.Select();
textBox1.Refresh();
}
You can pass Form1 to Form2 constructor:
namespace EditingBox
{
public partial class Form2 : Form
{
Form1 _form1;
public Form2(Form1 form1)
{
InitializeComponent();
_form1 = form1;
_form1.textBox1.Select();
_form1.textBox1.Text = "F2:Running";
_form1.textBox1.Refresh();
}
private void Form2_Load(object sender, EventArgs e)
{
_form1.textBox1.Select();
_form1.textBox1.Text = "F2:Running";
_form1.textBox1.Refresh();
}
}
}
I have two forms named form1 and form2:
form1 is made of a label and a button.
form2 is made of a textBox and a button
When I click the form1 button, this will show up form2. Any inputs in textBox should be written back to form1.label once I hit the button in form2.
I have the code below but it doesn't work.
// Code from Form 1
public partial class Form1 : Form
{
public void PassValue(string strValue)
{
label1.Text = strValue;
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2();
objForm2.Show();
}
}
// Code From Form 2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 objForm1 = new Form1();
objForm1.PassValue(textBox1.Text);
this.Close();
}
}
And a screenshot:
How can I realize that?
You don't access your form1, from which you created form2. In form2 button1_Click you create new instance of Form1, which is not the same as initial. You may pass your form1 instance to form2 constructor like that:
// Code from Form 1
public partial class Form1 : Form
{
public void PassValue(string strValue)
{
label1.Text = strValue;
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2(this);
objForm2.Show();
}
}
// Code From Form 2
public partial class Form2 : Form
{
Form1 ownerForm = null;
public Form2(Form1 ownerForm)
{
InitializeComponent();
this.ownerForm = ownerForm;
}
private void button1_Click(object sender, EventArgs e)
{
this.ownerForm.PassValue(textBox1.Text);
this.Close();
}
}
Like mentioned in other posts, you won't be able to reference the original Form1 by creating a new instance of Form1. You can pass Form1 into Form2's constructor or expose Form2's text as a public property, but I usually prefer using delegates for this to maintain loose coupling.
// Code from Form 1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2();
objForm2.PassValue += new PassValueHandler(objForm2_PassValue);
objForm2.Show();
}
public void objForm2_PassValue(string strValue)
{
label1.Text = strValue;
}
}
// Code From Form 2
public delegate void PassValueHandler(string strValue);
public partial class Form2 : Form
{
public event PassValueHandler PassValue;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (PassValue != null)
{
PassValue(textBox1.Text);
}
this.Close();
}
}
When you are doing:
Form1 objForm1 = new Form1();
objForm1.PassValue(textBox1.Text);
... you are creating a new Form1 and calling the PassValue method on the wrong Form1 object. Instead, you could do:
public partial class Form1 : Form
{
// This is the text that will be entered in form2
public String form2text;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Show form2
Form2 objForm2 = new Form2(this);
objForm2.ShowDialog();
// When form2 is closed, update the label text on form1
label1.Text = form2text;
}
}
public partial class Form2 : Form
{
// This is the instance of Form1 that called form2
private Form1 form1caller;
public Form2(Form1 form1caller)
{
InitializeComponent();
this.form1caller = form1caller;
}
private void button1_Click(object sender, EventArgs e)
{
// Pass the textBox value to form1 before closing form2
form1caller.form2text = textBox1.Text;
this.Close();
}
}
I just tried this code and it works, sure it will help you.
in the first form (Form1) type below:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(textBox1.Text);
f.ShowDialog();
}
}
in the second form (Form2) use below codes:
public partial class Form2 : Form
{
public Form2( string st)
{
InitializeComponent();
textBox1.Text = st;
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
You could do this:
class Form2
{
public string ReturnedText = "";
private void button1_Click(object sender, EventArgs e)
{
ReturnedText = textbox1.Text;
Close();
}
}
and in form1
Form2 objForm2 = new Form2();
objForm2.ShowDialog();
string ret = objForm2.ReturnedText;
You should pass reference on form1 to form2 instead of creating new instance in this code:
private void button1_Click(object sender, EventArgs e)
{
Form1 objForm1 = new Form1(); // ← this is another form1, not that you see
objForm1.PassValue(textBox1.Text);
this.Close();
}
The way that I normally approach this requirement is as follows:
I place a public property on the Form2 class:
public string ValueFromForm1 { get; set; }
//In the constructor, or other relevant method, I use the value
public Form2()
{
form2LabelToDisplayForm1Value.Text = ValueFromForm1;
}
In order to return something to Form1, you need to add a public property to the Form1 class to receive the value, and then send a reference to the form to Form2, so that Form2 can set the value:
//Add reference property to Form2 class
public Form1 CallingForm { get; set; }
//Form2 can access the value on Form1 as follows:
private someMethod()
{
this.CallingForm.ValueFromForm2 = "Info coming from form 2";
}
then
//Add public property to Form1 class
public string ValueFromForm2 { get; set; }
//When Form2 is created, set the reference property
Form2 objForm2 = new Form2();
objForm2.CallingForm = this;
objForm2.Show();
Since Form2 now has a reference to the Form1 that created, there is no need to call new Form1() anywhere in Form2. All Form2 has to do is set the value on the reference, and then close itself.
This is what you are going to do:
// Code from Form 1
public partial class Form1 : Form
{
public string MyValue { get; set; }
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2();
objForm2.textBox1.Text = MyValue;
objForm2.MainForm = this;
objForm2.Show();
}
}
// Code From Form 2
public partial class Form2 : Form
{
public Form1 MainForm { get; set; }
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MainForm.MyValue = textBox1.Text;
MainForm.Show();
this.Close();
}
}
Form 1 code...:-
namespace Passing_values_from_one_form_to_other
{
public partial class Form1 : Form
{
string str;
private String value1;//taking values from form no _of_test_cases
public string value
{
get { return value1; }
set { value1 = value; }
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = str;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
str = f2.passvalue;
}
}
}
Form 2 code....:-
namespace Passing_values_from_one_form_to_other
{
public partial class Form2 : Form
{
private string str;
public string passvalue
{
get { return str; }
set { str = value; }
}
public Form2()
{
InitializeComponent();
}
private void Btn_Ok1_Click(object sender, EventArgs e)
{
passvalue = textBox1.Text;
this.Close();
}
}
}
directly execute it u will get the clear picture....same way u can pass values from one form to other...
post your comments if you face any issues...
hope this will help...
or else you can refer this video...
http://www.youtube.com/watch?v=PI3ad-TebP0