我们如何从 form1.cs 中的另一个类调用 form1 类事件?
public partial class form1 : form
{
// an event to change the text of a textbox
}
public class A
{
sendtext()
{
//text to be sent to the texbox
// the text is created as a output of a thread
}
我们如何从 form1.cs 中的另一个类调用 form1 类事件?
public partial class form1 : form
{
// an event to change the text of a textbox
}
public class A
{
sendtext()
{
//text to be sent to the texbox
// the text is created as a output of a thread
}
A
您应该在课堂上创建一个事件并从form1
.
class A
{
public event Action<string> TextReady;
private OnTextReady(string text)
{
var ev = TextReady;
if(ev!=null) ev(text);
}
}
class Form1
{
private _a = new A();
public Form1()
{
_a.TextReady+= (text)=> textBox.Text = text;
}
}
您可能会偶然发现跨线程限制,但这是另一个问题,如果您愿意,请写评论。
您始终可以直接更改文本。
public partial class form1 : form
{
public string TextboxText
{
get { return txtBox.Text; }
set { txtBox.Text = value; }
}
}
然后做:
form1.TextboxText = "My new text";