在 Visual Basic 中使用 for 访问多个控件 [英] Access several controls with a for in visual basic

查看:25
本文介绍了在 Visual Basic 中使用 for 访问多个控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个 GroupBox 中得到了带有多个标签的表单,所有标签都具有相同的名称和一个数字(类似于默认的 Label1、Label2、LabelN)

I Got this form with several Labels inside a GroupBox, all with the same name plus a number (similar to the default Label1, Label2, LabelN)

我正在使用 sub() 更改此标签的外观和内容,但我无法弄清楚如何在不写下完整名称的情况下引用每个标签,可以执行以下操作:

I'm changing the look and content of this labels with a sub(), but I can't figure out how to refer to each label without write the complete name it's possible to do something like:

To All Labels inside Group Box 
Sub(LabelN)

目前我正在创建一个标签数组并在表单加载时分配名称,例如:

Currently I'm creating an Array of labels and assigning the names when the form loads, something like:

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LabelMatrix(0) = Label1
        LabelMatrix(1) = Label2
        LabelMatrix(2) = Label3
        LabelMatrix(3) = Label4
....
    End Sub

但我想一定有更好(更聪明)的方法来做到这一点.

But I suppose there must be a better (and smarter) way to do this.

我想以获取 Groups 框中所有 Labels 对象的方式执行此操作,但我的努力没有成功.

I wanted to do it in a way that I get the total of the Labels objects in the Groups box but my efforts were unsuccessful.

推荐答案

很简单,不需要数组:

For Each lbl As Label In MyGroupBox.Controls.OfType(Of Label)()
    ' ... do something with "lbl"
Next lbl

这篇关于在 Visual Basic 中使用 for 访问多个控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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