how to get information back to the calling form

J

jhs

Hello,

i'm developping a windows.forms app, using C#.

form a form (Form1), i want to call a second form (form2)

I want to use form2 as a generic form to set colors, fonts, .... for many
types of labels included in form1.
So the question is:

how can i call Form2 with initial value (get from calling label in Form1),
and when user validate form2 setting get back to Form1 with validated values
from Form2?

Thanks for your help
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi jhs,

In your case it sounds like you need modal form.
Create a form, add some properties for the data, you can also add some nicer
and useful constructors.
Then create an instace of the form, initialize it and show it via ShowDialog
method.

In the form class when you want to cose it don't call 'Close' method.
Instead set the DialogResult property with appropriate value from the
DialogResult enum. This will close the dialog, but wont disposit, so the
caller can read some properties and get the values. Once done with the form
the caller should dispose it (even though it doesn't have to)

So the code for using the form would look somethig like

using(MyDialog dlg = new MyDialog(<some params>)
{
//some more initilalization
//The caller will block on that call until dismiss thedialog
if(dlg.ShowDialog() == DialogResult.OK)
{
//use dialog values
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top