2011-08-30 54 views
1

我想從另一個窗口窗體文件的另一個方法中調用一個點擊事件。這對我來說不起作用。在德爾福棱鏡的相同窗口窗口中調用事件

例如:

theClass = partial class(System.Windows.Forms.Form) 
    method AlarmListBox_Click(sender: System.Object; e: System.EventArgs); 
    private 
    protected 
    public 
    method DoSomething; 
    end; 

    method theClass.DoSomething; 
    begin 
    self.AlarmListBox_Click; <<<this is where i want to call the click event 
    end; 

不管我做什麼,它不斷提高編譯器錯誤。我試過AlarmListBox.Click,AlarmListBox.performClick等

一些給我的錯誤:

  1. 沒有重載方法 「AlarmListBox_Click」 0 參數。
  2. 無法訪問底層事件現場

那麼,你怎麼火一樣的窗口表格中的事件?

回答

1

這是最好的調用使用默認參數的事件處理程序:

AlarmListBox_Click(self, EventArgs.Empty); 

通過傳遞自成您定義的調用方法不是AlarmListBox,而是您的表單。您也可以傳遞自定義的EventArgs,它聲明事件不會因爲單擊AlarmListBox而是您的代碼而引發。

0

你是不是傳遞給該方法的參數AlarmListBox_Click

試試這個

AlarmListBox_Click(nil, nil); 
+0

@ RRUZ,參數是事件的一部分。所以,來電者不應該能夠通過他們。糾正我,如果我錯了。在Delphi中,您只需調用AlarmListBox_Click(self)或僅調​​用AlarmListBox_Click; – ThN

+1

在delphi中,您可以使用像'Button1.Click'這樣的'Click'程序,或者直接執行與事件相關的過程'Button1Click(nil);',來調用與沒有參數的click事件關聯的過程。在.Net Winforms中,您可以使用按鈕的「PerformClick」方法執行此操作。但是這種方法只暴露了一些控件,如MenuItems,RadioButtons和Buttons。 – RRUZ

相關問題