how to refer a control from another class

G

Guest

hi,

I am doing a windows application. I have this basic problem:

I have a class Form1 got a label
from another class B i want to display some text in the label in form 1

in class B, the code i write is this
Form1 a = new Form1;
a.label1.Text = "hello";

but there s no text appeared in the dialog.

please help

regards
 
D

Dan Bass

This depends on the context... From reading your question, I thought you are
talking about being on some form (say form2), and referencing an already
open form (form1).

Your example then lends to the fact that you're creating another form and
wanting to set the label text before showing the label?

Let us know what you're trying to do and we'll see how we can help...
 
G

Guest

Jess,

For the most part a Windows Form will be used as the presentation layer for
an application. You would use an underlying class (e.g. the class B that you
are referring) to add functionality to the form. So instead of trying to set
the label to some text within class B you could try something like…

…inside form a
B myClass = new B();
a.label1.Text = B.SetLabelText();

…inside class B
public string SetLabelText()
{
return “helloâ€;
}

For additional information you may want to consult the MSDN @
http://msdn.microsoft.com/library/d...us/vbcon/html/vbconintroductiontowfcforms.asp . Good Luck!
 
J

Joel Martinez

Well ... to do that, you'll need to have a reference to the actual
Form1 instance that's currently showing. One common way of easing
functionality like this is to make the form a Singleton so you can
access the one instance from any other class easily.
http://msdn.microsoft.com/library/d...n-us/dnpatterns/html/ImpSingletonInCsharp.asp

Another possible solution for you would be to look into implementing
the MVC pattern:
http://www.codeproject.com/csharp/model_view_controller.asp

Joel Martinez
http://www.onetug.org - Orlando .Net User Group
http://www.codecube.net - blog
 
G

Guest

hi,

This is wat I am looking for. Anyway, in the method SetLabelText of class B,
I need to display some different text at different places to the label while
there only 1 return in a method. What should i do?
…inside class B
public string SetLabelText()
{
return “helloâ€;
}

Thank so much

Jess
 
G

Guest

Jess,

You could always pass in a reference parameter (multiple if needed) to the
method. This would allow you to get back as many different strings as needed
by the method. For example:

…inside class B
public string SetLabelText(ref string str1, ref string str2)
{
str1 = "hello there";
str2 = "I am string 2";
return "hello!";
}

....inside class a
string s1 = string.Empty,
s2 = string.Empty,
s3 = string.Empty;

s3 = b.SetLabelText(ref s1, ref s2);
a.label1.Text = s1 + s2 + s3;

Hope that this helps!
 

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