[英]How can I change the label text in form "a" when a button is pressed in form "b" .net C#
I was wondering if someone could help?我想知道是否有人可以提供帮助?
The goal of DataEntryForm is to return a string to form1. DataEntryForm 的目标是向 form1 返回一个字符串。
I've tried using event handlers so far but not had any luck.到目前为止,我已经尝试过使用事件处理程序,但没有任何运气。 Any suggestions?
有什么建议么?
My favorite way to do this is to encapsulate the logic in a static method of the form.我最喜欢的方法是将逻辑封装在表单的 static 方法中。 Static methods have access to the form's private members so you don't have to worry about exposing anything in public properties.
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;
}
}
}
Then in form1, all you have to do is this:然后在 form1 中,你所要做的就是:
var enteredText = DataEntryForm.Execute();
Add a public property InputValue to the DataEntryForm.将公共属性 InputValue 添加到 DataEntryForm。 When user clicks the button on the form assign the property and close the DataEntryForm:
当用户单击表单上的按钮时,分配属性并关闭 DataEntryForm:
this.InputValue = textbox.Text;
Opening and reading the value:打开并读取值:
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.