Vb.net Visual Studio多窗体

Vb.net Visual Studio多窗体,vb.net,winforms,visual-studio,Vb.net,Winforms,Visual Studio,所以我有一个关于我建立的程序的问题。首先,我的课本不是很好的资源。其次,我想我已经做了所有的事情,除了让总成本流入正确的标签。基本上我有两种形式,第一种是我的信息,第二种是你在会议上选择你想参加的内容。当我运行此命令时,一切都正常,除了总数没有从选项窗体交叉到主窗体。我束手无策。有什么建议吗 主要表格编号: Public Class MainForm Private Sub selectButton_Click(sender As Object, e As EventArgs) Han

所以我有一个关于我建立的程序的问题。首先,我的课本不是很好的资源。其次,我想我已经做了所有的事情,除了让总成本流入正确的标签。基本上我有两种形式,第一种是我的信息,第二种是你在会议上选择你想参加的内容。当我运行此命令时,一切都正常,除了总数没有从选项窗体交叉到主窗体。我束手无策。有什么建议吗

主要表格编号:

Public Class MainForm

    Private Sub selectButton_Click(sender As Object, e As EventArgs) Handles selectButton.Click
        ' Create an instance of the Conference options form
        Dim frmOptions As New Conference_Options

        ' Display the form
        frmOptions.ShowDialog()
    End Sub

    Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
        ' Close the form
        Me.Close()
    End Sub

    Private Sub resetButton_Click(sender As Object, e As EventArgs) Handles resetButton.Click
        ' Clear everything
        nameTextBox.Clear()
        companyTextBox.Clear()
        addressTextBox.Clear()
        cityTextBox.Clear()
        stateTextBox.Clear()
        zipTextBox.Clear()
        phoneTextBox.Clear()
        emailTextBox.Clear()
        totalLabel.Text = ""
    End Sub
    End Class
Public Class Conference_Options

    Function TotalCost(ByRef dblTotalCost)
        Dim dblRegistration As Double
        Dim dblDinnerandReg As Double
        Dim dblPreCon As Double

        'conference registration
        If registrationCheckBox.Checked = True Then
            dblRegistration = 895
        Else
            dblRegistration = 0
        End If

        'dinner and keynote speech
        If dinnerCheckBox.Checked = True Then
            dblDinnerandReg = 30
        Else
            dblDinnerandReg = 0
        End If

        'optional preconference workshop
        If preconferenceListBox.SelectedIndex = 0 Then
            dblPreCon = 295
        ElseIf preconferenceListBox.SelectedIndex = 1 Then
            dblPreCon = 295
        ElseIf preconferenceListBox.SelectedIndex = 2 Then
            dblPreCon = 395
        ElseIf preconferenceListBox.SelectedIndex = 3 Then
            dblPreCon = 395
        ElseIf preconferenceListBox.SelectedIndex = -1 Then
            dblPreCon = 0
        End If
        dblTotalCost = dblRegistration + dblDinnerandReg + dblPreCon
        Return dblTotalCost

    End Function
    Private Sub closeButton_Click(sender As Object, e As EventArgs) Handles closeButton.Click

        ' Close the form
        Me.Close()
    End Sub

    Private Function totalLabel() As Object
        Throw New NotImplementedException
    End Function
    End Class
选项表单代码:

Public Class MainForm

    Private Sub selectButton_Click(sender As Object, e As EventArgs) Handles selectButton.Click
        ' Create an instance of the Conference options form
        Dim frmOptions As New Conference_Options

        ' Display the form
        frmOptions.ShowDialog()
    End Sub

    Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
        ' Close the form
        Me.Close()
    End Sub

    Private Sub resetButton_Click(sender As Object, e As EventArgs) Handles resetButton.Click
        ' Clear everything
        nameTextBox.Clear()
        companyTextBox.Clear()
        addressTextBox.Clear()
        cityTextBox.Clear()
        stateTextBox.Clear()
        zipTextBox.Clear()
        phoneTextBox.Clear()
        emailTextBox.Clear()
        totalLabel.Text = ""
    End Sub
    End Class
