![](/img/trans.png)
[英]C#: How can i change the text of a label thats in form1 from another class?
[英]How can I change the label text in form "a" when a button is pressed in form "b" .net C#
我想知道是否有人可以提供幫助?
DataEntryForm 的目標是向 form1 返回一個字符串。
到目前為止,我已經嘗試過使用事件處理程序,但沒有任何運氣。 有什么建議么?
我最喜歡的方法是將邏輯封裝在表單的 static 方法中。 Static 方法可以訪問表單的私有成員,因此您不必擔心在公共屬性中公開任何內容。
class DataEntryForm : Form
{
/* The rest of your class goes here */
public static string Execute()
{
using (var form = new DataEntryForm())
{
var result = form.ShowDialog();
return (result == DialogResult.OK) ? form.MyTextBox.Text : null;
}
}
}
然后在 form1 中,你所要做的就是:
var enteredText = DataEntryForm.Execute();
將公共屬性 InputValue 添加到 DataEntryForm。 當用戶單擊表單上的按鈕時,分配屬性並關閉 DataEntryForm:
this.InputValue = textbox.Text;
打開並讀取值:
DataEntryForm formDE = new DataEntryForm();
formDE.ShowDialog();
if (formDE.DialogResult == DialogResult.OK)
{
// ok result
string value = formDE.InputValue
formDE.Dispose();
}
else
{
//cancel
formDE.Dispose();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.