【问题标题】:Why does Microsoft Visual Basic skip over part of this code为什么 Microsoft Visual Basic 跳过此代码的一部分
【发布时间】:2013-09-14 23:37:00
【问题描述】:

我正在尝试使我的表单上的一些标签可见,但我不想使用很多 if 语句,但出于某种原因,每当我将 Me.Controls(lbl).Visbel = True 放入 for 或 do 循环时它跳过整个循环。代码完全按照我想要的方式运行,直到我为整个类而不是在 From_load 私有子中调用 Dim lbl = Controls("Label" & counter_3) 时出错。有时我可以让它工作,但只有一个标签可见

Dim chararray() As Char = word_list(random_word).ToCharArray
Dim lbl = "Label" & counter_3

    For Each item In chararray
        If item = Nothing Then
        Else
            word_list(counter_2) = item.ToString()
            counter_2 += 1
        End If
    Next

    For Each item In chararray
        If item = Nothing Then

        Else
            counter_3 += 1
            Me.Controls(lbl).Visible = True
            MsgBox(item & " " & counter_3)
        End If
    Next

我也试过了。在这两个循环中都被完全跳过。我知道这是因为 MsgBox 没有出现。

Dim chararray() As Char = word_list(random_word).ToCharArray
Dim lbl = Controls("Label" & counter_3)

    For Each item In chararray
        If item = Nothing Then
        Else
            word_list(counter_2) = item.ToString()
            counter_2 += 1
        End If
    Next

    For Each item In chararray
        If item = Nothing Then

        Else
            counter_3 += 1
            lbl.Visble = True
            MsgBox(item & " " & counter_3)
        End If
    Next

【问题讨论】:

  • 如果您在 WinForm 事件处理程序中抛出异常,它通常会被捕获并静默忽略。尝试在调试器中为所有 C# 异常启用“Break on throw”。
  • 你能告诉我更多细节吗,我还是一个初级程序员,我不确定你的意思。
  • 另外,也许你应该试试If item <> Nothing Then... Endif,而不是If item = Nothing Then... ' no action... Else... Endif。我不确定在 VB 中如何处理 If 子句中的空子句,但一般情况下最好避免使用空条件子句。至少,它们会让你的代码更难阅读。
  • 我尝试启用'break on throw' C# 异常,但它仍然跳过代码

标签: vb.net winforms visual-studio


【解决方案1】:

我注意到的是,您正在根据从 word_list 返回的随机单词创建 Char 数组,然后使用数组中字符的计数作为索引遍历 Char 数组进入您的 word_list,如果您的单词中的字符数量超过列表中的单词数量,您将收到一个错误,并且由于此错误出现在 Forms Load 事件中,它将被吞下,之后的所有代码都将被中止.还有其他一些我想改变的问题,比如确保所有声明都有一个类型,我可能会改用Controls.Find 方法并检查它是否有一个实际的对象。但我可能首先要做的是将您的代码移动到一个单独的子例程中,并在 Forms Constructor(New) 方法中的 IntializeComponent 调用之后调用它。

类似的东西。

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    YourMethod
End Sub


Public Sub YourMethod()
    Dim chararray() As Char = word_list(random_word).ToCharArray
    Dim lbl As Control() = Controls.Find("Label" & counter_3, True)

    For Each item In chararray
        If item = Nothing Then
        Else
            word_list(counter_2) = item.ToString()
            counter_2 += 1
        End If
    Next

    For Each item In chararray
        If item = Nothing Then

        Else
            counter_3 += 1
            If lbl.Length > 0 Then
                lbl(0).Visible = True
            Else
                MsgBox("Control not Found")
            End If
            MsgBox(item & " " & counter_3)
        End If
    Next
End Sub

【讨论】:

  • 这是修复它的好方法。虽然我已经找到了解决方法,但我会考虑到这一点。
  • 我将在发布此问题的 8 小时内发布对此的修复。
  • @PygamePi 没关系,我想指出的是,Form 的 Load EventHandler 中的 any 错误将被吞下,不会在未运行后引发任何代码,如图所示,您最好将表单的构造函数与单独的方法一起使用。
  • 好吧,我仍然需要编写更简洁的代码。地雷有点乱。
猜你喜欢
  • 1970-01-01
  • 2014-10-21
  • 1970-01-01
  • 1970-01-01
  • 2021-08-16
  • 2021-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多