Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 10th, 2008, 06:06 PM
Friend of Wrox
 
Join Date: Sep 2007
Location: , , .
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default 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?



Reply With Quote
  #2 (permalink)  
Old September 10th, 2008, 08:24 PM
Friend of Wrox
 
Join Date: Sep 2007
Location: , , .
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

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

Reply With Quote
  #3 (permalink)  
Old September 11th, 2008, 08:00 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,413
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Could you describe what you did for the benefit of someone else encountering this question?

-Peter
compiledthoughts.com
Reply With Quote
  #4 (permalink)  
Old September 11th, 2008, 09:09 AM
Friend of Wrox
 
Join Date: Sep 2007
Location: , , .
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

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

Reply With Quote
  #5 (permalink)  
Old September 11th, 2008, 05:51 PM
Friend of Wrox
Points: 1,104, Level: 13
Points: 1,104, Level: 13 Points: 1,104, Level: 13 Points: 1,104, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 224
Thanks: 0
Thanked 30 Times in 29 Posts
Default

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
Reply With Quote
  #6 (permalink)  
Old September 11th, 2008, 10:57 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,413
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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
Reply With Quote
  #7 (permalink)  
Old September 12th, 2008, 01:07 AM
Friend of Wrox
 
Join Date: Sep 2007
Location: , , .
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

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?
Reply With Quote
  #8 (permalink)  
Old September 12th, 2008, 01:24 PM
Friend of Wrox
Points: 1,104, Level: 13
Points: 1,104, Level: 13 Points: 1,104, Level: 13 Points: 1,104, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 224
Thanks: 0
Thanked 30 Times in 29 Posts
Default

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.
Reply With Quote
  #9 (permalink)  
Old September 12th, 2008, 05:12 PM
Friend of Wrox
 
Join Date: Sep 2007
Location: , , .
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

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

Reply With Quote
  #10 (permalink)  
Old September 12th, 2008, 06:25 PM
Friend of Wrox
 
Join Date: Sep 2007
Location: , , .
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

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

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows Forms sambathrajmca Windows Workflow 1 November 27th, 2007 05: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 10:58 AM
windows forms chandrasekhar ASP.NET 2.0 Professional 2 February 27th, 2006 09:41 PM
Windows forms eyan C# 1 July 1st, 2004 04:10 PM



All times are GMT -4. The time now is 03:24 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.