如何等待 Visual Basic 2010 窗体的按钮单击事件结束
在我的 vb 程序中,我有两种协同工作的表单,允许您定义几乎任意数量的设计。在第一种形式中,有一个二维数组,存储一个整数(不重要)和一个ID类,我在另一种形式中将其定义为公共内部类。单击此表单中的“定义 ID”按钮将带您进入下一个表单,您将在其中定义新 ID 或根据您在第一个表单的组合框中选择或输入的数组中的索引来定义新 ID 或编辑旧 ID,在第二种形式中使用多个字段来定义其不同方面。我想要发生的是,当用户在第二个表单中定义 ID,然后单击第二个表单上的“继续”按钮时,第一个表单的代码将被挂起。经过一些错误检查后,表单要么暂时隐藏(这就是我最初实现它的方式),要么返回编辑后的 ID 对象,但前提是错误检查没有表明任何输入不正确。最初,我只是使用 newForm.curID 获取 ID 并将其设置为数组中的位置,等待用户使用 newForm.showDialog() 完成,但有时程序会返回 null 异常,因为我不这样做我认为我正确地复制了 ID。此外,尽管错误出现的时间很短,但“继续”按钮会保存 ID,无论其正确性如何。有没有办法操纵 showDialog 以从中获取结果响应?
简短版本:我需要以一种形式将内部类的对象返回到调用形式的数组,或者等待形式完成并使用调用形式复制其对象,然后关闭被调用者形式。
第一个表单的“定义”按钮方法
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项目有时有点令人沮丧。
In my vb program, I have two forms that work together to allow you to define an almost arbitrary amount of designs. In the first form, there is a two-dimensional array, storing an integer (not important) and an ID class that I defined as a public inner class in another form. Clicking on the "Define ID" button in this form will take you to the next form, where you will define a new ID or edit an old one based off of an index in the array you selected or entered in the first form's combo box, using multiple fields in the second form to define its different aspects. What I want to happen is the first form's code to be suspended as the user defines the ID in the second form, then they click on the Continue button on the second form. After a bit of error-checking, the form either is momentarily hidden (which is how I originally implemented it) or returns the edited ID object, but only if the error-checking does not indicate any input is incorrect. Originally, I simply grabbed the ID by using newForm.curID and set that to its spot in the array, waiting for the user to finish by using newForm.showDialog(), but sometimes the program would return a null exception because I don't think I copy the IDs correctly. Also, though the errors show up for a small amount of time, the Continue button saves the ID regardless of its correctness. Is there a way to manipulate the showDialog to get a result response out of it?
Short version: I need to either return an object of an inner class in one form to an array in a calling form or wait for a form to finish and use the calling form to copy its object, then close the callee form.
First Form's "Define" button method
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
Second Form's "Continue" button method
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
Thanks for any and all help! I'm much more proficient in Java, so this VB project is a bit frustrating sometimes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第二个表单上,创建一个如下所示的方法:
然后从第一个表单中调用该方法。
编辑:另外,让第二个表单的Continue方法调用Me.Close。这将关闭第二个表格。 ShowDialog 调用将阻塞,直到窗体关闭。
第一个表单不应调用第二个表单的 close 方法。
On your second form, make a method like this:
then call that from your first form.
EDIT: also, have your second form's Continue method call Me.Close. This will close the second form. The ShowDialog call will block until the form is closed.
The first form should not be calling the second form's close method.