473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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.
--
Message posted via http://www.dotnetmonster.com
Nov 17 '05 #1
9 1501
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.

"Jon S via DotNetMonster.com" <u2272@uwe> wrote in message
news:56aad5e954e85@uwe...
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.
--
Message posted via http://www.dotnetmonster.com

Nov 17 '05 #2
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.
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200510/1
Nov 17 '05 #3
Hi,
It should be:

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

Is that what you have?

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jon S via DotNetMonster.com" <u2272@uwe> wrote in message
news:56aad5e954e85@uwe...
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.
--
Message posted via http://www.dotnetmonster.com

Nov 17 '05 #4
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.

Nov 17 '05 #5
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.
--
Message posted via http://www.dotnetmonster.com
Nov 17 '05 #6
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.
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200510/1
Nov 17 '05 #7
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.

"Jon S via DotNetMonster.com" <u2272@uwe> wrote in message
news:56ab5ce744605@uwe...
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.
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200510/1

Nov 17 '05 #8
hi

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

Are both forms of the same type?

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jon S via DotNetMonster.com" <u2272@uwe> wrote in message
news:56ab3cbe55b72@uwe...
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.
--
Message posted via http://www.dotnetmonster.com

Nov 17 '05 #9
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.)

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael Brandt Lassen | last post by:
Hi gurus This problem is about calling Web services secured by Forms Authentication from Windows Forms user controls embedded in HTML. Using the object tag I’ve managed to include a Windows...
6
by: Jon Hyland | last post by:
Ok, I'm a little rusty on this, it should be a simple problem but I can't figure it out. How can I handle form events in my main code page?? I'm creating a Windows App in C#. Rather than make...
3
by: Scott Schade | last post by:
I have a form onto which I add a control during execution. The control is a control that I wrote. The control has a number of controls on it. I would like to click on a button on the control and...
6
by: Christian H | last post by:
Hi! I've created a custom control (myDrawControl) that deals with drawing. This control is then added to a form( myMainForm) Now, whenever something is dragged and dropped onto myDrawControl ,...
5
by: steve bull | last post by:
When adding a new user control to a panel I get the following call stack. Having put debugs everywhere I can think of I cannot get the program to break anywhere near where it is having this problem....
0
by: Tony Johansson | last post by:
Hello! I have a very specific question and that is about how to inherit a visual control for example the control System.Windows.Forms.TextBox without causing the environment to delete the...
2
by: sumanthsclsdc | last post by:
Hello friends, I have a problem, I implemented a class which uses tkinter and displays the window as required, the class will create a window with listbox and inserts some items into it, I...
1
by: bytebugs | last post by:
Hi, As a matter of principle I have refranied from creating an instance of Form or UserControl from within a class that does not inherit from System.Windows.Form. I am looking for comments on...
0
by: pinky22 | last post by:
I am calling SSIS package from a .Net windows UI. Both SSIS & .Net app are created in 2008. The SSIS package is stored in file system. When I ran .Net app I got error- The package failed to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.