Vb.net Windows窗体-传递数据和重用特定窗体
我有一个简单的申请表和两张表格。一个表单(form1)允许用户在列表框中选择记录。一旦做出选择,第二个表格(表格2)应使用第一个表格中的数据进行更新 用户应该能够选择不同的记录,第二个表单应该用新数据更新 我已经能够做到这一点,除了它使用Form.Show()在用户每次选择form1中的新记录时重新创建form2。我想要的是保持form2显示(一旦打开),然后在用户每次选择form1中的记录时刷新显示的数据 相关表格1代码:Vb.net Windows窗体-传递数据和重用特定窗体,vb.net,winforms,Vb.net,Winforms,我有一个简单的申请表和两张表格。一个表单(form1)允许用户在列表框中选择记录。一旦做出选择,第二个表格(表格2)应使用第一个表格中的数据进行更新 用户应该能够选择不同的记录,第二个表单应该用新数据更新 我已经能够做到这一点,除了它使用Form.Show()在用户每次选择form1中的新记录时重新创建form2。我想要的是保持form2显示(一旦打开),然后在用户每次选择form1中的记录时刷新显示的数据 相关表格1代码: Private Sub Form1_Load(sender As Ob
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
现在,表2中的相关代码:
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
我试过在form2和form1中使用form2.Refresh()和form2.Update()。我什么也得不到
我上面展示的只是创建了更多的form2版本,它不会更新form2的当前迭代。您需要保持对form2的引用处于活动状态,以便可以更新它,否则您将无法更新表单
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
您应该在Form1的模块级声明Form2,并将其实例化一次
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
现在,在DataGridView的事件处理程序中调用_form2上的update方法:
_form2.UpdateText(TextBox1.Text, Textbox2.Text)
被接受的答案是好的,但只是为了澄清这一点,它正在使用Form.Show()重新创建form2:Show()不重新创建表单;您可以在不同的上下文中使用此行
Dim newForm As New form2
创建两个不同的form2实例。Form.Show()将使表单可见,如果尚未加载表单,则会在Form.Show()上调用Form_Load。