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.
it is using Form.Show() which recreates form2
: Show() doesn't recreate the form; you create two different instances of form2 with this lineDim 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