vb.net 使用循环隐藏多个标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14256642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:51:19  来源:igfitidea点击:

Hide multiple labels using loop

vb.netlabelhide

提问by Thanzeem

How can I hide multiple Labelcontrols using a loop? I have tried doing so with the following code:

如何Label使用循环隐藏多个控件?我尝试使用以下代码执行此操作:

Dim i As Integer
For i = 1 To 14
    Dim lbl = Controls("Label" & i)
    lbl.Visible = False
Next

But it's not working for me. When the code executes, it throws a NullReferenceExceptionon the lbl.Visible = Falseline.

但这对我不起作用。当代码执行时,它抛出一个NullReferenceExceptionlbl.Visible = False线。

采纳答案by Thanzeem

I change my code like this.. Its working now.

我像这样更改我的代码..它现在可以工作了。

For i As Integer = 1 To 14
        Dim lbl = EMPGBDATA.Controls("Label" & i.ToString())
        If lbl IsNot Nothing Then
            lbl.Visible = False
        End If
    Next

EMPGBDATA is a GroupBox in my form.. All these labels in EMPGBDATA.

EMPGBDATA 是我的表单中的 GroupBox.. EMPGBDATA 中的所有这些标签。

回答by Nianios

The reason that you get that error is probably because you have less that 14 labels on your form. I'm guessing that it's Label14, or even a label with a smaller number, doesn't exist. Check that.

您收到该错误的原因可能是因为表单上的标签少于 14 个。我猜它Label14不存在,甚至是数字较小的标签不存在。检查那个。

Also, if you want to hide all of the labels on your form, then it would be better to do something like this:

此外,如果您想隐藏表单上的所有标签,那么最好执行以下操作:

 For Each lbl As Label In Controls.OfType(Of Label)()
     lbl.Visible = False
 Next

After your comments:

在您发表评论后:

Which version of Visual Studio are you using? OfTypeworks in both VS2008 and 2010. Maybe not in earlier versions...

您使用的是哪个版本的 Visual Studio? OfType适用于 VS2008 和 2010。也许不是在早期版本中...

Another way to do it is like this:

另一种方法是这样的:

For Each cnt As Control In Me.Controls
    If TypeOf cnt Is Label Then
        cnt.Visible = False
    End If
Next

回答by Steven Doggart

When you try to get a control from the Form.Controlscollection by index like this:

当您尝试Form.Controls通过索引从集合中获取控件时,如下所示:

Dim lbl As Control = Controls(100)

If no control by that index exists, it will immediately throw an ArgumentOutOfRangeException, as you would expect. However, for some strange reason, whoever implemented the ControlCollectionclass decided that when you are trying to get a control by name, like this:

如果不存在该索引的控制,它将立即抛出一个ArgumentOutOfRangeException,正如您所期望的那样。但是,出于某种奇怪的原因,实现ControlCollection该类的人决定,当您尝试按名称获取控件时,如下所示:

Dim lbl As Control = Controls("Label100")

If no control by that name exists, rather than throwing an exception, it simply returns Nothing. Therefore, to be safe, you should check to make sure a control was actually returned, like this:

如果不存在该名称的控件,它不会抛出异常,而是简单地返回Nothing。因此,为了安全起见,您应该检查以确保确实返回了控件,如下所示:

For i As Integer = 1 To 14
    Dim lbl = Controls("Label" & i.ToString())
    If lbl IsNot Nothing Then
        lbl.Visible = False
    End If
Next

Checking if lblis Nothing, like that, will stop the NullReferenceExceptionfrom being thrown. However, that doesn't explain why it's returning Nothingin the first place.

检查 if lblis Nothing,像那样,将阻止NullReferenceException抛出。但是,这并不能解释为什么它Nothing首先返回。

There are two reasons I can think of why it may not be finding your control:

我可以想到为什么它可能找不到您的控制权的原因有两个:

  • You have the wrong control name. You are looking for a control with the name "Label1", but you may be mistaken and that may not be the actual name of the control. It's possible, for instance, for the control's name to be different than the variable name that references it. To know for sure, inspect the value of Label1.Nameto see what its name actually is.
  • The Controlsproperty on your form has been overloaded so it's not calling the base property. To fix that, you could specify MyBase.Controlsinstead of simply Controls.
  • 您的控件名称有误。您正在寻找名称为“Label1”的控件,但您可能弄错了,这可能不是控件的实际名称。例如,控件的名称可能与引用它的变量名称不同。要确定,请检查 的值Label1.Name以查看其名称实际上是什么。
  • Controls表单上的属性已重载,因此它不会调用基本属性。要解决这个问题,您可以指定MyBase.Controls而不是简单地Controls.