By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,364 Members | 1,521 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,364 IT Pros & Developers. It's quick & easy.

Controlling Form 1 label from a seperate class file???

P: n/a
Hi all,

I posted this yesterday but didnt explain it too well so thought I would do
it again.

I have a label on a form that I want to be able to change the text while I'm
in a seperate class file. For example -

FORM1 IS :

nsInterface
{
clsInterface
{
private System.Windows.Forms.Label lblStatus;

public void setStatus(text)
{
lblStatus.Text = text;
}
}
}

THE SEPERATE CLASS FILE IS :

nsConvert
{
clsConvert
{
public void convertNow()
{
nsInterface.clsInterface objInterface = new
nsInterface.clsInterface();

objInterface.setStatus("hello world");
}
}
}

I intend the Form1 (which is called frmInterface) lblStatus control to show
the text which is passed to it from the convertNow method which is in a
seperate class file. So to repeat all I want to do is have the From1
(frmInterface) lblStatus control show the text which is passed to it.

Hope this makes sense. Thanks in advance.
--
Message posted via http://www.dotnetmonster.com
Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
I should add that when I compile there are no errors. The method setStatus
(string text) is receiving the correct text but I cant get the lblStatus to
show on the Form1 (frmInterface) while the app is runnning.

I should also add that in my initial post where it has clsInterface it is
actually frmInterface as this is what the form is called.
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200511/1
Nov 17 '05 #2

P: n/a
Hi,

what you want to implement is a Observer Pattern.
Try out the following.

interface ISetStatus
{
void SetStatus(string status);
}

class form1 : System.Windows.Forms.Form, ISetStatus
{
public form1()
{
MyCallerClass myc = new MyCallerClass((ISetStatus)this);
}

void SetStatus(string status)
{
this.lable1.Text = status;
}
}

class MyCallerClass
{
private ISetStatus _obISetStatus;

public MyCallerClass(ISetStatus oISetStatus)
{
this._obISetStatus = oISetStatus;
this._ObIStetStatus.SetStatus("Hello");
}
}

Hope that helps. You also can use event for such
things ;)

Cheers
Lars Behrmann

Nothing is impossible. UML is the key for all your problems.
AODL - Make your .net apps OpenOffice ready
http://aodl.sourceforge.net/
Jon S via DotNetMonster.com schrieb:
Hi all,

I posted this yesterday but didnt explain it too well so thought I would do
it again.

I have a label on a form that I want to be able to change the text while I'm
in a seperate class file. For example -

FORM1 IS :

nsInterface
{
clsInterface
{
private System.Windows.Forms.Label lblStatus;

public void setStatus(text)
{
lblStatus.Text = text;
}
}
}

THE SEPERATE CLASS FILE IS :

nsConvert
{
clsConvert
{
public void convertNow()
{
nsInterface.clsInterface objInterface = new
nsInterface.clsInterface();

objInterface.setStatus("hello world");
}
}
}

I intend the Form1 (which is called frmInterface) lblStatus control to show
the text which is passed to it from the convertNow method which is in a
seperate class file. So to repeat all I want to do is have the From1
(frmInterface) lblStatus control show the text which is passed to it.

Hope this makes sense. Thanks in advance.
--
Message posted via http://www.dotnetmonster.com


Nov 17 '05 #3

P: n/a
Hi,

If I got you right you would like to "elegantly" change the contents of a
label on a form defined in a different file?

Situation:
FormA is a form in FileA
FormB is a form in FileB
FormB is supposed to be able to alter the contents of a label on a FormA
window.

Remember, FormA annd Form B are CLASSES! with new FormA() or new FormB() you
can create instances of those classes/windows.

If an INSTANCE of FormB is to reference an INSTANCE of FormA it needs a
REFERENCE!

Solution

FormA.cs
namespace GetItRight {
public class FormA : System.Windows.Form {
private System.Windows.Forms.Label Label1 = null;
public FormA() {
// the usual stuff
}
public void Foo() { // here we create and display a FormB window giving
it a reference to our Label1!
using(FormB frmB = new FormB()) {
frmB.ExternalLabel = this.Label1;
frm.ShowDialog();
}
}
}
}

