VB.NET For each 枚举不正确

标签 vb.net visual-studio-2012 foreach

我有以下代码:

    For Each t As TabPage In Me.TabControl1.TabPages
        For Each p As Panel In t.Controls
            Dim sText As String = p.Name
            If modStrings.Has(sText, u) Then
                m_PrevPanel = p
                p.Parent = Me.pnlMain
                Return
            End If
        Next
    Next

但有时在排队

 For Each p as Panel in t.Controls

我得到了错误

“SystemWindows.Forms.Button 类型的对象无法转换为 System.Windows.Forms.Panel”。

我不明白为什么它会尝试在“p as Panel”枚举中包含一个按钮。 有人看到这里可能出了什么问题吗?

最佳答案

因为您在 TabPage 控件集合上有一个按钮。

尝试过滤它:

For Each p As Panel In t.Controls.OfType(Of Panel)()

Next

关于VB.NET For each 枚举不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21606730/

相关文章:

sql - SQL 表中的创建日期列

wpf - 给定 WPF 控件的内容之一,如何获取 ElementHost 控件

windows-8 - Windows 8 Metro 是否支持托管 c++/cli

c# - 是否可以更改 Visual Studio 中的编码约定

php - 在 foreach 循环期间将查询结果插入 php 字符串

c# - 是否可以在 asp.net[c#] 项目中添加一些 VB 类?

c# - Math.Round 在一台客户电脑上的奇怪行为

asp.net - 使用 ASP.NET 将二进制写入新选项卡/窗口

for-loop - XTend For-Loop 支持和添加范围支持

.net - 在 VB.NET 中使用 LINQ 的 ForEach 和匿名方法