【问题标题】:Calculate specific control occurance in a winform计算winform中的特定控件出现
【发布时间】:2013-04-08 02:06:39
【问题描述】:

一个winform包含多个放置在面板中的文本框,并且面板放置在选项卡控件的选项卡页中。所以,我想要的是我可以计算该 winform 中文本框控件的数量以及如何访问所有文本框。

欢迎提出建议。

【问题讨论】:

  • 您想在运行时执行此操作吗?还是基于源代码?如果是前者,您可以递归搜索加载的表单的控件树。如果是后者,“ctrl-F”不就够了吗?

标签: vb.net winforms visual-studio-2010 tabs textbox


【解决方案1】:

这是一段快速而肮脏的代码,演示了如何递归地遍历表单上的所有控件。它是递归的,这意味着它将深入挖掘其他容器控件。

    ' create a list of textboxes
    Dim allTextBoxes As New List(Of TextBox)

    ' call a recursive finction to get a list of all the textboxes
    ExamineControls(allTextBoxes, Me.Controls)

    ' run through the list and look at them
    For Each t As TextBox In allTextBoxes
        Debug.Print(t.Name)
    Next



Private Sub ExamineControls(allTextBoxes As List(Of TextBox), controlCollection As Control.ControlCollection)
    For Each c As Control In controlCollection
        If TypeOf c Is TextBox Then
            ' it's a textbox, add it to the collection
            allTextBoxes.Add(c)
        ElseIf c.Controls IsNot Nothing AndAlso c.Controls.Count > 0 Then
            ' it's some kind of container, recurse
            ExamineControls(allTextBoxes, c.Controls)
        End If
    Next
End Sub

您显然希望将此逻辑移动到表单的不同部分,并将结果存储在文本框类型的表单级列表中...

【讨论】:

    猜你喜欢
    • 2021-03-02
    • 1970-01-01
    • 2022-12-22
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多