如何等待Visual Basic 2010表单按钮单击事件结束

时间:2011-05-20 16:43:02

标签: vb.net visual-studio-2010

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

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

First Form的“Define”按钮方法

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项目有时会让人感到沮丧。

1 个答案:

答案 0 :(得分:0)

在你的第二张表格上,制作一个这样的方法:

public function ReturnSomething(curId as whateverType) as whateverReturnType

  me.curId = curId
  me.showDialog
  return someValue

end function

然后从你的第一张表格中打电话。

编辑:还有,你的第二个表格的继续方法叫Me.Close。这将关闭第二个表格。 ShowDialog调用将阻塞,直到表单关闭。

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