I made my textboxes on form1 public. I was able to access them through my form2, after I made a statement like form1 form2 = new form1();
Problem is, the text boxes are all empty on form2. Is this because I initiated a new form1 and therefore the textboxes are set back to their default as empty How can I access variables stored in form1 from form2 thanks.

passing variables to form
Howart
In your first post, you had:
form1 form2 = new form1();
Now you have:
Form1 NewForm = new Form2();
Can you see why I'm confused Anyway, I think maybe what you're trying to do is open Form1 from Form2 and grab data from Form1 for use on Form2, is this correct For that, you'd need to use a modal form (which is simply using .ShowDialog()), something like this:
// somewhere in Form2's code
Form1 o = new Form1();
if (o.ShowDialog() == DialogResult.OK)
{
// do something with o.TextBox1.Text
}
Srock
To have one form talk to another, pass the Form as an argument
< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
In class Form2, write a method like
Public Method(Form myForm1) {
myForm1.TextBox1.Text = “Hi”;
}
Ian Espiga
Davidkapp
tbrierly
im starting to wonder about this language. I have another question maybe you can help me with now. I tried going about things differently and using a seperate class to hold variables, so that the other forms could access them equally. I made a public class and defined a string value in that class, the class is calles Open().
in that I defined a String named strTotal. In my form I created an instance like this:
Open P = new Open();
then set P.strTotal = to one of my textboxes.Text. Thing is, when I go into another form and tried to look at P.strTotal.Text, it shows up as empty! Same freakin problem Im having with trying to pass variables to forms. What am I doing wrong and why does P.strTotal.Text show up as something in one form and empty in another! Is it because I created a new instance, and therefore the class is run from the start, and the variable P.StrTotal is initialized again, therefore set to nothing If that is the case, how in the world can one pass data between forms and classes What am I missing here Thanks again.
Yury_A
Go back and re-read my post where I said to open Form2 with a .ShowDialog().
Form1 never closes, Form2 opens up modally on top of Form1 and when Form2 closes, the Form1 code can then do whatever needs to be done.
Matt Waldron
RichardGreenwell
OK, I think we need to step back a moment here and you need to explain a few things:
1) Why would Form2 make Form1 visible From what you posted earlier, it sounded like Form1 has already come and gone, after data had been entered into it.
2) It's not hard to pass data to Form2 (you can pass parameters in the constructor), but I have no clue how you're calling either form ... are they related at all does one call the other are they opened from a menu selection are they opened from a button click
The answers to these questions determine the best way to handle this situation.
GreyW63
Form1 is the main Form in my program. Form2 is opened from Form1 whenever the user clicks the "OPEN" button. Form2 opens, and displays a list of saved files that the user can open. I wanted then, Form2 to tell Form1 what to display, according to what file the user wants opened. Rigth now I just write that info to a .txt file, and when Form1 opens I have it look for that file, and if it exists, it reads it and displays the proper info. Make sense
I dont mind writing everything to .txt file, but if I could just pass info between forms, it would make life easier. So, yes Form2 is opened from Form1 by way of a button click. Then, after Form2 is done, Form1 should open again, or Re-Load, with the new information to display. Is it possible to tell a form that is open to simply reload Or, is it possible for Form2 to open a new instance of a Form1, and at the same time Close another instance of Form1 that is already open I just want Form1 to reload according to what Form2 tells it to reload with, and I would like Form1 to remain viewable until Form2 opens a new Form1, and closes the old one. I hope this is making sense.
How do you pass variables to a new form.. like this
Form2 OpenForm = new Form2(variable, variable,variable);
Form2 is not an inherited form, its just a regular form, if thats what you are asking. Once again, thanks for the help. I am new to C#, and .NET in general, so your help is greatly appreciated.
patridge
In Form1, I allow the user to input a few text boxes that are used in some calculations. I want to be able to access that data stored in Form1 from my Form2. I tried something like this:
Form1 NewForm = new Form2();
NewForm.txtbox1.Text.....
and then I was able to access the text box data from Form1. The problem is, they are all empty. I tried accessing a public string array from Form1, and the same thing, it is empty when I try to access it from Form2. What the heck is going on here! Thanks for the help.
yarini
So, Form1 is not available and not even open when you're in Form2 If that's the case, then yeah, you're gonna have to save the info somewhere, it's just a matter of where.
Besides saving it to a file (which is ok to do), another option is to save it elsewhere in your app ... a static class maybe, something like that.
VirginiaA
Yeshia,
I know exactly what you are talking about. I am having the same issue! In VB.NET it is very easy to accomplish what you are wanting to do, but in C# for some reason it is not the same procedure. I have found a work around that will work for most of your situations.
On form1 what you can do is make global public variables that will store the data from the text fields. By declaring then public static your other form will be able to access them. So here is my example on my form for a snipet of code I have.
FORM 1
public static int loadIdHospital; public static string idHospital; public static int myIdHospital; public System.Windows.Forms.MenuItem mnuToolsDelete; public static string hospitalName; //this is the name of the hospital from the surgery form that the user selected. public frmHospital(){
// // Required for Windows Form Designer support //InitializeComponent();
// // TODO: Add any constructor code after InitializeComponent call //}
Notice my delcared variables at the top of the form.
So when I come to form2 and want to access the data from form 1 I would simply do this.
labelHospital.text = form1.idHospital;
And now the label will display the value stored in that variable from form 1.
Hope this helps!
~zero
suparba
Ooops, just went back and read that myself ... I think it was assuming it the other way around. Let me do the code again.
The button on Form1 would open Form2:
Form2 oForm = new Form2()
if (oForm.ShowDialog() == DialogResult.OK)
{
// Do stuff to show new stuff on Form1 based on
// public properties that are available still from oForm (Form2)
}
Refresher