|
-
Feb 1st, 2008, 05:34 PM
#1
Thread Starter
Lively Member
[2.0] Pass data between forms?
Hello!
I have a small problem and I could use some help to solve it.
I wish to pass information which is entered in two textBoxes on form2 which are then somehow passed on to two strings in form1. I have looked at this "http://www.c-sharpcorner.com/UploadFile/thiagu304/passdata05172006234318PM/passdata.aspx" but I can't figure out how to assign information to variables. Would appreciate some help on this, thank you!
Cheers,
Zolomon
-
Feb 1st, 2008, 05:41 PM
#2
Re: [2.0] Pass data between forms?
Possibly the easiest way to do it:
In Form2, declare two strings as public variables.
In Form1, when you need the info from Form2, call its' ShowDialog method. This ensures that Form1 will stop all processes until Form2 is hidden.
In Form2, I'm assuming there's a button to confirm the info in the text boxes. When you click this button, assign the two strings the values of the text boxes, then call Hide().
In Form1, after ShowDialog(), you should be able to access those variables as members of Form2.
-
Feb 4th, 2008, 03:46 AM
#3
Re: [2.0] Pass data between forms?
The question of how to pass data between forms comes up a lot here. It's also an indication of bad programming practice.
There are two primary types of form: application windows and utility windows.
Application windows stay on-screen all the time; they are where the user does their work.
Utility windows are things like floating toolboxes and dialogue boxes. Your question involves dialogue boxes.
Forms should not contain application logic.
The bulk of the application should be contained in separate classes. Code in forms should serve only to facilitate what appears in the form. This is a 'good practice' rule.
A dialogue box is an object, but think of it like a function. It takes data from the user and returns it to the application.
Now, just as it's bad practice to go from function to function in some kind of long chain, it's also bad practice to pass data straight from form to form. A dialogue box should be invoked from a place in your application logic, and return to that place once the user has inputted the necessary values. Functions should do only one thing, but do it well.
Best practice:
Call the dialogue box form by using its ShowDialog method. In the dialogue form logic, set the DialogResult property to return a general value (such as OK, or Cancel, or so on) and expose the actual data input as properties. You can then access the inputted values by querying the properties once control has returned to your application logic.
Here is an example:
Code:
// Create and show a preferences dialog (for example)
// A 'using' block will automatically dispose of the object when the block ends
using (PrefsDialog pd = new PrefsDialog())
{
DialogResult result = pd.ShowDialog();
// Only save changes if the user clicked OK
if (result == DialogResult.OK)
{
// Save the preference values
MyProgramSettings.Save("ExamplePref1", pd.ExamplePref1);
MyProgramSettings.Save("ExamplePref2", pd.ExamplePref2);
}
}
You should be able to work out what to write in the dialogue class.
I realise this probably isn't the answer you wanted. However, experience has taught me that this is the best approach to dealing with forms. Granted, timeshifter's example will work, and is 'easy', but is also a bad habit to get into. Develop good habits now and you will get less burnt the first time you try coding any application of non-trivial scale.
-
Feb 4th, 2008, 07:37 AM
#4
Thread Starter
Lively Member
Re: [2.0] Pass data between forms?
Penagate, thank you for you informative answer.
I have a question though, how would I create a dialog box? Do I create a form and call it as a dialog or how would I go about it? And the "MyProgramSettings", is that a class? I don't really understand how the Save method works, or where I can use it.
Thank you!
Cheers,
Zolomon
-
Feb 4th, 2008, 09:25 AM
#5
Re: [2.0] Pass data between forms?
A dialogue box is just a form like any other. You can invoke any form as a dialogue by calling its ShowDialog method. However you should write some code to set the DialogResult property otherwise there is little point in using ShowDialog.
Code:
private void okButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
-
Feb 4th, 2008, 09:29 AM
#6
Thread Starter
Lively Member
Re: [2.0] Pass data between forms?
Ah! At the moment I get a weird error when I try to use that, if I recall correctly the error says that I already have a ShowDialog instance running, or at least something similar to that.
Could you please explain what you did with the .Save() method, and how it works?
Thanks,
Zolomon
-
Feb 4th, 2008, 09:58 AM
#7
Re: [2.0] Pass data between forms?
 Originally Posted by Zolomon
Ah! At the moment I get a weird error when I try to use that, if I recall correctly the error says that I already have a ShowDialog instance running, or at least something similar to that.
ShowDialog causes the form to be displayed modally, which blocks access to other windows in the application. As such, you can only have one modal form open at any one time.
 Originally Posted by Zolomon
Could you please explain what you did with the .Save() method, and how it works?
That was was just a fictional example. (I couldn't think of a better one at the time.)
.NET 2.0 actually provides its own facility for saving application settings.
Using Settings in C#
-
Feb 4th, 2008, 05:00 PM
#8
Lively Member
Re: [2.0] Pass data between forms?
I am sure this has been said already.
You can always pass the desired variables to the new form on the constructor...
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
|