我有一个包含按钮和文本框的form.cs文件,还有一个处理当点击按钮时触发的事件的类。
基本上,当按下按钮时,它应该触发一个事件,并且Print类中的事件处理程序应该将文本打印到表单中的TboxPrint。
这是我的代码样子:
基本上,当按下按钮时,它应该触发一个事件,并且Print类中的事件处理程序应该将文本打印到表单中的TboxPrint。
这是我的代码样子:
//Declare the delegate
public delegate void EventHandler(object sender, EventArgs e);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//event
public event EventHandler Print;
//Event caller, protected to prevent calling from other classes
protected virtual void OnPrint(System.EventArgs e)
{
if (Print != null) Print(this, e);
}
//raising event
public bool print_action(object value)
{
OnPrint(EventArgs.Empty);
return true;
}
public void BtnPrint_Click(object sender, EventArgs e)
{
PrintClass p = new PrintClass();
Form1 s = new Form1();
s.Print += p.printEventHandler;
print_action(true);
}
}
处理该事件的类和方法是:
class PrintClass
{
public void printEventHandler(object sender, EventArgs e)
{
string text = "Print Event occured";
}
}
看起来没有触发任何事件..我认为我引发事件或订阅事件处理程序的方式是错误的。那么我如何将事件处理程序中的文本传递回表单呢?
非常感谢您的帮助。
PrintClass
中添加一个字段,如internal TextBox outputBox;
并访问控件! - Parimal Raj