Newbe Question...

  • Thread starter Thread starter Brian K. Williams
  • Start date Start date
B

Brian K. Williams

If I have a Web form with a Label and I have separate class file as below,
how can I write to the label from the class (clsTest)

Web Form:
using BKW.test;

namespace BKW
{
public class frmTest : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblDescription;
public class frmFileManagerTest : System.Windows.Forms.Form
{
Code...
}
}


Class File:

namespace BKW.test
{
public class clsTest
{

}

}

Thanks in advance.
-Brian
 
Are you sure this isn't a Windows Forms app, not a Web Forms App?
When you create an instance of the class "clsTest" you can pass in an
instance to the form using a constructor. A simple example follows:

public class clsTest
{
private MyForm myform;

public clsTest(MyForm myform)
{
this.myform = myform;
}

private void ChangeLabel(string txt)
{
this.myform.lblDescription.Text = txt;
}
}
Note: "System.Windows.Forms.Label lblDescription" would need to be visible
outside Form scope:
public System.Windows.Forms.Label lblDescription;

Alternatively, you could always define an event in "clsTest". Then when you
create an instance of "clsTest" inside the Form scope you can hook into this
event with an event handler. Inside the event handler, which exists on the
Form, you can then just update the label. This might even be a better way as
it will abstract things like the label from code outside the Form. Thus
allowing you to keep the scope of the label private. All the class "clsTest"
would need to do is recognize that a "client" might need to know about a
change and fire the event. So the Form would be the only code that needs to
know what to do with the actual value. If what you are doing with the value
changes, for example because you are assigning this value to a textbox
instead, then you don't have to change the code in the class, just the Form
to which the TextBox exists. Did I explain that ok? Or was my little rant
too confusing? :)
 
Thanks for the help...
I guess it shows in my question, I have been a Web developer for years. Only
recently been playing with Window Forms.

-Brian
 
Back
Top