 |
| C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

September 10th, 2008, 06:06 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
|
|
How to access values from different windows forms?
Hi
I have a form(lets call it form1) with a textbox on it. I also have another form(lets call it form2) and to keep thing simple I have for form2 to a textbox.
Now both of them are open and I want to take what is in the form1 textbox and place that text in the form2 textbox.
How would I do this?
|
|

September 10th, 2008, 08:24 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
|
|
Quote:
quote:Originally posted by chobo2
Hi
I have a form(lets call it form1) with a textbox on it. I also have another form(lets call it form2) and to keep thing simple I have for form2 to a textbox.
Now both of them are open and I want to take what is in the form1 textbox and place that text in the form2 textbox.
How would I do this?
|
Solved
|
|

September 11th, 2008, 08:00 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Could you describe what you did for the benefit of someone else encountering this question?
-Peter
compiledthoughts.com
|
|

September 11th, 2008, 09:09 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
|
|
Quote:
quote:Originally posted by planoie
Could you describe what you did for the benefit of someone else encountering this question?
-Peter
compiledthoughts.com
|
Sure but this site will do a better job :)
http://www.codeproject.com/KB/cs/pas...een_forms.aspx
|
|

September 11th, 2008, 05:51 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
|
|
Suppose form1 has an integer named myValue you want to pass to form2. The cleanest way to do this is:
(Code in form1 that invokes form2):
form2 secondForm = new form2(myValue); // Line 1
secondForm.ShowDialog(); // Line 2
The first line calls the constructor for form2, but passes in the value you wish to use in the second form. Because the default constructor has no arguments, C# knows a "non-default" constructor should be used. The second line simply makes the form visible.
Your constructor code in form2 might look like:
public class form2
{
// some additional code, probably
int passedInValue;
public form2(int valueFromForm1)
{
passedInValue = valueFromForm1;
}
// remainder of form2 code...
}
The end result is that passedInValue now has been assigned the value from form1 at the time form2 is instantiated.
If you need to pass in multiple values, just create a parameter list for the contructor. If you need to pass in a bunch of similar data type values, I'd suggest using an array in place of valueFromForm1 above.
In an unabashed plug for my book, page 392 tells how to call another form.
Dr. Purdum
|
|

September 11th, 2008, 10:57 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Another approach which greatly simplifies the job of synchronizing two forms is to employ the model-view-controller (MVC) pattern (really just the model-view part of it).
Create a class representing the data model (the M of MVC) with the properties you need. Then give each form (the V of MVC) the same instance of the model. Then you can utilize simple data binding to link the textbox text property to a property of the model. You can actually do this with minimal code. Since the data binding handles the updates to and from the textbox and model property, you barely have to write anything.
Here's a zip with a complete example of the simplest model-view sample I could create:
http://www.geekdork.com/samples/zips...tMVCSample.zip
One thing to note with the sample: you have to specify the "Data Source Update Mode" of the databinding to "OnPropertyChanged" otherwise the value won't be updated on the model until after you leave the textbox. This can result in strange behavior. With it configured properly, you can spin up many instances of the same exact view, change the text in one textbox and every other textbox will update accordingly, staying synchronized.
-Peter
compiledthoughts.com
|
|

September 12th, 2008, 01:07 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
|
|
Quote:
quote:Originally posted by DrPurdum
Suppose form1 has an integer named myValue you want to pass to form2. The cleanest way to do this is:
(Code in form1 that invokes form2):
à à à form2 secondForm = new form2(myValue);à à à // Line 1
à à à secondForm.ShowDialog(); à // Line 2
The first line calls the constructor for form2, but passes in the value you wish to use in the second form. Because the default constructor has no arguments, C# knows a "non-default" constructor should be used. The second line simply makes the form visible.
Your constructor code in form2 might look like:
public class form2
{
à à à // some additional code, probably
à à à int passedInValue;
à à à public form2(int valueFromForm1)
à à à {
à à passedInValue = valueFromForm1;
à à à }
à à à // remainder of form2 code...
}
The end result is that passedInValue now has been assigned the value from form1 at the time form2 is instantiated.
If you need to pass in multiple values, just create a parameter list for the contructor. If you need to pass in a bunch of similar data type values, I'd suggest using an array in place of valueFromForm1 above.
In an unabashed plug for my book, page 392 tells how to call another form.
Dr. Purdum
|
I think I spoke too soon when I said I got it. I got another problem. Now I need to pass a value back to form1 from form2.
So what I got is form1 passes a value to form2. Once form2 is done using the value from form1 it return the result value to another textbox on form1.
I am not sure how to send it back now. Can I do what you where just stating above?
|
|

September 12th, 2008, 01:24 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
|
|
Actually, you can use pointers in C#, but they must be defined as "unsafe" to be used. Also, that approach might be less portable.
Perhaps the easiest way is to pass in an array of values as the argument to the constructor and then update the array in form2. Because the lvalue of the array in form1 is passed to form2, the new values you assign to the array in form2 will exist when control returns to form1. If you don't know about lvalues and rvalues, I cover it in my C# book or you can read about it in Dr. Dobbs at:
http://www.ddj.com/cpp/208808373?cid=RSSfeed_DDJ_All
I hope this helps.
|
|

September 12th, 2008, 05:12 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
|
|
Quote:
quote:Originally posted by DrPurdum
Actually, you can use pointers in C#, but they must be defined as "unsafe" to be used. Also, that approach might be less portable.
Perhaps the easiest way is to pass in an array of values as the argument to the constructor and then update the array in form2. Because the lvalue of the array in form1 is passed to form2, the new values you assign to the array in form2 will exist when control returns to form1. If you don't know about lvalues and rvalues, I cover it in my C# book or you can read about it in Dr. Dobbs at:
http://www.ddj.com/cpp/208808373?cid=RSSfeed_DDJ_All
I hope this helps.
|
Could you be so kind and make a simple example?
I don't really get that article and I don't have your book(I don't even know what is it called).
It does not have to be anything special. Just one form with 2 textboxes and a button. When you click the button the new form shows up and the values(in this case you can just send one value as long as it is the same as send more then one) is sent to the second form.
Once there you can just have another button that just sends back say the value from textbox1 to textbox 2.
Thanks
|
|

September 12th, 2008, 06:25 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
|
|
Hi
I think I found a great way to do it. I gave ownership of form2 to form1. This works pretty good.
However I would like to see your way if you have time just incase this way would not work for whatever reason. I even would love to see how to do that mvc way too.
Here is the tutorial I found. I am not sure if there are any real big down sides of changing ownership.
http://forums.microsoft.com/msdn/Sho...iteID=1&mode=1
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| Windows Forms |
sambathrajmca |
Windows Workflow |
1 |
November 27th, 2007 06:25 AM |
| windows forms |
lakshmiR |
.NET Framework 1.x |
1 |
August 31st, 2007 08:32 PM |
| Help with Windows Forms |
tycotrix |
C# |
1 |
January 16th, 2007 11:58 AM |
| windows forms |
chandrasekhar |
ASP.NET 2.0 Professional |
2 |
February 27th, 2006 10:41 PM |
| Windows forms |
eyan |
C# |
1 |
July 1st, 2004 04:10 PM |
|
 |