如何从form1.cs中的另一个类调用form1类事件?C#.NET更改文本框的文本
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类事件?C#.NET更改文本框的文本
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;
}
}
您可能会遇到跨线程限制,但这是另一个问题,如果您愿意,可以写一条评论。
谢谢!我意识到在问题上我很不清楚,但你真的帮助了我!我真的不明白,如果我应该试图学习代表或其他东西 – user1484717
是的,这是一个相当大的挑战,得到什么是问题:) – Anri
您可以随时直接更改文本。
public partial class form1 : form
{
public string TextboxText
{
get { return txtBox.Text; }
set { txtBox.Text = value; }
}
}
然后就去做:
form1.TextboxText = "My new text";
您可以编辑您的问题,正确的代码,并更好地解释你想达到什么样的? – Ewerton
并且使其成为有效的C#程序 –