从 vb6 迁移之前替换控制数组

发布于 2024-07-06 06:52:24 字数 77 浏览 8 评论 0原文

为了避免对移民设置进一步的障碍,同时 增强现有的 VB6 程序。 有没有办法在不使用vb​​6中的控制数组的情况下实现与它们相同的功能?

With a view to avoiding the construction of further barriers to migration whilst
enhancing an existing vb6 program.
Is there a way to achieve the same functionality as control arrays in vb6 without using them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

握住我的手 2024-07-13 06:52:24

在 .NET 中,您有一个标记属性。 您还可以让同一个委托处理由多个控件引发的事件。 将新控件的 Tag 属性设置为 Index。

Private Sub MyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click,Button2.Click

        Dim Btn As Button = CType(sender, Button)
        Dim Index As Integer = CType(Btn.Tag, Integer)
' Do whatever you were doing in VB6 with the Index property

End Sub

您还应该查看 VB6.Compatibility 中从 BaseControlArray 继承的类,它可以自动执行一些工作。 我发现使用 Tag 在转换过程中比依赖控件名称更不容易出错。 但是,不要绝对地感谢这一点。 您必须决定控件名称方法是最好还是标记作为索引方法最好。

无论哪种情况,您都可以轻松设置 .NET,将多个控件引发的事件汇集到一个处理程序中。

In .NET you have a tag property. You can also have the same delegate handle events raised by multiple controls. Set the Tag property of the new control to the Index.

Private Sub MyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click,Button2.Click

        Dim Btn As Button = CType(sender, Button)
        Dim Index As Integer = CType(Btn.Tag, Integer)
' Do whatever you were doing in VB6 with the Index property

End Sub

You also should look at the classes that inherit from BaseControlArray in the VB6.Compatibility which automates some of the work. I find the use of Tag to be less error prone in the conversion process than relying on the control name. However don't thank this as an absolute. You will have to decide whether the control name approach is best or the tag as index approach.

In either case you can easily setup .NET to funnel the events raised by multiple controls into one handler.

荭秂 2024-07-13 06:52:24

好吧,您始终可以在代码中创建自己的控件数组:) 不过,也许更好的容器是 Collection 或 Dictionary 对象。 根据您想要执行的操作,您也许可以使用自定义集合类为控件创建一个包装类...但是在 .NET 中使用泛型创建对象模型要好得多,因此现在最好在 VB6 中保持简单。

VBA 用户窗体缺乏对控件数组的支持,所以为什么不 Google 寻求有关如何使用 VBA、用户窗体、Excel 等模拟控件数组的建议。

顺便说一句,您是否尝试过将控件数组从 VB6 迁移到 VB.NET? 只是猜测,但考虑到它们在 VB 中常用,我想它们处理得很好。

Well, you could always create your own array of controls in code :) Perhaps a better container, though, is a Collection or Dictionary object. Depending on what you want to do, you could perhaps create a wrapper class for the control with a custom collection class... but creating an object model is far nicer using generics in .NET so probably best to keep it simple in VB6 for now.

VBA Userforms lack support for control arrays, so why not Google for suggestions on how to mimic control arrays with VBA, Userforms, Excel, etc.

BTW have you tried migrating control arrays from VB6 to VB.NET? Just a guess but considering they are commonly used in VB I imagine they are handled quite well.

凹づ凸ル 2024-07-13 06:52:24

在过去的几天里,我做了一些阅读和实验,似乎 vb6 中没有其他方法可以完成控制数组所做的事情。
如果您已经知道将在运行时、设计时创建的控件数量,那么您可以“使用事件”声明私有控件对象变量,并在运行时动态地实例化这些变量。 如果您需要创建更多内容,则可以这样做,但这些不会有任何代码可以响应事件而触发。
据我所知,这是问题的核心。 无法将代码与 vb6 中动态创建的控件的事件动态关联。

I did a bit of reading and experimentation over the last couple of days and it seems that there is no other way in vb6 to be able to do the things control arrays do.
If you already know the number of controls you'll be creating at runtime, at design time, then you can declare Private control object variables 'with events' and instance these at dynamically at runtime. If you need to create any more then you can do so but these won't have any code to fire in response to events.
This as far as I can see is the nub of the problem. there is no way to dynamically associate code with an event of a dynamically created control in vb6.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文