Hey I am kinda stuck here and could need a little help or reference.
I have one main form (Form1) and two sub forms (Form2 & Form3).
On Form1 is a Panel (Panel1) with two Buttons (Button1 & Button2).
If I click Button1 I want to display Form2 inside the Panel of Form1.
If I click Button2 I want to display Form3 inside the Panel of Form1.
I got that working already with the following code (Button1 Click):
Form2.TopLevel = false;
Panel1.Controls.Add(Form2);
Form2.FormBorderStyle = FormBorderStyle.None;
Form2.Dock = DockStyle.Fill;
Form2.Show();
now to my actual problem.
In Form1 I have booleans, integers and strings that I want to reach (read and write) from Form2 and Form3.
Since Form1 and either Form 2 or Form 3 are active I can't just use "Form1 form1 = new Form1;" since initializing a new form doesn't use the already created instance. It's a new form so it has the default values from start and not the changed ones from the active Form1 which I want.
So now I want to have a field in Form2 and Form3 to hold the active instance of Form1 so that I can change and read the values of the actual active Form.
How would I do that?
Pack all data you want to pass from form 1 in a class. ( define a class for this ).
You can define constructors for Form 2 and 3 and pass the instance of type you defined from form 1 to 2 and 3. That is one way of doing this.
Another way is to define properties for Form 2 and 3 and assign values before you call .Show() method. This is not a great idea if your form 2 and 3 can be used some where else and you may accidentally not assign the values to the properties.
Did you create instances of Form2 and Form3 in Form1? If Yes, just add an argument to the constructor of both sub forms.
UPDATED
// In Form1
public partial class Form1 : Form
{
private readonly Form2 _form2;
public Form1()
{
_form2 = new Form2(this);
InitializeComponent();
}
}
// In Form2
public partial class Form2 : Form
{
private readonly Form1 _form1;
public Form2(Form1 form1)
{
_form1 = form1;
InitializeComponent();
}
}
Related
My problem is:
Main form is opened first, there is a button to open Child form. I use Constructor and I want return a string from Child form to the textbox on Main form, and my code now is:
Form1 f = new Form1(txt1.Text);
f.Show();
But, a new main form will open up, the string won't fill in the first main form.
So how to work with only one main form?
You can do this, get it via OpenForms:
Form1 frm = Application.OpenForms.OfType<Form1>().FirstOrDefault();
Make sure you declare your control txt1 as public on its Modifier property so that you can access it from Child form:
Form1 frm = Application.OpenForms.OfType<Form1>().FirstOrDefault();
frm.txt1.Text = "Change";
frm.Show();
Now if you are trying to change it via its constructor, I doubt you can do it since it was already initialize, unless you call another new Form1 to initialize it and will go through its constructor again.
What you can do is change its property directly:
Form1 frm = Application.OpenForms.OfType<Form1>().FirstOrDefault();
frm.stringMain = "Foo"; //Your property you want to change
frm.txt1.Text = "Change";
frm.Show();
The plan:
I want to ask the user with second Form2 to input some text.
When this Form2 is closed, i want to display the input text in a textbox on Form1...
On a button event, on Form2 i can reach Form1's textbox:
Form1 form1 = new Form1();
And:
form1.myText = "Test Name";
And then i close Form2:
this.Close();
But the value "Test Name" does not appear in form1's textbox...
I don't get an error.
When you call new Form1(), then new instance of Form1 is created. You have two objects of Form1. That's why your code doesn't work.
If you want to make it fast, add Form1 as a variable to Form2 class.
public Form1 form1;
Then you can set it just before showing Form2.
Form2 form2 = new Form2();
form2.form1 = this;
form2.Show();
Remember to remove this part: Form1 form1 = new Form1();.
Now your code should work.
Create event handler on Form2 and when open Form2 from Form1 hook on this event. In form2 fire this event with your own eventargs which contains text which u need to show in Form1.
Another solution is Action. Create property Action in Form2 and when opened Form2 set this action. When Form2 is closed fire this action like _action.Invoke(textWhichUneed);
You should make assign value before close. For example
form1.myText = "Test Name";
this.Close();
Also form1 shouldn't be created, but passed. How do you pass form1 into form2?
This question already has answers here:
Passing a value from one form to another form
(9 answers)
Closed 9 years ago.
in windows application I have 2 forms: form1 containing a TextBox. I just want to use the value of the form1's textbox in form2, so in form2 I just create a object of form1 and try to access the value of textbox by writing the code:
form1 f1=new form1();
string value=f1.textbox1.text
...but the value is not coming.....please help me
Creating a new instance of Form1 will not work as it a new instance, not the one you were using. As the comments above suggest, the best option would be to create a custom constructor for Form2 and accept the textbox value there.
public Form2(string form1TextBoxValue)
{
this.ValueFromForm1 = form1TextBoxValue;
}
Therefore in Form1 you can use the value in Form2 when you instantiate it:
Form2 form2 = new Form2(this.textBox.Text);
Alternatively, you could pass the Form1 instance to Form2. This way you can access all the Form1 values if needed. This does mean that Form2 has a direct dependence on Form1 which should be avoided.
// In Form2
public Form2(Form form1Instance)
{
this.Form1Instance = (Form1)form1Instance;
}
// In Form1
Form2 form2 = new Form2(this);
Suppose I have two forms: Form1 and Form2. Form2 has a text control named text1
In VB6 I could write to Form 2's textbox
control from Form1 by using: Form2.Text1.text = "Some text here"
How can I make this work in C#? I have tried everything!
What I have tried:
Form2 Frm2 = new Form2();
Frm2.show();
Frm2.Activate(); // Trying everything to make sure it sees the form (it does).
Frm2.Text1 (Doesn't find the control)...
ANSWER:
I ended up making a public routine in Form2, and then just calling this routine from form1. In this public routine of Form2 I would then call the textbox!
I believe all form elements are private by default. So you're going to have to switch their definition to public (in your .designer.cs) or make a public getter and setter for each textbox you want to access.
You need to keep a reference to the second form in the first form.
// form1 code
// variables
Form2 myForm2;
// Form1_Loaded event handler
myForm2 = new Form2();
myForm2.Show();
// place where you want to change text on form2 from within form1
myForm2.Text1.Text = "Some text here";
You have to have a reference to the instance of Form2 in order to write to it. Pass the reference to the instance of Form2 to Form1 and then manipulate it like you are expecting. You can pass this instance in via the constructor or you can pass it in later. You can also store the instance reference in a central location. Just be sure that you don't create a new instance of Form2 when you try to access the instance. For example, if you put the following code in your form:
Form2 myInstance = new Form2();
then you will be creating a new instance of Form2, which probably isn't what you want.
My goal was add text to a Text Box on another form. I had Form1 and Form2. Form2 has a text box control named Text1. In order to make this work, I created a Sub Routine:
public Void WriteToText(string sData)
{
// Here is where I wrote to my textbox
Text1.text = sData;
}
Form 1 code:
Form2 Frm2 = new Form2();
Frm2.WriteToText("My Data");
I have Three Forms as below:
Form1
Form2
Form3
The Form1 and Form3 are communicating with Form2. I want If Form1 visited to Form2 than
Form1 Should leave it’s identity to Form2’s textbox1. If Form3 visited to Form2 than Form3
should leave it’s Identity to Form2’s textbox2.
Is it possible?.
Here is Visted Means as below.
By Form1
Form2 f2= new Form2();
f2.Show();
By Form3
Form2 f2= new Form2();
f2.Show();
And Identity Like "Form1" and "Form3"
I believe you are asking for the form that opens Form2 to set some specific value to the textbox. You could have a property on Form2 that sets the textbox text.
public partial class Form2 : Form
{
/*.......*/
public string Identity
{
set
{
textbox1.Text = value;
}
}
}
Then in Form1 or Form3:
Form2 form2 = new Form2();
form2.Identity = this.Name;
form2.Show();
The best thing to do is overload the Show method for each of your forms to accept something like a "previous form" parameter that will serve as a reference to the caller (you could do this either as a Form object, whose properties you could then access, or simply as a String). Then, wherever you show a second form from an existing form, you pass information about the existing form into the Show method for the second form. The second form's Show method would take care of updating its textbox with the name of the form that displayed it.
For example, each form would contain an overloaded Show method:
public void Show(Form previousForm)
{
//Set the textbox on this form to contain the name of the calling form
myTextBox.Text = previousForm.Name;
//Call the base class's method to show this form
base.Show();
}
And you would show one form from another by calling its overloaded Show method:
private void ShowOtherFormButton(object sender, EventArgs e)
{
//Create a new instance of the form you want to display
Form2 myOtherForm = new Form2();
//Show the other form, passing the calling form as a parameter
myOtherForm.Show(this);
}
This way, no form is responsible for or even allowed to update controls contained by another form. You don't have to expose any additional properties or remember to call any additional methods on the forms you want to show—it's built right into the Show method that you have to call anyway.
Alternatively, you could overload each form's constructor in the same way, but that won't work if you want to be able to move between existing instances of each form. You'll only be able to specify information about the previous form when you're creating a new instance of each form.
You will need a reference to the caller. You can do this by overriding the constructor of the Form.
public partial class Form2 : Form
{
Form _caller;//Caller form
//Default constructor
public Form1()
{
InitializeComponent();
}
public Form1(Form caller)
{
InitializeComponent();
this._caller = caller;
textbox2.Text = caller.Name;
}
}
All you have to do now is call Form2 using the new constructor:
Form2 form2Instance = new Form2(this);
form2Instance.Show();
Whichever form will call Form2, will now have it's name written on textbox2 of Form2
You need to pass data between the window forms(ie.,the name of the form in this scenario)so that you can get it in Form2.
Refer the lint to pass data between window forms: http://www.vbdotnetheaven.com/UploadFile/thiagu304/passdata12262006073406AM/passdata.aspx