Visual Basic - 发布连接表单和显示输入

Visual Basic - Issue connecting forms and displaying input

我有一个正在为项目编写的基本程序,您可以在其中订购蜡烛。在表格 (frm_Order) 上,我有用于输入姓名、地址、邮政编码等的标签。然后你点击继续,它会带你到下一个表格。在那个表格上,它是第一个表格 (frm_items) 上的客户信息和购物车内容 (lst_cart) 的订单摘要。

我有两个框总结了订单,我希望输出是购物车中的商品以及来自包含客户信息的订单的输入。如何编写模块以显示来自先前表单的那些输入?另外,当它打开每个程序表格时,我如何关闭前一个表格,这样就没有 3 windows 打开?

好吧,我认为您可以在其中一个或两个表单中实现此目的。我认为您可以使用 Funtions 而不是 Module 来做到这一点。我不是很清楚您拥有的工具以及您将如何在要显示的表单上显示它的顺序。您似乎有 3 个表格,但您还没有清除他们所做的,因此将使用两个表格向您展示。

您可以在第一个表格中进行选择,然后在第二个表格中您可以看到您在第一个表格中选择的内容以及客户姓名:

在 form1 上,您需要(将使其尽可能简单)2 个文本框,第一个命名为 txtname,第二个命名为 txtsurname。

添加一个 ComboBox 并将其命名为 cmbitem 添加一个按钮并将其命名为 btnorder


现在让我们转到form2,我相信你已经添加了它。 在顶部添加标签并将其命名为 lblorder_from 添加一个列表框并将其命名为 lstitem 添加一个按钮并将其命名为关闭

现在让我们开始编码。

返回 form1 并双击订购按钮并输入此代码。

 ' First check if textboxs are not null.
 If txtname.Text = "" Or txtsurname.Text = "" Then
 ' The textboxs or one of them has nothing typed so display error
 MsgBox("The fields can not be empty, please type something")
 Else
 ' We are good, textboxs are not null
 ' Declare our variables that we will use.
 Dim Name, Surname, Item As String ' You can also just not use this, instead just call the controls it self
 ' Assigning values to our variables
 Name = txtname.Text
 Surname = txtsurname.Text
 Item = cmbitem.SelectedItem.ToString
 ' Transfering data from this form to the second form.
 Form2.lblitem_from.Text = Name & " " & Surname & "'s order"
 lstitem.Items.Add(Item)
 ' Hide this form before showing the second form.
 Me.Visible = False
 ' Show the second form
 Form2.show

现在转到 Form2 并双击按钮关闭按钮并输入此代码。

 Application.Exit()
 ' This will close all forms but if you used the "Close" it would close only form2 so you would need to make some changes on a project properties to enable that the project should close if form2 is closing because right not by defualt it is set to close only when form1 is closing.

希望这能让您了解如何在表单之间传输 items/data,您现在就可以 运行 然后您必须至少在 ComboBox 上双击添加一个项目form1 表单转到 form_load() 并输入此

 ' Add items to combobox
 cmbitem.Items.Add("Rice")
 cmbitem.Items.Add("Pie")

那么现在就可以运行了,填好controls然后点击order按钮就可以看到了

输出应该是,在第二个表单上您应该看到名字和姓氏,在列表框中您应该看到您选择的项目。根据您的要求,一次只能显示一个表格。