C# 2008 aka C# 3.0Discuss 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 .
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.
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.
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.
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:
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.
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?
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:
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:
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.
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.