代码之家  ›  专栏  ›  技术社区  ›  OldBuildingAndLoan Caffé

如何捕获运行时对象上的事件

  •  0
  • OldBuildingAndLoan Caffé  · 技术社区  · 17 年前

    我正在创建一个运行时带有几个按钮和组合框的表单。

    dim f as new form 
    

    (胡说八道)

    然后设置按钮AcceptDescription和RejectDescription…
    然后设置组合框说明组合框…

    然后…

    AddHandler acceptDescription.Click, AddressOf handleAcceptedDescription
    AddHandler rejectDescription.Click, AddressOf handleRejectedDescription
    

    然后我有这两种方法来捕获单击事件…但无法确定如何引用其他运行时生成的控件。(如果接受则组合框,如果拒绝则组合框)

    Private Sub handleAcceptedDescription(ByVal sender As System.Object, ByVal e As System.EventArgs)
      'stub 
      'will need to reference the chosen combobox value here 
      dim acceptedDescription as string = descriptionCombo.selectedValue .tostring  
    End Sub
    Private Sub handleRejectedDescription(ByVal sender As System.Object, ByVal e As System.EventArgs)
      'I want to close the runtime created form here, but can't reference it
      f.close()
      'and return user to main form
      Me.Focus()
    End Sub
    
    3 回复  |  直到 17 年前
        1
  •  1
  •   Dave Swersky    17 年前

    如果生成表单的代码在主窗体中,那么在主窗体类的类级别声明表单变量,以便您可以从事件处理程序访问它。组合框和文本字段也是如此-您需要确保变量声明在处理程序的范围之外,以便可以在处理程序中引用它们。

        2
  •  0
  •   Stu    17 年前

    你为什么不能参考它?只需将其保存为模块/窗体级别的变量,即可设置。

        3
  •  0
  •   OldBuildingAndLoan Caffé    17 年前

    为了避免全局定义,最佳答案是

    Private Sub handleRejectedDescription(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'sender is a button in this case.
        'get the button
        dim b as new button
        b = ctype(sender,button)
        'now get the button's parent form
        dim f as new form
        f = ctype(b.parent, form)
        'now close the form
        f.close()
    End Sub