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.
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.