![](/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.