Public Class Conference_Options

    Function TotalCost(ByRef dblTotalCost)
        Dim dblRegistration As Double
        Dim dblDinnerandReg As Double
        Dim dblPreCon As Double

        'conference registration
        If registrationCheckBox.Checked = True Then
            dblRegistration = 895
        Else
            dblRegistration = 0
        End If

        'dinner and keynote speech
        If dinnerCheckBox.Checked = True Then
            dblDinnerandReg = 30
        Else
            dblDinnerandReg = 0
        End If

        'optional preconference workshop
        If preconferenceListBox.SelectedIndex = 0 Then
            dblPreCon = 295
        ElseIf preconferenceListBox.SelectedIndex = 1 Then
            dblPreCon = 295
        ElseIf preconferenceListBox.SelectedIndex = 2 Then
            dblPreCon = 395
        ElseIf preconferenceListBox.SelectedIndex = 3 Then
            dblPreCon = 395
        ElseIf preconferenceListBox.SelectedIndex = -1 Then
            dblPreCon = 0
        End If
        dblTotalCost = dblRegistration + dblDinnerandReg + dblPreCon
        Return dblTotalCost

    End Function
    Private Sub closeButton_Click(sender As Object, e As EventArgs) Handles closeButton.Click

        ' Close the form
        Me.Close()
    End Sub

    Private Function totalLabel() As Object
        Throw New NotImplementedException
    End Function
    End Class

我希望你能看到,我确实付出了很大的努力,但我正在努力让它工作。

通常这种问题是用一个有两个按钮的窗体来处理的,一个标记为确认,DialogResult属性设置为DialogResult.OK,另一个标记为取消,DialogResult属性设置为DialogResult.Cancel

现在,当用户按下确认按钮时,您可以在TotalCost函数中执行代码,但将结果保存在表单的全局属性中

像这样的

Dim totalCost as Decimal
Public Readonly Property TotalCost as Decimal
     Get
          Return totalCost
     End Get
End Property

Private Sub confirmButton_Click(sender As Object, e As EventArgs) Handles confirmButton.Click

   totalCost = CalcTotalCost()

End Sub

Private Function CalcTotalCost() as Decimal

    ' This is your previous function TotalCost renamed to
    ' avoid conflicts with the property (and now it is private)

    Dim decRegistration As Decimal
    Dim decDinnerandReg As Decimal
    Dim decPreCon As Decimal

    'conference registration
    If registrationCheckBox.Checked = True Then
        decRegistration = 895
    Else
        decRegistration = 0
    End If

    'dinner and keynote speech
    If dinnerCheckBox.Checked = True Then
        decDinnerandReg = 30
    Else
        decDinnerandReg = 0
    End If

    'optional preconference workshop
    If preconferenceListBox.SelectedIndex = 0 Then
        decPreCon = 295
    ElseIf preconferenceListBox.SelectedIndex = 1 Then
        decPreCon = 295
    ElseIf preconferenceListBox.SelectedIndex = 2 Then
        decPreCon = 395
    ElseIf preconferenceListBox.SelectedIndex = 3 Then
        decPreCon = 395
    ElseIf preconferenceListBox.SelectedIndex = -1 Then
        decPreCon = 0
    End If

    Return decRegistration + decDinnerandReg + decPreCon
End Function 
现在,您可以通过这种方式调用frmOptions并获取值

Public Class MainForm

    Private Sub selectButton_Click(sender As Object, e As EventArgs) Handles selectButton.Click
        ' Create an instance of the Conference options form
        Using frmOptions = New Conference_Options
            ' Display the form and check if the user confirms
            DialogResult result = frmOptions.ShowDialog()
            If result = DialogResult.OK Then
                ' Read the value calculated in the Confirm event handler
                Dim total = frmOptions.TotalCost
            End If
        End Using ' Closing and destroying the options form
    End Sub

请注意,我已将变量从Double改为Decimal,因为在处理货币值时更适合使用此类型。

是否尝试将TotalCost公开为公共属性在选项表单中从函数中设置其值,并将其命名为:frmOptions.TotalCost,既然你已经在主窗体中持有它的一个实例了?