2
我有一個form.cs,其中包含一個按鈕和一個texbtox,也是一個類,它處理單擊按鈕時引發的事件。單擊按鈕並將其訂閱到其他課程中的事件處理程序時引發事件?
基本上,當點擊該按鈕時,它應該引發一個事件,並在打印類的事件處理程序應在形式
這是我的代碼看起來是打印文本到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";
}
}
顯然沒有提出..我相信方式即提高事件或訂閱到事件處理程序是錯誤的。以及如何將事件處理程序中的文本傳回給窗體?
任何幫助表示讚賞..謝謝
的原因,我需要另一個類,我有一些數據要處理,所以當單擊按鈕時,它應該通知該類執行該過程並返回輸出。我希望這是明確的 – Liban 2013-02-23 06:37:52
@李本 - 在這種情況下,答案的上半部分將完成這項工作! – 2013-02-23 06:39:10
是的,我在做這個..怎麼樣返回值的形式(輸出到文本框)? – Liban 2013-02-23 06:47:49