在 vb.net 中如何在变量名中使用变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21048524/
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 16:25:04  来源:igfitidea点击:

in vb.net how do I use a variable in a variable name

vb.netvariables

提问by John

I have a bunch of variables named "panel_1", panel_2", etc and I want to change the height of all of the controls using a loop rather than naming each one.

我有一堆名为“panel_1”、panel_2”等的变量,我想使用循环更改所有控件的高度,而不是命名每个控件。

Currently I use

目前我使用

Dim panel_1 as new panel
Dim panel_2 as new panel
Dim panel_3 as new panel

panel_1.height = 100
panel_2.height = 100
panel_3.height = 100

How can I do it this way?

我怎么能这样做?

for x as integer = 0 to 1000
  panel_[x].height = 100
next

thanks.

谢谢。

回答by Steven Doggart

If this is a WinForm project, all of the controls are included in the form's Controlscollection, which is accessible by name, like this:

如果这是一个 WinForm 项目,则所有控件都包含在表单Controls集合中,该集合可以通过名称访问,如下所示:

For x As Integer = 0 to 1000
    Me.Controls("panel_" & i.ToString()).height = 100
Next

However, be aware that the form's Controlscollection only contains the controls that are added directly to the form. If you have controls that are inside another container control, you will need to access them through that container's Controlsproperty, for instance:

但是,请注意表单的Controls集合仅包含直接添加到表单的控件。如果您的控件位于另一个容器控件中,则需要通过该容器的Controls属性访问它们,例如:

For x As Integer = 0 to 1000
    MyContainer.Controls("panel_" & i.ToString()).height = 100
Next

However, accessing controls by their string name may be something that you wish to avoid, since it can lead to unintended consequences down the road. As other's have suggested, it may be better to store the list of panels in a List(Of Panel) variable, which only contains the specific panel controls to which the resizing logic applies, like this:

但是,您可能希望避免通过字符串名称访问控件,因为这可能会导致意想不到的后果。正如其他人所建议的那样,最好将面板列表存储在 List(Of Panel) 变量中,该变量仅包含调整大小逻辑适用的特定面板控件,如下所示:

Dim autoResizePanels As New List(Of Panel)({panel1, panel2, panel3})

Then, you can loop through them easily, like this:

然后,您可以轻松地遍历它们,如下所示:

For Each i As Panel In autoResizePanels 
    i.height = 100
Next

回答by Kna?is

You can't do such variable access in VB.NET.

你不能在 VB.NET 中进行这样的变量访问。

Instead of defining three variables, define an array or list

与其定义三个变量,不如定义一个数组或列表

Dim panels as new List(Of Panel)
panels.Add(new Panel())
panels.Add(new Panel())

for x as integer = 0 to 1000
  Dim p as new Panel
  p.height = 100
  panels.Add(p)

  panels(x).width = 150
next

回答by ??ssa P?ngj?rdenlarp

My preferred method is to (usually) store the controls names:

我的首选方法是(通常)存储控件名称:

Dim panels as new List(Of String)

For n As integer = 0 to X
   Dim pnl As New Panel
   '... set values
   panels.Add(pnl.Name)
   Controls.Add(pnl)
Next n

To reference one:

参考一:

Controls(panels(IndexofOneYouWant).Height = 100

Since you dynamically created the control, you could also be deleting them on occasion. In that case the List is not holding a reference to them when you go to remove one. If they remain once created, then a List(of Panel)is easier and better.

由于您动态创建了控件,因此您有时也可能会删除它们。在这种情况下,当您删除一个列表时,列表不会保存对它们的引用。如果它们在创建后保持不变,那么 aList(of Panel)会更容易也更好。

回答by DNKROZ

Just loop through how many panels you want (In your case 3) & create a new panel on each iteration, setting the height & name programmatically.

只需遍历您想要的面板数量(在您的情况 3 中)并在每次迭代时创建一个新面板,以编程方式设置高度和名称。

For i = 0 To 3
  Dim panel As New Panel
  panel.Name = "panel" & i
  panel.Height = 100
Next