如何将值从Form1传递到Form2
我正在制作一个生成SQL Server代码的程序,以便在我的VB.NET程序中使用它。
I'm making a program that generates SQL Server code to use it in my VB.NET program.
我有第一个包含您这样的连接的表格参见下图:
I have this first form that contains the connection like you see in picture below:
连接有效100%,但是在第二个表单中有两个 DataGridView
s,一个用于表,一个用于字段。
The connection works 100%, but in the second form I have two DataGridView
s, one for tables and one for fields.
因此,当我单击任何表时 DataGridView1
=> DataGridView2
的显示字段:
So when I click on any table of DataGridView1
=> DataGridView2
show it fields:
当我单击 DataGridView1
来获取 ComboBox的值时
在 Form1
中的 Form2
中使用它我遇到以下错误:
When I click on DataGridView1
to get the value of ComboBox
from Form1
to use it in Form2
I have the following error:
无法连接到服务器。
Failed to connect to server.
代码:
Dim frm As New Form2
prd.ServerConnection = New ServerConnection(frm.ComboServer.Text) ' here the error
prd.DGVField(MetroGridTables, MetroGridField)
我使用 Form1
进行连接,并使用 Form2
进行操作。
I use Form1
to make connection and Form2
to make operation.
将值从一种形式传递给另一种形式的最简单方法是实现 New
要将值传递给的方法:
The simplest way to pass a value from one form to another is to implement the New
method on the form you want to pass the value to:
Form1
:
Public Class Form1
Private Sub btnPass_Click(sender As Object, e As EventArgs) Handles btnPass.Click
Dim form As New Form2(TextBox1.Text)
form.Show()
End Sub
End Class
Form2
:
Public Class Form2
Public Sub New(ByVal value As String)
' This call is required by the designer.
InitializeComponent()
Label1.Text = value
End Sub
End Class
截图: