【问题标题】:calling event within the same Window Form in Delphi Prism在 Delphi Prism 的同一窗口窗体中调用事件
【发布时间】:2011-08-30 19:28:52
【问题描述】:

我正在尝试从同一个窗口窗体文件的另一个方法中调用单击事件。它对我不起作用。

例如:

  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. 无法访问底层事件字段

那么,如何在同一个窗体窗体中触发事件呢?

【问题讨论】:

    标签: events listbox click delphi-prism


    【解决方案1】:

    最好使用默认参数调用事件处理程序:

    AlarmListBox_Click(self, EventArgs.Empty);
    

    通过将 self 传递给方法,您可以定义调用的来源不是 AlarmListBox,而是您的表单。您还可以传入自定义 EventArgs,声明事件不是因为单击 AlarmListBox 而引发的,而是来自您的代码。

    【讨论】:

      【解决方案2】:

      你没有传递方法AlarmListBox_Click的参数

      试试这个

      AlarmListBox_Click(nil, nil);
      

      【讨论】:

      • @RRUZ,参数是事件的一部分。所以,调用者不应该能够传递它们。如果我错了,请纠正我。在 Delphi 中,您只需调用 AlarmListBox_Click(self) 或仅调用 AlarmListBox_Click;
      • 在 delphi 中,您可以使用 Click 过程(如 Button1.Click; 或直接执行与事件关联的过程 Button1Click(nil); )调用与单击事件关联的过程而无需参数。在 .Net Winforms 中,您可以使用按钮的 PerformClick 方法执行此操作。但是这种方法只对少数几个控件公开,例如 MenuItems、RadioButtons 和 Buttons。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多