|
-
Sep 6th, 2006, 09:49 AM
#1
Thread Starter
Addicted Member
passing value between forms???
Ok guys.
I am using MDI
I have a variable containing a string in one form and I need to pass it into another form in order to populate a text field.
Both the forms are already displayed on screen, I have seen some examples of how to pass values but they all seem to create new form instances but in my case both forms are already displayed.
any idea how I do this?
-
Sep 6th, 2006, 11:13 AM
#2
Re: passing value between forms???
Code:
public partial class Form1 : Form
{
private Form2 frm;
public Form1()
{
InitializeComponent();
frm = new Form2();
}
private void button1_Click(object sender, EventArgs e)
{
frm.Label1Text = "Welcome";
}
}
public partial class Form2 : Form
{
private Label Label1;
public string Label1Text
{
get
{
return Label1.Text;
}
set
{
Label1.Text = value;
}
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Sep 6th, 2006, 03:50 PM
#3
Re: passing value between forms???
Passing data from one object to another comes down to one thing: one object must have a reference to the other. That's why the examples you see involve creating a new form: because that's when you have to pass the reference from the creator to the new form or else save a reference to the new form in a class-level variable of the creator. Once this link exists you can pass data whenever you want simply by setting a property or calling a method.
-
Sep 7th, 2006, 10:33 AM
#4
Thread Starter
Addicted Member
Re: passing value between forms???
will 'frm = new Form2();' not cause a new form to be displayed?
At the moment I have my main form and from the menubar I open a new form inside the main one(MDI). When I click a button in the sub form I want the value to be passed into a variable in the pre existing main form.
Does the sample given above allow this to happen.
It appears to create a totally new form instance and I don't really want this
-
Sep 7th, 2006, 11:37 AM
#5
Re: passing value between forms???
I wasn't talking about setting values before form displaying. changing public properties during run-time will allow you to do whatever you want
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Sep 7th, 2006, 11:52 AM
#6
Thread Starter
Addicted Member
Re: passing value between forms???
sorry I'm new to .net in general
can you give a code sample of how I go about changing the public properties for my situation described above?
-
Sep 7th, 2006, 12:15 PM
#7
Re: passing value between forms???
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Nov 2nd, 2006, 10:19 AM
#8
Thread Starter
Addicted Member
Re: passing value between forms???
I still am not fully understanding this code.
I have a form with filename form1.cs which contains just a menubar and a label, on its menu there is a menuitem that when clicked creates another form with fielname form2.cs which contains a textfield and button
since the menu item click event on form1 executes 'form2 f = new form2()'
I understand thats its easy for me to add a button on form1 that when clicked will access a textfield property on form2 and place the data in a label on form1. i.e form1label.text = Form2object.properyname
but form1 doesn't have a button so I want the label on form1 to be updated when the button on form 2 is clicked. Since both windows are currently already on screen (objects already created, so dont know how to call a property of form1 since I never created an object name for it) at this stage I cant see how it is possible for the clickevent on form2 to be able to place its textfield data into the label on form1.
am i thinking about this wrongly and/or stupidly? lol
Last edited by eakin; Nov 2nd, 2006 at 10:53 AM.
-
Nov 2nd, 2006, 03:05 PM
#9
Frenzied Member
Re: passing value between forms???
Form2 frm = new Form2();
frm.Text = "Easy";
What's so hard bout' it? I understand ur new but this is as easy as it gets
-
Nov 3rd, 2006, 03:19 AM
#10
Thread Starter
Addicted Member
Re: passing value between forms???
u've misudnerstood, where did I say I was looking to set any text on form2?
-
Nov 3rd, 2006, 05:03 AM
#11
Re: passing value between forms???
 Originally Posted by eakin
u've misudnerstood, where did I say I was looking to set any text on form2?
A property is a property, whether it's Text or something else. Other properties are set in exactly the same way.
-
Nov 10th, 2006, 01:28 PM
#12
Member
Re: passing value between forms???
Eakin,
Here's some code to try to explain what they're telling you.
On the second Form, you do 2 things.
1. create variables that can be seen over the scope of the whole form
string myVariable=""; // Any string
Form1 f1; // will be used to talk to Form1 from From2
To do this, you declare this variable right after the Form 2 class is started.
Like this:
Code:
public class Form2 : System.Windows.Forms.Form
{
string myVariable="";
Form1 f1;
etc...
2. create a Public function to open up From2 so you can pass a reference to it for the first form.
Like this:
Code:
public void showMe(Form1 f, string var1)
{
myVariable = var1; // Sets your form variable you can use over the whole for to the string you passed in
f1 = f; // Sets your Form2 f1 Form1 refence to = the form you pass in
this.Show(); // displays the form like normal
}
Now you have the string you passed to Form2 stored in the variable myVariable. So you can use it anywhere you want on Form2 now.
Like from a button
Code:
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("String from form 1 is: " + myVariable);
}
Now for Form1
If you want Form2 to change text in a control on Form1, you have to change that control to a public object.
A 'Public' control on one form can be accessed by other form.
By default, controls are created as private. So you'll have to go in and manually edit the control you want to access and change it to public.
So if you want to change a Label on From1 from Form2, you would have to change the word private to public for that control.
So, you find your control in code on the form. It will look like this:
private System.Windows.Forms.Label label1;
All you do is change it to public
public System.Windows.Forms.Label label1;
Now Form2 can see the Label1 on Form1.
Then on Form 1 you open Form 2, and pass it a reference to itself(Form1-which is already open) and any other variables you want Form2 to have - like a string - by calling the new Public function you created on Form 2.
Like this:
Code:
private void MenuItem_Click(object sender, System.EventArgs e)
{
Form2 frm2 = new Form2();
frm2.showMe(this, "whatever your string is"); // <--- This shows the form by calling your Public showMe() function on Form2
}
Back to Form2
So, now if you want to set a variable on Form 1 from Form 2, you do this on Form 2
Code:
f1.Label1.Text = "yadda";
Hope that helps.
-
Nov 12th, 2006, 11:16 PM
#13
Fanatic Member
Re: passing value between forms???
public static Form formvar = Form1;
In form 2
formvar .textbox1.text = "hello";
formvar .textbox2.text = "welcome";
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|