循环遍历Windows窗体中的变量

时间:2014-04-02 14:23:18

标签: .net vb.net winforms loops

我正在编写一些VB.Net代码来从一个数组中填充Windows窗体上的文本和图片框,但这似乎是一种相当低效的处理方式:

Item1_TB.Text = FormatCurrency(Items(0).Item_Cost, 2)
Item2_TB.Text = FormatCurrency(Items(1).Item_Cost, 2)
Item3_TB.Text = FormatCurrency(Items(2).Item_Cost, 2)
Item4_TB.Text = FormatCurrency(Items(3).Item_Cost, 2)
Item5_TB.Text = FormatCurrency(Items(4).Item_Cost, 2)
Item6_TB.Text = FormatCurrency(Items(5).Item_Cost, 2)

Item1.Image = Image.FromFile(Items(0).Item_Image)
Item2.Image = Image.FromFile(Items(1).Item_Image)
Item3.Image = Image.FromFile(Items(2).Item_Image)
Item4.Image = Image.FromFile(Items(3).Item_Image)
Item5.Image = Image.FromFile(Items(4).Item_Image)
Item6.Image = Image.FromFile(Items(5).Item_Image)

有什么方法可以将它放在for循环中并使用类似的东西:

Item[i]_TB.Text = FormatCurrency(Items(i).Item_Cost, 2)
Item[i].Image = Image.FromFile(Items(i).Item_Image)

4 个答案:

答案 0 :(得分:1)

您可以遍历父控件中的所有控件。不知道那是你的表格或小组还是那样的。

dim tb as textbox
dim i as integer
for each o as object in me.controls
 if typeof(o is textbox) then
  tb = ctype(o,textbox)
  i = tb.name.substring(4,1)
  tb.Text = FormatCurrency(Items(i).Item_Cost, 2)
 end if
next

答案 1 :(得分:0)

将所有控件放在类似的数组中允许循环。

可以使用这样的格式......

Dim ctls() as control={Item1_TB, Item2_TB, etc... }
For iQ as integer = 0 to ubound(ctls)
    ctls(iQ).Text = .FormatCurrency(Items(iQ).Item_Cost, 2)
Next iQ

答案 2 :(得分:0)

这有点粗略,因为它假设你永远不会有超过九个控件(即1到9)并且它假定TextBox和PictureBox控件都以相同的方式处理。如果表单上有其他TextBox或PictureBox控件,则无效。

For Each control In Me.Controls
    If TypeOf control Is TextBox Then
        Dim txt As TextBox = CType(control, TextBox)
        Dim index As Integer = CInt(txt.Name.Substring(4, 1))

        txt.Text = FormatCurrency(Items(index - 1).Item_Cost, 2)
    ElseIf TypeOf control Is PictureBox Then
        Dim pic As PictureBox = CType(control, PictureBox)
        Dim index As Integer = CInt(pic.Name.Substring(4, 1))

        pic.Image = Image.FromFile(Items(index - 1).Item_Image)
    End If
Next

可以使其超过9个控件,但Name.Substring调用需要更加智能化。

更新

我只是想到了另一种选择。 TextBoxPictureBox都有Tag属性。您可以使用它来表示索引。如果未设置Tag属性,则只需跳过它。

For Each control In Me.Controls
    If TypeOf control Is TextBox Then
        Dim txt As TextBox = CType(control, TextBox)

        If Not txt.Tag Is Nothing Then
            Dim index As Integer = CInt(txt.Tag)

            txt.Text = FormatCurrency(Items(index).Item_Cost, 2)
        End If
    ElseIf TypeOf control Is PictureBox Then
        Dim pic As PictureBox = CType(control, PictureBox)

        If Not pic.Tag Is Nothing Then
            Dim index As Integer = CInt(pic.Name.Substring(4, 1))

            pic.Image = Image.FromFile(Items(index).Item_Image)
        End If
    End If
Next

这样做的好处是,如果向窗口添加新的TextBox / PictureBox对,则永远不必更改代码。只需添加控件,将Tag属性设置为Items数组的索引,该数组应该用于填充每个数组,然后离开。

答案 3 :(得分:0)

最有效的方法是让你的代码知道你的用户界面中存在逻辑控制范围。

Dim textboxesRange As TextBox()
Dim pictureboxesRange As PictureBox()

在构造函数中,您必须初始化这些范围。

Public Sub New()
    // ...

    textboxesRange = {Item1_TB, Item2_TB, Item3_TB, Item4_TB, Item5_TB, Item6_TB }
    pictureboxesRange = {Item1, Item2, Item3, Item4, Item5, Item6 }

    // ...
End Sub

之后,您将能够遍历这些数组:

For i = 0 To textboxesRange.Length - 1
    textboxesRange(i).Text = FormatCurrency(Items(i).Item_Cost, 2)
Next

For i = 0 To pictureboxesRange.Length - 1
    pictureboxesRange(i).Image = Image.FromFile(Items(i).Item_Image)
Next

通过这种,您可以让代码了解存在相关控件的现实,从而加强您的应用。从现在开始,您不仅可以更轻松地启动值,还可以更改或重置它们(如果需要)。您还可以以动态,健壮,优雅和高效的方式将这些控件作为不同事物(隐藏,移动等)的逻辑组进行操作。

虽然使用控件的NameTag属性也可以很好地工作,但恕我直言,这是不好的做法,因为这些并不是专门用于解决这样一个特定问题。不要试图依赖那些。

相关问题