简体   繁体   中英

Controls on form1 not updated if form2 is open

When I hit the submit button I want the entered data to appear in textboxes on groupForm, but this isn't happening. I have to restart the form or hit the Refresh button for the data to appear.

在此处输入图片说明

UPDATE

I zoned in on the problem;

I have to pass arguments to the form so it knows which groups' data to show, like this;

Dim frmReceiver As New form_addNewProfitObject
frmReceiver.setGroup(theMainGroupID, theGroupID)
frmReceiver.ShowDialog()

If I open the form like this, the data entered appears just fine without refreshing;

form_addNewProfitObject.Show()

But this way the form wouldn't know what group to add data to. Is there a work around? I also tried to do frmReceiver.Show() instead of frmReceiver.ShowDialog() but that doesn't make a difference.

I'm not entirely sure why the method I am using isn't working.. maybe it's because upon hitting 'submit' it calls groupForm.NewProfitObjectsItem(ByVal *snip*) and this doesn't work if groupForm was created using new ?

END UPDATE

Here's the code for hitting the 'refresh' button on Form number one ( groupForm.vb ).

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    populate(Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID))
End Sub

And here's the code for the Submit button, which (at the end) calls that exact same function, but from form_addNewProfitObject.vb .

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Try

        Dim groupID As Integer = theGroupID
        'New ProfitObject
        ReDim Preserve Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID).ProfitObjects(Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID).ProfitObjects.Length)
        Dim ProfitObjectID As Integer = Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID).ProfitObjects.Length - 2
        Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID).ProfitObjects(ProfitObjectID) = New ProfitObject
        'Set ProfitObject properties
        'Set ID
        Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID).ProfitObjects(ProfitObjectID).id = ProfitObjectID
        'Set Name
        Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID).ProfitObjects(ProfitObjectID).name = TextBox1.Text
        'Set Profit
        Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID).ProfitObjects(ProfitObjectID).profit = TextBox2.Text
        'Set TypeID
        Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID).ProfitObjects(ProfitObjectID).typeID = TextBox3.Text
        'Set Rarityd
        Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID).ProfitObjects(ProfitObjectID).rarity = TextBox4.Text
        'populate(Form1.main.Groups(groupID))
        Dim textt As String = "ID" & ProfitObjectID.ToString
        groupForm.NewProfitObjectsItem(textt, Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID).ProfitObjects(ProfitObjectID).name, Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID).ProfitObjects(ProfitObjectID).profit, Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID).ProfitObjects(ProfitObjectID).rarity, Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID).ProfitObjects(ProfitObjectID).typeID)

        groupForm.populate(Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID))

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

    groupForm.Refresh()
    Me.Close()
End Sub

I added groupForm.populate() at the end of the latter function for testing purposes, but I know that shouldn't be necessary ("populate" just loops over all items in the ProfitObjects array and calls NewProfitObjectsItem for each one).

Anyway; when I submit the data from the second form, the groupForm doesn't automatically show the new data. I either have to hit the refresh button or restart the form (it calls populate() on form load).

I simply want the new item to appear immediately after Submitting it. And it should, because that's what groupForm.NewProfitObjectsItem() does. And to top it off I added groupForm.populate() . I'm assuming it's not working because it's called from an external form, or because the other form is open/has the focus.. Any ideas?

I do not know what is wrong with your code. Probably you can find the bug by using the debugger. Set a breakpoint in the first line of the "Submit" button click subroutine and go through the process step by step. May be it will help you to figure out what is happening.

One point, which seems suspicious to me, is that you ReDim the ProfitObjects array with the same size it already has. This creates a new array. If the other form has set a reference to this array before the ReDim with

Dim a = Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID).ProfitObjects

Then this variable a would still point to the old array before it was ReDimmed !


Something completely different. In the comments, it was mentioned that you should refactor your code. I did a few refactorings in this sense. Does this not look much better?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
        Dim groupID As Integer = theGroupID
        Dim groupObject As GroupObject = Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID)

        'New ProfitObject 
        ReDim Preserve groupObject.ProfitObjects(groupObject.ProfitObjects.Length)

        Dim ProfitObjectID As Integer = groupObject.ProfitObjects.Length - 2
        Dim profit As ProfitObject = New ProfitObject()
        groupObject.ProfitObjects(ProfitObjectID) = profit

        'Set ProfitObject properties 
        profit.id = ProfitObjectID
        profit.name = TextBox1.Text
        profit.profit = TextBox2.Text
        profit.typeID = TextBox3.Text
        profit.rarity = TextBox4.Text

        'populate(Form1.main.Groups(groupID)) 
        Dim textt As String = "ID" & ProfitObjectID
        groupForm.NewProfitObjectsItem(textt, profit.name, profit.profit, profit.rarity, profit.typeID)

        groupForm.populate(groupObject)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

    groupForm.Refresh()
    Me.Close()
End Sub

Simplify the parameter list of NewProfitObjectsItem like this

Public Sub NewProfitObjectsItem(ByVal profit As ProfitObject)
    Dim textt As String = "ID" & profit.id
    '...
End Sub

Then you can simplify the call further

'populate(Form1.main.Groups(groupID)) 
groupForm.NewProfitObjectsItem(profit)

Remember the original (here with additional line breaks)

'populate(Form1.main.Groups(groupID)) 
Dim textt As String = "ID" & ProfitObjectID.ToString
groupForm.NewProfitObjectsItem(textt, Form1.main.Groups(theMainGroupID) _
  .GroupObjects(theGroupID).ProfitObjects(ProfitObjectID).name, _
  Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID) _
  .ProfitObjects(ProfitObjectID).profit, Form1.main.Groups(theMainGroupID) _
  .GroupObjects(theGroupID).ProfitObjects(ProfitObjectID).rarity, _
  Form1.main.Groups(theMainGroupID).GroupObjects(theGroupID) _
  .ProfitObjects(ProfitObjectID).typeID)                    

One more thing. form.ShowDialog() is a blocking call. This means that the code does not continue at this point until the form is closed! If you are working with several forms at the same time, definitely use form.Show() .

Figured out what was causing it. Since I was using New to create & show " groupForm ", " form_addNewProfitObject " wasn't adding controls to the correct form when it called groupForm.NewProfitObjectsItem .

I had to move the Public frmReceiver As New groupForm into the CGroupMain class so that form_addNewProfitObject can access it like this; Form1.main.Groups(theMainGroupID).frmReceiver.NewProfitObjectsItem

The controls are now correctly added to the groupForm upon hitting submit .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM