如何增加变量的名称,例如bar1、bar2、bar3、VBNET 在 For 循环中? [英] How do I increment the name of a variable e.g. bar1, bar2, bar3, VBNET in a For loop?

查看:44
本文介绍了如何增加变量的名称,例如bar1、bar2、bar3、VBNET 在 For 循环中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试简化我的代码:

I'm trying to simplify my code:

        bar_x(0) = delocateX(bar1.Left)
        bar_y(0) = delocateY(bar1.Top)
        bar_x(1) = delocateX(bar2.Left)
        bar_y(1) = delocateY(bar2.Top)
        bar_x(2) = delocateX(bar3.Left)
        bar_y(2) = delocateY(bar3.Top)
        bar_x(3) = delocateX(bar4.Left)
        bar_y(3) = delocateY(bar4.Top)

一直持续到 29,我试图使用 For 循环来减少行数并简化代码.这是我的尝试:

which continues until 29, and I was trying to use a For loop to reduce the number of lines and simplify the code. This is my attempt:

     Function set_bar_positions()
         Dim i As Double
         For i = 0 To 29
             bar_x(i) = delocateX(("bar" & (i + 1)).Left)
             bar_y(i) = delocateY("bar" & (i + 1) + ".Top")
         Next

我怎样才能简化这个函数?

How can I simplify this Function?

推荐答案

我设法找到了最简单的解决方案:

I managed to find the easiest solution to be:

Function set_bar_positions()
        For i = 0 To 29
            Dim cs = Me.Controls.Find("bar" & i.ToString(), True)
            If cs.Any() Then
                Dim c = cs.First()
                bar_x(i) = delocateX(c.Left)
                bar_y(i) = delocateY(c.Top)
            End If
        Next
    End Function

djv

这篇关于如何增加变量的名称,例如bar1、bar2、bar3、VBNET 在 For 循环中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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