开发者

How to refer to a WinForms label from a class

开发者 https://www.devze.com 2022-12-21 05:55 出处:网络
I have Form1 and class in file called AbstractClass.cs. I want to call a label in Form1 and assign msg string to the text in that lable.

I have Form1 and class in file called AbstractClass.cs.

I want to call a label in Form1 and assign msg string to the text in that lable.

How can I do that???

here is my class.

//"Concrete CPUMoon" drived from AbstractCPU class when Diagnosticing CPU for Moon system
class CPUMoon : AbstractCPU
{
    public override void DisplayName(AbstractCPU a)
    {
        //Form1 f1 = new Form1();

        string msg;
        // create reader & open file
        StreamReader tr = new StreamReader("Moon.txt");
        String fromFile = tr.ReadLine();
        // close the stream
        tr.Close();
        msg = "CPU diagnosing has be done for   " + a.GetType().Name + "                " + fromFile;
        //Console.WriteLine("CPU diagnosing has be done for 开发者_如何学编程  " + a.GetType().Name + "                //" + fromFile);
    }
}


in your CPUMoon Class create a property called

class CPUMoon : AbstractCPU
{
    public string message {get;set;}
    public override void DisplayName(AbstractCPU a)
    {
        //Form1 f1 = new Form1();

        // create reader & open file
        StreamReader tr = new StreamReader("Moon.txt");
        String fromFile = tr.ReadLine();
        // close the stream
        tr.Close();
        message = "CPU diagnosing has be done for   " + a.GetType().Name + "                " + fromFile;
        //Console.WriteLine("CPU diagnosing has be done for   " + a.GetType().Name + "                //" + fromFile);
    }
}

in Your Form1 Class, Call the CPUMoon method and use the public message property in Form1 Label.

CPUMoon c = new CPUMoon();
label1.Text = c.Message;


Controls on a form are private. If you want to be able to modify the properties of one from some external class, then you need to make your form expose a public property to do so:

public class Form1
{
    public string MyMessage
    {
        get { return label1.Text; }
        set { label1.Text = value; }
    }
}

Then it becomes trivial to reference:

Form1 form = new Form1();
form.MyMessage = "...";

I do have to point out that what you're writing looks some pretty crazy spaghetti code. But without knowing the details of what you're trying to do, this is about as much advice as anyone can offer.


All You need is a Property in your class and then call this property from the class like

   public class AnyCLass
   {
       public string Message{get; set;}
       // any other methods or properties in class ...
       // note thatin any method which you change the Message content it should be 
       // used and if you do not call the method which change the Message value ,
       // you have a string.Empty value
       // to test this change the Message Value in Constructor like below

       public AnyClass()
       {
          this.Message = "You have to change the value somewhere which call with user";
       }
   }

   public partial class Form1.cs
   {
       AnyClass instance = new AnyClass();
       Label1.Text = instance.Message;
   }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号