FormB.cs
namspace GetItRight {
public class FormB : System.Windows.Form {
private System.Window.Forms.Label mExternalLabel = null;
public FormB() {
// the usual stuff
}
// here we define an "elegant" solution for outsiders to reference my
label
public System.Windows.Forms.Label ExternalLabel {
get { return mExternalLabel; }
set { mExternalLabel = value; }
}
public Foo () { // here I change the Text-Value of ExternalLabel
situated "somewhere"
ExternalLabel.Text = "I changed you on a FormA-Window!";
}
}
}
"Jon S via DotNetMonster.com" <u2272@uwe> schrieb im Newsbeitrag
news:56b4bbf2e3ac6@uwe...
I should add that when I compile there are no errors. The method setStatus
(string text) is receiving the correct text but I cant get the lblStatus
to
show on the Form1 (frmInterface) while the app is runnning.

I should also add that in my initial post where it has clsInterface it is
actually frmInterface as this is what the form is called.
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200511/1

Nov 17 '05 #4

P: n/a
Hey Lars and Robert

Thanks for replying. I dont think the problem is with referencing the
lblStatus. The lblStatus is getting the text but its not showing on the
frmInterface. The lblStatus control has properties to show the label but for
some reason when it runs it doesnt show even though the form that the
lblStatus resides on is showing. Do I need to add some code in order for it
to show the lblStatus. I now have the following code :

nsInterface
{
public class frmInterface
{
public void setStatus(string text)
{
this.lblStatus.Text = text;
}
}
}

nsConvert
{
public class clsConvert
{
public void convertNow()
{
nsInterface.frmInterface objInterface = new nsInterface.
frmInterface();
objInterface.setStatus("hello world");
}
}
}
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200511/1
Nov 17 '05 #5

P: n/a
if the prefixes ns, frm, cls refer to namespace, Form, Class then
in setStatus you might (under certain circumstances) need to call
this.lblStatus.Refresh() or this.Refresh()

"Jon S via DotNetMonster.com" <u2272@uwe> schrieb im Newsbeitrag
news:56b5607bcfabe@uwe...
Hey Lars and Robert

Thanks for replying. I dont think the problem is with referencing the
lblStatus. The lblStatus is getting the text but its not showing on the
frmInterface. The lblStatus control has properties to show the label but
for
some reason when it runs it doesnt show even though the form that the
lblStatus resides on is showing. Do I need to add some code in order for
it
to show the lblStatus. I now have the following code :

nsInterface
{
public class frmInterface
{
public void setStatus(string text)
{
this.lblStatus.Text = text;
}
}
}

nsConvert
{
public class clsConvert
{
public void convertNow()
{
nsInterface.frmInterface objInterface = new nsInterface.
frmInterface();
objInterface.setStatus("hello world");
}
}
}
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200511/1

Nov 17 '05 #6

P: n/a
Hi,

if it is a repaint problem? Then try to call
the Refresh() method on the control after
you call setStatus. It will force to paint it
self and all his child controls new.

Cheers
Lars Behrmann

Jon S via DotNetMonster.com schrieb:
Hey Lars and Robert

Thanks for replying. I dont think the problem is with referencing the
lblStatus. The lblStatus is getting the text but its not showing on the
frmInterface. The lblStatus control has properties to show the label but for
some reason when it runs it doesnt show even though the form that the
lblStatus resides on is showing. Do I need to add some code in order for it
to show the lblStatus. I now have the following code :

nsInterface
{
public class frmInterface
{
public void setStatus(string text)
{
this.lblStatus.Text = text;
}
}
}

nsConvert
{
public class clsConvert
{
public void convertNow()
{
nsInterface.frmInterface objInterface = new nsInterface.
frmInterface();
objInterface.setStatus("hello world");
}
}
}
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200511/1


Nov 17 '05 #7

P: n/a
Hi guys,

Yes, you're both abosolutely right the refresh() method works.

Thanks.
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200511/1
Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.