遍历winform标签 [英] looping through winform label

查看:62
本文介绍了遍历winform标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是VB的新手,我的表单包含100个标签.我需要将标签21-75的文本更改为"abc".我正在使用下面的代码,但它更改了所有标签.

Hi,

I''m new in VB, I''m having a form which contains 100 labels. I need to change the text of Label no 21-75 to "abc". I''m using the code below, but it''s changing all the label.

Dim cControl As Control
        For Each cControl In Me.Controls
            If (TypeOf cControl Is TextBox) Then
                cControl.Text = "abc"
            End If
        Next cControl



以及为什么我不能这样写:



and Why can''t I write like this:

Dim lbl As New Label()
        For i As Integer = 21 To 50
            lbl(i).Text = "abc"
        Next




感谢您的帮助.




Thanks for your help.

推荐答案

您不能这样写,因为lbl是标签的 new 数组,而不是标签上的标签表格.
这有点像购买新书架,并期望其中包含您所有的旧书!

如果您只想更改其中的一些,则可以:
1)在循环外设置一个整数变量,每次四舍五入时将其递增1.检查数字是否在范围内,并且仅对那些数字进行更改.
2)在每个TextBox的Tag字段中设置一个值,说我需要更改"或我不需要更改",然后在循环中进行检查.
3)使用Control.Name属性检查是否应更改文本.

顺便说一句:在同一窗体上具有100个控件可能是一个坏主意:用户很难确定要去哪里,要做什么.考虑使用TabControl或类似的控件对它们进行分组.


您能详细解释一下路线"标签吗?"


每个控件都有一个Tag属性,它是一个对象引用,因此可以在其中放置任何内容.它在那里,您可以将数据与控件关联,并将其作为sender参数的一部分传递给事件处理程序.
因为您可以在其中放置任何东西,所以可以将其用于任何目的.例如,您可以在其中放置一个整数,然后将其用于将相关控件组合"在一起.假设您在五个控件中放置了1个,在其他6个控件中放置了2个.
循环时,您可以查看Tag并确定控件是否为:
完全没有任何组(Tag为null)
在要更改为"ABC"的组中(标签为1)
在要更改为"DEF"的组中(标签为2)

You can''t write that because lbl is a new array of labels, and not the labels on the form.
It''s a bit like buying a new bookcase, and expecting it to contain all your old books!

If you only want to change some of them, then either:
1) Set up an integer variable outside the loop, and increment it by one each time you go round. Check if the number is inside the range and only change teh text on those that are.
2) Set a value into the Tag field of each TextBox which says "I need to be changed" or "I don''t need to be changed" and check that in the loop.
3) Use the Control.Name property to check if you should change the text.

BTW: It is probably a bad idea to have 100 controls on the same form: it becomes very difficult for the user to work out what goes where, and what does what. Consider grouping them by using a TabControl or similar instead.


"Can you explain the tag Route in some details?"


Every Control has a Tag property, which is an object reference so anything can be put in it. It is there so that you can associate your data with the control, and it will be passed through to event handlers as part of the sender parameter.
Because you can put anything in there, you can use it for any purpose. For example, you could put an integer in there, and use it to "group" related controls together. Say you put a 1 in five controls, and a 2 in a different 6 controls.
When you go round your loop, you can look at the Tag and decide if the control is:
In no group at all (Tag is null)
In the group to be changed to "ABC" (Tag is 1)
In the group to be changed to "DEF" (Tag is 2)

For Each c As Control In Controls
    Dim t As TextBox = TryCast(c, TextBox)
    If c.Tag IsNot Nothing AndAlso t IsNot Nothing Then
        Dim group As Integer = TryCast(c.Tag, [integer])
        Select Case group
            Case 1
                t.Text = "ABC"
                Exit Select
            Case 2
                t.Text = "DEF"
                Exit Select
        End Select
    End If
Next


这篇关于遍历winform标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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