本文介绍了标签名称的变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的计算机课程做一个项目.真的可以请一些帮助.

I am doing a project for my computing coursework. Could really do with some help please.

我希望能够使用这样的基本代码行...

I want to be able to take a basic line of code like this...

   lblSectionsAttempted.ForeColor = Color.Green

基本上把它变成这个......但这不起作用:

And basically turn it into this... but this doesn't work:

 lblSectionsAttempted(TempInc).ForeColor = Color.Green

TempInc 是一个变量,每次循环完成时增加 1.

TempInc is a variable that gets incremented by 1 each time a loop is done.

我的表单上有 14 个lblSectionAttempted"标签.而且我希望能够根据变量 TempInc 的值来更改每个标签的前景色……例如:

I have 14 'lblSectionAttempted' labels on my form. And I want to be able to change the fore color of each label depending what the value of the variable TempInc is... So for example:

所以当 TempInc = 1 时,我希望 lblSectionsAttempted1.ForeColor 改变

So when TempInc = 1, I want lblSectionsAttempted1.ForeColor to change

然后当 TempInc =2 时,我希望 lblSectionsAttempted2.ForeColor 改变

Then when TempInc =2, I wan lblSectionsAttempted2.ForeColor to change

 If TempInc = 1 Then
      lblSectionsAttempted1.ForeColor = Color.Green

 Else if TempInc = 2 Then
      lblSectionsAttempted2.ForeColor = Color.Green

等等.等

但是有很多 if 语句并不理想.请有人告诉我如何重新编写这行代码以使变量的值影响更改的标签..

However having lots of if statements isn't ideal. Please can someone tell me how I can re word this line of code to make the value of the variable affect what label is changed..

 lblSectionsAttempted(TempInc).ForeColor = Color.Green

推荐答案

您可以使用 Controls.Find():

You can use Controls.Find():

    Dim matches() As Control = Me.Controls.Find("lblSectionsAttempted" & TempInc, True)
    If matches.Length > 0 AndAlso TypeOf matches(0) Is Label Then
        Dim lbl As Label = DirectCast(matches(0), Label)
        lbl.ForeColor = Color.Green
    End If

这篇关于标签名称的变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 09:45