Milsnips wrote:
Hi there,
i have a test application with 2 forms - frmMain and frmDialog
the application starts with frmMain (which has a label on it called
lblStatus), and when i click a button, it opens the dialog where i can type
some text. What i cant figure out is while i'm in the dialog, i want to set
the main form's label to the text i typed in my dialog form.
any help appreciated.
when i type my referenced form "fMain" - eg. fMain. , it doesnt show me any
of the controls on the form???
This is a common question, and it comes up because most people
(including me) do stream-of-consciousness coding: write a main form,
write a dialog, then realize that the dialog needs something from the
main form... how does it get it?
The solution becomes much easier if you think about it the other way
around: how does the main form pass the dialog what it needs? How does
the main form get information back from the dailog? (As opposed to
having the dialog push the information back to the main form on its own
initiative.)
Create a property in the dialog for the needed information. You said
that your text is a status of some kind. You could then do this:
public class MyDialog : System.Windows. Forms.Form
{
... the usual stuff ...
public string Status
{
get { return this.statusText Box.Text; }
set
{
if (value == null) throw new
ArgumentNullExc eption("value") ;
this.statusText Box.Text = value;
}
}
}
Now, in your main form, you can fetch the status from the dialog and
populate the status label:
....
using (MyDialog dlg = new MyDialog())
{
if (dlg.ShowDialog () == DialogResult.OK )
{
this.lblStatus = dlg.Status;
}
}
....