在运行时VB6的表单上为每个控件添加一个事件处理程序 [英] Add an event handler to each control on a form at runtime VB6

查看:198
本文介绍了在运行时VB6的表单上为每个控件添加一个事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VB6应用程序,我希望在其应用程序范围内的控件之间保持一致的行为。例如,其中一个行为会突出显示一个文本框,当它获得焦点,并在失去焦点时删除高亮。我希望这可以在每个表单上发生。



我想要做的是有一个子程序,可以在加载时被所有表单调用使这种行为发生。这样,我不需要为每个单独的文本框手动编码,使其突出显示。



我已经尝试让VB6将一个事件处理程序附加到控件在运行时,但它只是吠叫我。我来自一个.Net背景,所以也许我接近错误的VB6。但是,如果没有为每个控件手动编写代码,我该如何得到这个所需的行为?

解决方案

你也可以子类您的文本框控件使用 WithEvents 。这里的优势在于,您可以在一个地方编写突出显示和去高亮显示,而无需经过并替换所有现有的控件(Scott建议)。



缺点是您必须向所有表单的Form_Load事件添加代码才能注册该表单上的控件。但是,如果要将技术应用于每个控件,即使这样也不会太糟糕;在这种情况下,您只需编写一个循环通过表单的 .Controls 集合的函数,并注册每个控件。然后在每个窗体的Form_Load事件中调用此函数。


I have a VB6 application where I'd like to have a consistent behavior among its controls application-wide. One of the behaviors, for example, would be highlighting a text box when it gains focus and removing the highlight when it loses focus. I'd like this to happen on every form.

What I'm trying to do is have one sub procedure that can be called by all forms when they load that will make this behavior happen. That way, I don't have to manually code for each individual text box to make it highlight.

I've tried getting VB6 to attach an event handler to a control at runtime but it just barks at me. I come from a .Net background so maybe I'm approaching it wrong for VB6. But how can I get this desired behavior without having to manually code it for every control?

解决方案

You could also "Subclass" Your TextBox Controls Using WithEvents. The advantage here is that you can code the highlighting and de-highlighting in one place without having to go through and replace all of your existing controls (as Scott suggests).

The downside is that you have to add code to the Form_Load event of all your forms to "register" the controls on that form. However, even this should not be too bad if you want to apply the technique to every control; in that case, you just need to write a function that loops through the .Controls collection of a form and registers each control. Then just call this function in each form's Form_Load event.

这篇关于在运行时VB6的表单上为每个控件添加一个事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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