need some explanation with referencing controls on other forms

M

Milsnips

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???

thanks,
Paul
 
S

Stoitcho Goutsev \(100\)

Milsnips,

By default the class members generated for controls are private. To set the
text in the label you can:
- make its member public (or internal), which I wouldn't suggest.
- have a callback( or event) called by the dialog when the label needs to be
changed. The dialog should pass the text in a paramemter to the callback.
- have a public property on the level of the main form which when set with a
text trasnfers the text to the label.
 
B

Bruce Wood

Milsnips said:
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.statusTextBox.Text; }
set
{
if (value == null) throw new
ArgumentNullException("value");
this.statusTextBox.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;
}
}
....
 

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