且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在运行时更改多个标签的文本(逐个)

更新时间:2023-01-13 11:10:22

你需要的只是通过表单中的控件集合。



这是一个ASP.NET的例子,但机制是相同:如何:使用控件集合访问控件 [ ^ ]



更多:

每个......下一个声明(Vi sual Basic) [ ^ ]

Windows窗体控件:Z顺序和复制集合 [ ^ ]



foreach循环以循环表单中的所有文本框
[ ^ ]



请先试试。你可以做到!





1)创建新的Windows应用程序项目

2)添加class( MyLabel.vb )到您的项目并粘贴到代码下面:

All what you need is to go through the collection of controls in a form.

Here is an example for ASP.NET, but mechanism is the same: How to: Access Controls by using the Controls Collection[^]

More:
For Each...Next Statement (Visual Basic)[^]
Windows Forms Controls: Z-order and Copying Collections[^]

foreach loop to loop all the text box in a form
[^]

Please, try first. You can do it!


1) Create new Windows application project
2) Add class (MyLabel.vb) to your project and paste below code:
Public Class MyLabel
    Inherits Label

    Private bFlag As Boolean = False

    Public Property Flag() As Boolean
        Get
            Return bFlag
        End Get
        Set(ByVal value As Boolean)
            bFlag = value
        End Set
    End Property

End Class



2)将下面的代码复制并粘贴到Form1代码类中:


2) Copy and paste below code into Form1 code class:

Public Class Form1
    WithEvents ml As MyLabel


    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim lbl As MyLabel = Nothing
        For i As Integer = 1 To 5
            lbl = New MyLabel
            With lbl
                .Parent = Me
                .Left = 8
                .Top = (i * 24) + 8
                .Text = "Label_" & i.ToString
                AddHandler lbl.MouseDown, AddressOf ml_MouseDown
            End With
        Next

    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub


    Private Sub ml_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ml.MouseDown
        Dim lbl As MyLabel = CType(sender, MyLabel)
        lbl.Flag = Not lbl.Flag
        MsgBox(lbl.Flag.ToString())
    End Sub
End Class





测试它!



[/ EDIT]



Test it!

[/EDIT]