VB6中动态控件的事件处理 [英] Event handling for dynamic controls in VB6

查看:245
本文介绍了VB6中动态控件的事件处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在VB6中实现动态控件的事件处理?任何想法?

How can i achieve event handling for dynamic controls in VB6? Any ideas?

推荐答案

最简单的方法是声明与控件相同类型的模块级变量,并使用 WithEvents 关键字。例如

The easiest way is to declare a module-level variable of the same type as the control, and use the WithEvents keyword. For example

Option Explicit
' Declare object variable as CommandButton and handle the events.'
Private WithEvents cmdObject As CommandButton 

Private Sub Form_Load()
   'Add button control and keep a reference in the WithEvents variable'
   Set cmdObject = Form1.Controls.Add("VB.CommandButton", "cmdOne")
   cmdObject.Visible = True
   cmdObject.Caption = "Dynamic CommandButton"
End Sub

'Handle the events of the dynamically-added control'
Private Sub cmdObject_Click()
    Print "This is a dynamically added control"
End Sub

如果您希望能够通过一个中央例程处理来自许多不同类型的控件(可能是不同类型的)的事件,则会有更复杂的变化。

There are more complicated variations if you want to be able to handle events from many different controls, perhaps of different types, through one central routine.

这篇关于VB6中动态控件的事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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