【问题标题】:Visual Studio Multi FormsVisual Studio 多表单
【发布时间】:2015-10-13 04:07:51
【问题描述】:

所以我有一个关于我构建的程序的问题。首先,我的教科书不是一个好的资源。其次,我已经完成了一切,“我认为”,除了让总成本流入正确的标签。所以基本上我有两个表格,第一个有我的信息,第二个是你选择你想参加会议的地方。当我运行它时,一切正常,除了总数没有从选项表单交叉到主表单。我已经黔驴技穷了。有什么建议么?

主窗体代码:

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

我希望你看到我确实付出了很多努力,但我正在努力让它发挥作用。

【问题讨论】:

  • 您是否尝试在选项表单中将 TotalCost 公开为公共属性(从函数中设置其值)并像这样调用它:frmOptions.TotalCost,因为您已经在 Main 表单中保存了它的实例?

标签: vb.net winforms visual-studio


【解决方案1】:

通常使用具有两个按钮的表单来处理此类问题,一个标记为“Confirm”,DialogResult 属性设置为 DialogResult.OK,另一个标记为“Cancel”,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,因为在处理货币值时使用这种类型更合适。

【讨论】:

    猜你喜欢
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多