vb.net 从 VB 中的另一种形式获取价值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17662915/
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-17 14:15:59  来源:igfitidea点击:

Getting value from another form in VB

vb.netwinformseventsradio-button

提问by Shawn

So I have these two forms: Form1 and Form2. The first form has a groupbox with radio buttons in it that selects a dormitory with values. At the bottom of the page is a button that says continue and goes to Form 2. In Form2 there is another button that has a groupbox with radios to select a meal plan. At the bottom of the page there is a button that says calculate. When this button is clicked Form1 should show again and there textbox for total cost will have a value in it for the dormcost and mealcost. It works for getting the meal cost, but not the dorm.

所以我有这两种形式:Form1 和 Form2。第一个表单有一个带有单选按钮的分组框,用于选择带有值的宿舍。在页面底部是一个按钮,上面写着继续并转到 Form 2。在 Form2 中还有另一个按钮,它有一个带有收音机的 groupbox,用于选择膳食计划。在页面底部有一个按钮,上面写着计算。单击此按钮时,Form1 应再次显示,并且总成本的文本框将包含宿舍成本和膳食成本的值。它适用于获得膳食费用,但不适用于宿舍。

Anybody have any idea why?

有人知道为什么吗?

Form 1:

表格一:

Public Class Form1

Public DormCost As Integer

Private Sub ButtonCalculate_Click(sender As Object, e As EventArgs) Handles ButtonCalculate.Click

    Dim DormCost As Integer

    If RadioAllen.Checked = True Then
        DormCost = 1500
    ElseIf RadioPike.Checked = True Then
        DormCost = 1600
    ElseIf RadioFarthing.Checked = True Then
        DormCost = 1200
    ElseIf RadioUniversity.Checked = True Then
        DormCost = 1800
    End If

    Form2.Show()

End Sub
End Class

Form 2:

表格2:

Public Class Form2

Dim MealCost As Integer
Dim Total As String


Private Sub ButtonCalculate_Click(sender As Object, e As EventArgs) Handles ButtonCalculate.Click


    If Radio7.Checked = True Then
        MealCost = 560
    ElseIf Radio14.Checked = True Then
        MealCost = 1095
    ElseIf RadioUnlimited.Checked = True Then
        MealCost = 1500
    End If

    Me.Close()

    Total = Convert.ToString(Form1.DormCost + MealCost)
    Form1.TextBox1.Text = "$" + Total


End Sub
End Class

回答by Chase Ernst

You can make a global variable class, which can be used all throughout your program.

您可以创建一个全局变量类,它可以在整个程序中使用。

Public Class GlobalVariables

Public Class GlobalVariables

Public Shared yourvariable As yourtype

End Class

End Class

Put this class at the bottom of one of your forms and you can call the variable form anywhere in the program. GlobalVariables.yourvariable

把这个类放在你的一个表单的底部,你可以在程序的任何地方调用变量表单。 GlobalVariables.yourvariable

回答by logixologist

Here is another option:

这是另一种选择:

Make DormCostlocal to form1 and call it say myDormCostand put it as Public to Form2 then when you call

DormCost本地设置为 form1 并将其称为 saymyDormCost并将其设置为 Public 到 Form2 然后当您调用时

form2.show

right before that do

在那之前做

form2.DormCost = myDormCost

That should work.

那应该有效。