[英]Changing the text property or any property of control from another form
我正在使用C#開發Windows應用程序。
我有一個來自說X的標簽,其中我有一個標簽說是label1,來自代碼類X.cs的x是
繼承類說FormManager.cs(用於控制通用功能的自定義方法
表單加載或表單關閉時的所有表單)。 我正在FormManager.cs上運行線程以執行
一些重復的任務,並且我要從其中更改更改label1的標簽文本
此FormManager.cs類。 我怎樣才能做到這一點。
Invoke
以便在UI線程中執行UI更新。 這是一個實現示例: FormManager
表單:
public partial class FormManager : Form
{
public FormManager()
{
InitializeComponent();
}
public Action BackgroundActionCompleted { get; set; }
public void OnBackgroundActionCompleted()
{
if (this.BackgroundActionCompleted != null)
{
// Invoke so the action will be launched on the UI thread
this.Invoke(this.BackgroundActionCompleted);
}
}
}
X
形式:
public partial class X : FormManager
{
public X()
{
InitializeComponent();
}
private void X_Load(object sender, EventArgs e)
{
this.BackgroundActionCompleted = delegate() { this.label2.Text = "New Text"; };
}
}
基類看不到子屬性。
但是你可以在基類上使用
abstract void UpdateLabel();
在您的線程中調用它,然后在派生類中實現此方法,您可以在該類中訪問Label1對象。
請記住,要從其他線程更新UI對象,您可能需要使用Invoke()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.