2010-02-21 44 views
1

我有Form1和類名爲AbstractClass.cs的文件。如何引用一個類的WinForms標籤

我想調用Form1中的標籤並將msg字符串分配給該標籤中的文本。

我該怎麼做?

這是我的課。

//"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); 
    } 
} 
+0

您需要詳細說明您的問題!你不能只使用標籤名稱/ ID分配。 lblName = msg; – azamsharp 2010-02-21 05:13:33

+0

標籤已經在表格上了嗎? DisplayName方法是否與窗體在同一個線程上運行?如果是這樣,你可以做到這一點(標籤名爲「label1」: label1.Text = msg; – 2010-02-21 05:14:55

+0

我想將類中的字符串傳遞給另一種形式的標籤!!! – Eyla 2010-02-21 05:50:54

回答

0

所有你需要的是一個屬性在您的類,然後從類調用這個屬性像

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; 
    } 
2

在CPUMoon類 創建一個名爲

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); 
    } 
} 

在你Form1類屬性,調用CPUMoon方法和用途在Form1的標籤公共信息性質。

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

謝謝你的回覆我做了但是當我把表單中的方法將返回空消息 CPUMoon C =新CPUMoon(); lbl1.Text = c.message; 任何建議?? – Eyla 2010-02-21 05:54:51

+0

放置調試點的顯示名稱方法和看 – 2010-02-21 06:21:23

+0

'c.Message'是空的,因爲'DisplayName()'沒有被調用。 – 2010-02-21 07:28:27

1

窗體上的控件是私人的。如果您希望能夠從一些外部類修改一個的屬性,那麼你需要讓你的形式公開公共財產可以這樣做:

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

然後,它變得微不足道參考:

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

我不得不指出,你寫的是一些非常瘋狂的意大利麪代碼。但不知道你想要做什麼的細節,這與任何人都可以提供的建議一樣多。

+0

我實現你的代碼,但是它的返回值爲空!! 任何建議, – Eyla 2010-02-21 06:43:35

+0

@Eyla:什麼返回null?財產獲得者?它w生病返回無論標籤的文字是什麼。您需要更具體地瞭解您正在做什麼以及問題是什麼,才能幫助您進行調試。 – Aaronaught 2010-02-21 14:27:22

相關問題