【问题标题】:How to wait for a Visual Basic 2010 form's button-click event to end如何等待 Visual Basic 2010 表单的按钮单击事件结束
【发布时间】:2011-05-21 02:14:15
【问题描述】:

在我的 vb 程序中,我有两种形式协同工作,允许您定义几乎任意数量的设计。在第一种形式中,有一个二维数组,存储一个整数(不重要)和一个 ID 类,我在另一种形式中定义为公共内部类。单击此表单中的“定义 ID”按钮将带您进入下一个表单,您将在其中定义新 ID 或根据您在第一个表单的组合框中选择或输入的数组中的索引来编辑旧 ID,在第二种形式中使用多个字段来定义其不同方面。我想要发生的是第一个表单的代码被暂停,因为用户在第二个表单中定义了 ID,然后他们单击第二个表单上的继续按钮。经过一些错误检查后,表单要么暂时隐藏(这是我最初实现它的方式),要么返回编辑后的 ​​ID 对象,但前提是错误检查未指示任何输入不正确。最初,我只是使用 newForm.curID 获取 ID 并将其设置到数组中的位置,等待用户使用 newForm.showDialog() 完成,但有时程序会返回 null 异常,因为我没有认为我正确复制了 ID。此外,尽管错误显示的时间很短,但“继续”按钮会保存 ID,而不管其正确性如何。有没有办法操纵 showDialog 以获得结果响应?

短版:我需要以一种形式将内部类的对象返回到调用形式的数组中,或者等待形式完成并使用调用形式复制其对象,然后关闭被调用形式。

First Form 的“定义”按钮方法

Private Sub DesignButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DesignButton.Click
    Me.totalCoilBox.Focus() 'calls validating method, makes textbox yellow if invalid'

    If totalCoilBox.BackColor = Color.White Then
        Dim newForm As New IVD_DesignData 'creates an object of ID called curID as well'

        Try
            If DesignIDBox.Items.Contains(DesignIDBox.Text) Then 'design exists
                set the curID to the one already in the array'
                newForm.curID = CGID(Integer.Parse(DesignIDBox.Text), 1)
                'set number of coils to new number, if applicable'
                CGID(Integer.Parse(DesignIDBox.Text), 0) = Integer.Parse(DesignIDBox.Text)

                'show dialog for editing'
                newForm.ShowDialog()
                'put the (edited) curID back into the array'
                CGID(Integer.Parse(DesignIDBox.Text), 1) = newForm.curID
            Else 'design does not exist, make a new one
                put number of coils into new array spot'

                CGID(Integer.Parse(DesignIDBox.Text), 0) = Integer.Parse(totalCoilBox.Text)
                'set the design ID to the number of defined IDs plus one'
                newForm.curID.designID = Integer.Parse(DesignIDBox.Text)
                'show dialog for editing'
                newForm.ShowDialog()
                'add curID into the array'
                CGID(Integer.Parse(DesignIDBox.Text), 1) = newForm.curID
                'add that design number to the dropdown box'
                DesignIDBox.Items.Add(newForm.curID.ToString())
                'increment number of defined designs'
                Current_IDBox.Text = Integer.Parse(Current_IDBox.Text) + 1
            End If

        Catch ex As ArgumentOutOfRangeException 'dropdown box has no elements in it;
            do the same as adding a new ID:
            put number of coils into new array spot'
            CGID(Integer.Parse(DesignIDBox.Text), 0) = Integer.Parse(totalCoilBox.Text)
            'set the design ID to the number of defined IDs plus one'
            newForm.curID.designID = Integer.Parse(DesignIDBox.Text)
            'show dialog for editing'
            newForm.showDialog()
            'add curID into the array'
            CGID(Integer.Parse(DesignIDBox.Text), 1) = newForm.curID
            'add that design number to the dropdown box'
            DesignIDBox.Items.Add(newForm.curID.ToString())
            'increment number of defined designs'
            Current_IDBox.Text = Integer.Parse(Current_IDBox.Text) + 1
        End Try
        DesignIDBox.Text = newForm.curID.ToString()
        newForm.Close()
    End If

第二个表单的“继续”按钮方法

Private Sub ContinueButton_Click(ByVal sender As System.Object, ByVal e As ByVal e As System.EventArgs) Handles ContinueButton.Click
    Me.NTBox.Focus()
    Me.IDBox.Focus()
    Me.ODBox.Focus()
    Me.TBox.Focus()
    Me.WBox.Focus()
    If HSCBox.Checked Then
        Me.HSC_MultBox.Focus()
    Else
        curID.HSC = "n"
        curID.HSC_Mult = 1
    End If

    If NTBox.BackColor = Color.White And IDBox.BackColor = Color.White And ODBox.BackColor = Color.White And TBox.BackColor = Color.White And WBox.BackColor = Color.White And ((HSCBox.Checked And HSC_MultBox.BackColor = Color.White) Or Not (HSCBox.Checked)) Then
        'success!  window falls back?'
    End If
End Sub

感谢您的任何帮助!我对Java比较精通,所以这个VB项目有时有点令人沮丧。

【问题讨论】:

    标签: vb.net visual-studio-2010


    【解决方案1】:

    在你的第二个表单上,创建一个这样的方法:

    public function ReturnSomething(curId as whateverType) as whateverReturnType
    
      me.curId = curId
      me.showDialog
      return someValue
    
    end function
    

    然后从您的第一个表单中调用它。

    编辑:另外,让您的第二个表单的 Continue 方法调用 Me.Close。这将关闭第二个表单。 ShowDialog 调用将一直阻塞,直到窗体关闭。

    第一个表单不应该调用第二个表单的 close 方法。

    【讨论】:

    • 这是朝着正确方向迈出的一步,但不幸的是,除非我理解错了,否则单击 Continue 后我的错误检查仍然会被忽略。
    猜你喜欢
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 2020-04-25
    • 1970-01-01
    相关资源
    最近更新 更多