Getting Form2 variable to Form1 (User text input in TextBox) - c#

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?

Related

Open Form1 as new instance after successful operations on Form2

I am having two forms as Form1 and Form2 on Form1 button click I am opening the form2 as follows
private Form2 form2;
this.Hide();
form2.ShowDialog();
this.Show();
I have a back button and submit button on Form2 when I click on Back this is how I am showing Form1
this.Hide();
this.DialogResult = DialogResult.OK;
But when I submit the form I need to load the Form1 with new instance, this is what I tried but Form1 is showing multiple times
if (DialogResult.OK == MessageBox.Show("Installation was done", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information))
{
Form f1 = new Form ();
f1.Show();
this.Close();
}
When I execute my application this is how it looks after entering data to my textbox
After clicking on button1 it will show the data in Form2 if user wants to modify data he can come back if he clicks on submit I would like to have the new instance of Form1
What you need to do is after you open form 2 do something like
Form1.close();
and then when you are done with the operation:
Form1 form1 = new Form1();
form1.ShowDialog();
this.Close();
Ok if you want to save the data from form1 then i would either fave the data as a file that you could retrieve when ever of pass all of the variables you want to save into Form2 and then save them there. also instead of using a new form 1 you could use the existing one and reset any values that you want to reset.
I hope this helps.
Here is the code I tried, on your Form1 button click event write the following
private void button1_Click(object sender, EventArgs e)
{
form2.ShowData(textBox1.Text);
form2.ShowDialog(this);
}
In your Form2 submit click have the following code
private void button2_Click(object sender, EventArgs e)
{
this.Owner.Hide();
this.Owner = new Form1(this);
this.Hide();
this.Owner.Show();
}

MDI Child Forms Opening each other with the same parent form

So I have 3 Forms, lets call them Form1, Form2, and Form3.
I have sent the IsMDIParent Property to true for Form1.
When I launch the app, It loads Form2 as an MDI Child using
Form2 frm = new Form2();
frm.MdiParent = this;
frm.Show();
And that works fine. What I then want to do is click a button withing the 2nd form that will close Form2 and open up Form3 as a child form of Form1.
I tried
SecondForm SecondFormMDI = new SecondForm();
SecondFormMDI.MdiParent = Form1;
SecondFormMDI.Show();
on the button click event in Form2, but it would not work.
Do I have to always launch a Child form from the parent form? and if so, how would i go about doing that when it is on the button click event on a child form?
Just use this.MdiParent, instead of Form1, like
SecondForm SecondFormMDI = new SecondForm();
SecondFormMDI.MdiParent = this.MdiParent;
SecondFormMDI.Show();
You can set the MDIParent of any form in design time, why do it in run-time?
Simply set the value of MDIParent property of Form2 and Form3 to Form1, and that's it.
You could create a method in your MDIForm to open a childform:
public void OpenForm(Form form)
{
form.MdiParent = this;
form.Show();
}
When you want to open a new form in another form you do something like this (example in ChildFormOne with button):
private void btnOpenChildFormTwo_Click(object sender, EventArgs e)
{
((MDIForm)this.MdiParent).OpenForm(new ChildFormTwo());
this.Close();
}
Hope this helps.
ChildForm frmChild = new ChildForm();
frmChild.MdiParent = this.MdiParent;
frmChild.Dock = DockStyle.Fill();
frmChild.Show();

How to make a second form appear on top of the main form at start?

I have 2 forms; My main Form named Form1 and my second Form named Form2
My main form shows up at start, and I'd like to show Form2 too but it shows up under Form1.
And I want it to show up on top of my main form.
I've tried to set Form2's TopMost property to true then false but it didn't work.
I also tried to create a different Thread for Form2 to appear after Form1, in this case Form2 shows up quickly then disappear.
Show Form2 in Shown event handler of Form1:
private void Form1_Shown(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
You can use ShowDialog() instead of Show() if you want Form2 to be modal.
Add this code at Form1 load event:
Form2 form2 = new Form2();
form2.TopMost = true;
form2.Show();
new Form2().ShowDialog()
This will also lock Form1 until Form2 gets closed

Writing to a textbox on a separate form (C#)

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

Fill Identity Of WinForms Into Textboxes

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

Categories

Resources