Calling a forms control from anther class???????

  • Thread starter Jon S via DotNetMonster.com
  • Start date
J

Jon S via DotNetMonster.com

Hi all,

This should be simple. I have a form called frmInterface. On this form I
have a label called lblStatus. I want to change the TEXT of this label from
another class.
For example
nsInterface.frmInterface objTemp = new nsInterface.frmInterface;
objTemp.lblStatus.text = "etc etc";

Both the frmInterface class and lblStatus control are public.
When I run the program the text in lblStatus doesnt change - why???????

Thanks in advance.
 
K

Kevin Spencer

Public members are instance members. In order to manipulate a public member
on an instance, the class must have access to the instance in which it
resides.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 
J

Jon S via DotNetMonster.com

Hi Kevin,

Thanks for replying. I'm not quite getting it. From what I can see
everything is public so therefore should have access-am I right in thinking
this?????

BTW the line of code where I create an object for the nsInterface.
frmInterface objTemp = etc... does have () at the end of it. I left this
out in the initial message.

Thanks.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


It should be:

nsInterface.frmInterface objTemp = new nsInterface.frmInterface;
objTemp.Show();
....
....
objTemp.lblStatus.text = "etc etc";

Is that what you have?

cheers,
 
B

Bruce Wood

A few things.

First, when you post code here, please post the real code.
Cut-and-paste the relevant bits of code directly from your project into
your post. You'll get a much better response from people because your
post will be clearer. For example, I notice that you also typed the
..Text property as .text, which won't work in C#.

Second, I have a theory (although I can't tell for sure because I can't
see all of your code). You do realize that when you say:

nsInterface.frmInterface objTemp = new nsInterface.frmInterface();

that you are creating a brand new form, that has nothing to do with any
other frmInterface that may be showing on the screen? Let me
illustrate. Let's say that you say:

nsInterface.frmInterface inter1 = new nsInterface.frmInterface();
inter1.Show();

and then somewhere else in your code you say:

nsInterface.frmInterface objTemp = new nsInterface.frmInterface();
objTemp.lblStatus.Text = "etc. etc.";

that this will do _absolutely nothing_ to the form that's showing on
the screen (that has a reference stored in "inter1"). By saying "new"
twice you're creating _two_ forms and then changing the label text on
the second one. This does nothing to the first one. That's what Kevin
was talking about when he said that you had to have a reference to the
form instance. In the code above I created _two_ instances of the same
form. If I had said

objTemp.Show();

then I would see two identical forms on the screen (except one would
have its label text set to "etc. etc.").

Third, realize that updates to the user interface occur only after the
UI thread has finished all of its processing. If you're in the middle
of some code and you say

objTemp.lblStatus.Text = "etc. etc.";

the label text doesn't change right away. Instead, WinForms marks the
label as "dirty" and needing to be redrawn. Then, when the UI thread
returns to Windows, WinForms then walks through the form looking for
stuff to redraw. This is why having a "status label" doesn't show
progress if you're doing all of your processing in the UI thread. If
you run the following code under the UI thread

for (int i = 0; i < 10000; i++)
{
objTemp.lblStatus.Text = String.Format("Processing {0} of
10000...", i);
}
objTemp.lblStatus.Text = "Done.";

all you'll see in the label, after a brief pause, is "Done.". That's
because WinForms marked lblStatus as "dirty", but only actually redrew
it on the screen after you'd finished your work in the UI thread, at
which point its text reads "Done."

Fourth, and finally, it's generally bad form to expose child controls
to the outside world. You should prefer to write a property in your
frmInterface class that looks like this:

public string StatusString
{
get { return this.lblStatus.Text; }
set { this.lblStatus.Text = value; }
}

and then make lblStatus private. That way you can one day decide to
change your status display from a label to some other control without
breaking all of your client code.
 
J

Jon S via DotNetMonster.com

Hi Ignacio

Thanks for replying. I didn't have that originally but I've now tried it and
now I get two forms appearing (both the main form). The original form doesnt
show the lblStatus and the newer main form goes white until the program
finishes and then appears correctly with the lblStatus showing the last
caption it is meant to but all the prior captions dont show because the form
goes white.

Hope I make sense.

Thanks.
 
J

Jon S via DotNetMonster.com

Hi Bruce,

I really appreciate the indepth explanation - thank you. At the same time
you posted your message I replied to another message with my findings which
is exactly what you said would happen in your message. Ok so I'm how then do
I reference the label lblStatus correctly?

Thank you.
 
K

Kevin Spencer

Hi Jon,

Assuming that both Forms are running in the same application, they are
running under the same context. Depending on how your app is designed, one
form usually has access to the other. So, assuming that an instance called
Form1 has referenced an instance of another form whose instance name is
Form2, and Form2 has the label, you would refer to Form2.lblStatus. In other
words, as long as a form instance has a reference to another form instance,
it can refer to any public member of that form.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi

post the code of the method where you create/show the second form

Are both forms of the same type?

cheers,
 
B

Bruce Wood

OK.. first question is, do you intend that there only ever be one
instance of nsInterface.frmInterface at any one time, or do you allow
for the possibility that the user may have several copies of this form
displaying at once? (The first is the more usual case, by the way.)
 

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