为什么UserControl中的控件可以从usercontrol外部访问?

时间:2014-07-27 03:16:57

标签: .net vb.net

我有一个包含文本框的UserControl,当我将这个usercontrol放在一个表单上并进行所有控件的循环时,我可以看到usercontrol以及usercontrol中的控件,这意味着位于用户控件内的TextBox仍然可以从用户控件库外部访问

当我想要列出表单的所有控件(包括位于TabControl或GroupBox中的子控件)时,我的问题就出现了,我发现我的UserControls中的控件都被视为表单控件的一部分,这里是我的代码

Sub GetAllControls(Obj As Object, ByRef Controls As List(Of Control))
    For Each con As Control In Obj.Controls
        Controls.Add(con)
        If con.HasChildren Then GetAllControls(con, Controls)
    Next
End Sub

Private Sub frmPHASE_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim ls As New List(Of Control)
    Me.GetAllControls(Me, ls)
    Dim St As String = ""
    For Each c As Control In ls
        St = St & c.Name & ","
    Next
    If St <> "" Then St = St.Substring(0, St.Length - 1)
    MsgBox(St)
End Sub

0 个答案:

没有答案