如何捕获运行时对象上的事件 [英] how do I catch events on runtime objects

查看:24
本文介绍了如何捕获运行时对象上的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个包含几个按钮和运行时组合框的表单.

I'm creating a form with a few buttons and a combobox at runtime.

dim f as new form 

(废话)

然后设置按钮acceptDescription和rejectDescription...
然后组合框 descriptionCombo 被设置...

then the buttons acceptDescription and rejectDescription are set up...
then the combobox descriptionCombo is set up...

然后...

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

然后我有这两种方法来捕获点击事件......但无法弄清楚如何引用其他运行时生成的控件.(如果接受则为组合框,如果拒绝则为表单)

then I have these two methods to catch the click events... but can't figure out how to reference the other runtime generated controls. (combobox if accepted, form if rejected)

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

推荐答案

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

In order to avoid global definitions, the best answer is

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

这篇关于如何捕获运行时对象上的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