0

I have 2 forms in my program. Form1 has a tab control on it, and on one of these tabs there are a load of labels. Form2 has a few textboxes and dropdown lists on it. Form1 has a button on it that opens Form2 on top of it as a normal form, not as a dialog.

There is code in Form1 that on loading populates the labels on it from a MySQL database. This code is in a separate public sub in the form that is called when the form loads.

What I am trying to do is fill in some of the boxes on Form2, when this Form closes it updates the database with these values (Works fine) and then those values are displayed on Form1. I can get Form2 to run the code in Form1 that populates the labels, but the problem is that the labels on Form1 never actually change. I have tried multiple things, including calling refresh on the labels, trying to change the labels from within Form2 instead of Form1, but they just never update. The .text value of the labels is being updated to the new value, I have checked this via debugging and stopping at the correct points to see what the value is.

Any ideas anyone?? I think it might be to do with the fact that it's Form2 calling the code and for some reason it doesn't have access to change the labels on Form1.

Code:

Form1 (AECSurveyForm)

Private Sub AECSurvey_Load(sender As Object, e As EventArgs) Handles     MyBase.Load

    LoadMeterData()

End Sub


Public Sub LoadMeterData()

    Dim AECMeteringDataAdapter As New AECMeteringDataTableAdapter
    Dim AECMeteringData As AECMeteringDataDataTable
    AECMeteringData = AECMeteringDataAdapter.GetAECMeterDataBySurveyUniqueIdentifier(AECGlobalValues.CurrentSurveyUniqueIdentifier)

    'utility

    Meter1UtilityLabel.Text = AECMeteringData(0)("Utility1")

End Sub

Private Sub MeterButton1_Click(sender As Object, e As EventArgs) Handles Meter1Button.Click

    If Not Application.OpenForms().OfType(Of AECMeteringDataForm).Any Then

        AECMeteringDataForm.GetData(AECGlobalValues.CurrentSurveyUniqueIdentifier, 1)

        AECMeteringDataForm.Show()

    End If

End Sub

Form2 (AECMeteringDataForm)

Private Sub AECMeteringDataForm_FormClosing(sender As Object, e As EventArgs) Handles MyBase.Closing

    Dim AECMeteringDataAdapter As New AECMeteringDataTableAdapter
    Dim AECMeteringData As AECMeteringDataDataTable
    AECMeteringData = AECMeteringDataAdapter.GetAECMeterDataBySurveyUniqueIdentifier(AECGlobalValues.CurrentSurveyUniqueIdentifier)

    AECMeteringData(0)("Utility1") = UtilityComboBox.SelectedItem.ToString

    AECMeteringDataAdapter.Update(AECMeteringData)

    AECSurveyForm.LoadMeterData()

End Sub
  • you have probably run afoul of VB's default form instances; hard to tell for certain without code – Ňɏssa Pøngjǣrdenlarp Feb 04 '15 at 11:44
  • I'm not quite sure what default form instances means. Have added code to show what I'm doing. – Robert Quick Feb 04 '15 at 12:07
  • there are several dubious things there, but one would be default form instances. Ironically, you have code there to get the form2 instance (`Application.OpenForms`) but dont use it. See [Form Controls not updating](http://stackoverflow.com/a/28290434/1070452) – Ňɏssa Pøngjǣrdenlarp Feb 04 '15 at 12:24
  • Plutonix, thanks for your help, I've managed to get it working. I now understand that the code wasn't referencing the correct form. I will add in the code as shown on your reply for all of my forms. What else did you see that was dubious?? – Robert Quick Feb 04 '15 at 12:40
  • slight correction: "the code wasn't referencing the correct form *instance*". That seems like a lot of repeated code...frm2 updates the db; then frm1 basically repeats the code to read a value. If the forms shared a data source (such as in a class) the changes should just flow thru – Ňɏssa Pøngjǣrdenlarp Feb 04 '15 at 12:52
  • Thanks for the correction, that's what I meant. I've now changed the code so it updates the values on frm1 directly from frm2, totally bypassing re-reading the data from the db as I can now directly change the values of the labels. How do I accept your answer as the correct one? – Robert Quick Feb 04 '15 at 12:58

0 Answers0