每个循环中的问题 [英] Problem in For Each Loop

查看:107
本文介绍了每个循环中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友.
我有一个带有一些标签和一个图片框的表单.
我想使所有标签透明.我用下面的代码来做到这一点.但是,它使某些标签透明,而跳过其他标签.

Dear Friends.
I have a form with some labels and a picturebox.
I want to make all the labels transparent. I have used the following code to do that. But, It makes some of the labels transparent and skips the others.

Dim lbls As Control
      For Each lbls In Me.Controls
          If TypeOf lbls Is Label Then
              lbls.Parent = PictureBox1
              lbls.BackColor = Color.Transparent
          End If
      Next




因此,请Plz帮助我如何在我的窗体上使所有标签透明.




So, Plz Help me that How can I make all the labels transparent on my Form.

推荐答案

在这里您可以进行以下操作:
视频教程:使标签透明化 [线程讨论 [
Here you go:
Video Tutorial: Make a label transparent[^]


BTW, based on the issue you are facing might be because of label co-ordinates. Have a look at this thread discussion [^]that sounds similar.


如果您的表单包含容器控件(如面板或组框),您的代码将丢失这些控件.您将需要检查任何面板或组框,并遍历它们包含的用于搜索标签的控件.
If your form contains a container control like a panel or groupbox, your code will miss those. You''ll need to check any panel or groupbox and loop through the controls they contain searching for labels as well.


上面的代码应该可以工作.您什么时候运行代码?在Form_Load中吗?

这样可以使表单上的所有标签都透明,但是如果其他标签在例如面板,它们不会受到影响,因为它们不在Me.Controls(窗体控件集合)中.

确保所有标签都在表格上...

另一个解决方案是使函数递归:

The above code should work. When are you running the code? In Form_Load?

That would make all the labels on the form transparent but if there are other labels in e.g. panels, they would not be affected because they are not in Me.Controls (the forms control collection).

Make sure that all the labels are on the form...

Another solution is to make the function recursive:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        MakeLabelsTransparent()
    End Sub

    Private Sub MakeLabelsTransparent()
        LoopThroughCollection(Me)
    End Sub

    Private Sub LoopThroughCollection(ByVal parent As Control)
        Dim lbls As Control

        For Each lbls In parent.Controls
            If TypeOf lbls Is Label Then
                lbls.Parent = PictureBox1
                lbls.BackColor = Color.Transparent
            Else
                LoopThroughCollection(lbls)
            End If
        Next lbls
    End Sub



请注意:如果将标签的父级设置为PictureBox1,并且标签完全或部分位于图片框之外,则将导致部分或全部标签完全不显示...

祝你好运



Please note: If you set a labels parent to PictureBox1 and the label is positioned completely or partially outside the picturebox, it will make a part or all of the label not show up at all...

Good luck


这篇关于每个循环中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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