vb.net 在 VS 2010 中的窗体之间传递变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17707795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:00:05  来源:igfitidea点击:

Passing variables between windows forms in VS 2010

vb.netwinformsvisual-studio-2010variables

提问by gwhenning

I have two forms. Form 1 allows the user to pick an employee from a dropdown combo box. That employee is then passed onto Form 2 where the user enters additional information regarding that employee. That data will then be passed into a SQL table.

我有两种形式。表单 1 允许用户从下拉组合框中选择员工。然后将该员工传递到表格 2,用户在其中输入有关该员工的其他信息。然后,该数据将被传递到 SQL 表中。

On form 1 I have:

在表格 1 上,我有:

Dim ChangeJobInfo As New Form2(Employee)
ChangeJobInfo.Show()

On Form 2 I have:

在表格 2 上,我有:

Public Sub New(ByVal Employee As String)
MsgBox(Employee)
End Sub

The variable passes just fine. The issue is that nothing shows up on the new form. When I setup Form2, I added a combobox, date picker, two text boxes, submit button, etc., but when the form loads it is completely blank. No errors, the MsgBox returns the right result, but none of my gui elements show up. If I change the code on form 1 to Form2.show() I see the form as laid out in the designer.

变量通过就好了。问题是新表格上没有显示任何内容。当我设置 Form2 时,我添加了一个组合框、日期选择器、两个文本框、提交按钮等,但是当表单加载时它是完全空白的。没有错误,MsgBox 返回正确的结果,但没有显示我的 gui 元素。如果我将表单 1 上的代码更改为 Form2.show(),我会看到设计器中布局的表单。

Any ideas on how to get those items to show up?

关于如何让这些项目出现的任何想法?

回答by Dave H

Change your code in Form2.vb for the Newsub to this:

将您在 Form2.vb 中的代码更改为New

Public Sub New(ByVal Employee As String)

    ' This call is required by the designer.

    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    MsgBox(Employee)

End Sub

If you don't call InitializeComponent(), your complete GUI is not going to render.

如果您不调用InitializeComponent(),您的完整 GUI 将不会呈现。

回答by Eric F

You don't even have to use the InitializeComponent or New functions.

您甚至不必使用 InitializeComponent 或 New 函数。

I have made an example to show how easily this can be done.

我已经举了一个例子来说明这可以多么容易地完成。

enter image description here

在此处输入图片说明

Clicking "Show Form" results in the below:

单击“显示表单”结果如下:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Form2.Show()
End Sub

which is simply used to display the second form.

它只是用来显示第二种形式。

By clicking "Pass Data" results in the following code:

单击“传递数据”会产生以下代码:

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    Form2.Label1.Text = TextBox1.Text
End Sub

As shown above you can pass the data directly from control to control. The same idea can be used with variables too.

如上所示,您可以直接将数据从控件传递到控件。同样的想法也可以用于变量。

回答by lucullo08

I am late but I think this answer can help.

我迟到了,但我认为这个答案会有所帮助。

For example, Form1 named "menu" opens and passes variable to Form2 named "ordine". The variable to pass is "hotel"

例如,名为“menu”的 Form1 打开并将变量传递给名为“ordine”的 Form2。要传递的变量是“酒店”

In menu on button_click

在 button_click 上的菜单中

Dim ordinef As New Ordine()

If ordinef Is Nothing Then
    'control that form is not opened yet if open close before
    ordinef = New Ordine()
Else
    ordinef.Close()
    ordinef = New Ordine()
End If

ordinef.hotel = hotel

ordinef.Show()

In Form2 (Ordine):

在 Form2 (Ordine) 中:

Private Sub Ordine_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

public hotel as string

msgbox hotel

That's it!!

就是这样!!