2
votes

I have a simple application with 2 forms. One form (form1) allows a user to select a record in a ListBox. Once the selection is made, the second form (form2) should be updated with data from the first form.

The user should be able to select a different record, and the second form should be updated with the new data.

I have been able to do this, except it is using Form.Show() which recreates form2 each time the user selects a new record in form1. What I want is to keep form2 displayed (once opened), then refresh the data being displayed each time the user selects the record in form1.

Pertinent form1 code:

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

    Dim newForm As New form2
    newForm.Location = Screen.AllScreens(LBound(Screen.AllScreens)).Bounds.Location
    newForm.Hide()

End Sub

Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles BoatsGrid.CellMouseClick
    If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
        Dim selectedRow = gridview1.Rows(e.RowIndex)
        TextBox1.Text = gridview1.CurrentRow.Cells(1).Value.ToString()
        Textbox2.Text = gridview1.CurrentRow.Cells(2).Value.ToString()
        Textbox3.Text = gridview1.CurrentRow.Cells(0).Value.ToString()

        Dim newForm As New form2

        newForm.Location = Screen.AllScreens(LBound(Screen.AllScreens)).Bounds.Location

        newForm.UpdateText(TextBox1.Text, Textbox2.Text)
        newForm.Show()

    End If
End Sub

Now, the pertinent code from form2:

Public Class WeightDisplay
    Private Sub WeightDisplay_Load() Handles MyBase.Load

    End Sub

    Public Sub UpdateText(TextString1 As String, TextString2 As String)

        Label1.Text = TextString1 
        Label2.Text = TextString2

    End Sub
End Class

I have tried using form2.Refresh() and form2.Update() from within form2 and from form1. I get nothing.

What I am showing above just creates more versions of form2, it does not update the current iteration of form2.

2
The accepted answer is good but just to clarify this it is using Form.Show() which recreates form2: Show() doesn't recreate the form; you create two different instances of form2 with this line Dim newForm As New form2 in different contexts. Form.Show() will make the form visible and Form_Load will be called upon Form.Show(), if it is not loaded already.djv

2 Answers

0
votes

You need to keep a reference to form2 alive so you can update it, otherwise you will not be able to update the form.

Dim newForm As New form2
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


    newForm.Location = Screen.AllScreens(LBound(Screen.AllScreens)).Bounds.Location
    newForm.Hide()

End Sub
Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles BoatsGrid.CellMouseClick
    If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
        Dim selectedRow = gridview1.Rows(e.RowIndex)
        TextBox1.Text = gridview1.CurrentRow.Cells(1).Value.ToString()
        Textbox2.Text = gridview1.CurrentRow.Cells(2).Value.ToString()
        Textbox3.Text = gridview1.CurrentRow.Cells(0).Value.ToString()

        newForm.Location = Screen.AllScreens(LBound(Screen.AllScreens)).Bounds.Location

        newForm.UpdateText(TextBox1.Text, Textbox2.Text)
        newForm.BringToFront()

    End If
End Sub
0
votes

You should declare Form2 at the module level in Form1, and instantiate it once.

Public Class Form1

    Private _form2 As New Form2 = Nothing

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _form2 = New Form2

    End Sub

End Class

Now, you call the update method on _form2 in the DataGridView's event handler:

_form2.UpdateText(TextBox1.Text, Textbox2.Text)