如何在运行时更改多个标签的文本(逐个) [英] How to change text of multiple labels (one by one) during runtime

查看:79
本文介绍了如何在运行时更改多个标签的文本(逐个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我在我的vb表格上有一个5标签。

现在我想在运行时更改或设置文本和其他属性。

我想这样做只需选择一个标签并设置文字和其他财产。



我的机制应该是......



我选择label1从文本框中设置文本,从组合中设置fontsize,从组合中设置字体等等

然后是furher ....

Here i heve a 5 label on my vb form.
Now i want change or set a Text and other property at RUN TIME.
I want to do it as just select a label and set a text and other property.

My machanism should be as..

I have select label1 set its text from textbox,fontsize from combo,font from combo...etc
and then furher....

推荐答案

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



这是一个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]


这篇关于如何在运行时更改多个标签的文本(逐个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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