【问题标题】:Update one form based on selections from another form根据另一个表单的选择更新一个表单
【发布时间】:2016-01-03 21:38:07
【问题描述】:

如果标题有点模糊,我很抱歉,我才在这里一天。

所以我的问题是我有一个菜单表单,我可以在其中输入来自组合框的选项。然后我转到下一个显示相关导入文本文件信息的表单。 但是,当我单击“返回”按钮返回菜单并在组合框中输入不同的信息时,它不会带我进入正确的文本文件信息,它只会显示上一个选择的信息。

here is the student menu pic

here is the text file form

下面是学生菜单下一步按钮的代码:

    If OptionBox.Text = "Introduction" Then

        Introduction.Show()

    Else
        If OptionBox.Text = "Explanation" Then
            Explanation.Show()
        End If
    End If


End Sub

下面是文本文件表单加载页面和返回按钮的代码

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

    Student_Menu.Hide()

    Dim font As New System.Drawing.Font("Calibri", 11)

    If Student_Menu.TopicSelect.Text = "Computer Systems" Then

        Dim strFile As String = "C:\Users\Sales\Documents\Visual Studio 2010\Projects\gcsecomputingtask\textfiles\Introduction\ComputerSystems.txt"
        Dim sr As New IO.StreamReader(strFile)
        IntroductionLabel.Text = sr.ReadToEnd()

        sr.Close()
    Else
        If Student_Menu.TopicSelect.Text = "Hardware" Then
            Dim strFile As String = "C:\Users\Sales\Documents\Visual Studio 2010\Projects\gcsecomputingtask\textfiles\Introduction\Hardware.txt"
            Dim sr As New IO.StreamReader(strFile)
            IntroductionLabel.Text = sr.ReadToEnd()

            sr.Close()
        Else
            If Student_Menu.TopicSelect.Text = "Software" Then
                Dim strFile As String = "C:\Users\Sales\Documents\Visual Studio 2010\Projects\gcsecomputingtask\textfiles\Introduction\Software.txt"
                Dim sr As New IO.StreamReader(strFile)
                IntroductionLabel.Text = sr.ReadToEnd()
            Else
                If Student_Menu.TopicSelect.Text = "Representation of Data" Then
                    Dim strFile As String = "C:\Users\Sales\Documents\Visual Studio 2010\Projects\gcsecomputingtask\textfiles\Introduction\RepresentationOfData.txt"
                    Dim sr As New IO.StreamReader(strFile)
                    IntroductionLabel.Text = sr.ReadToEnd()
                Else
                    If Student_Menu.TopicSelect.Text = "Databases" Then
                        Dim strFile As String = "C:\Users\Sales\Documents\Visual Studio 2010\Projects\gcsecomputingtask\textfiles\Introduction\Databases.txt"
                        Dim sr As New IO.StreamReader(strFile)
                        IntroductionLabel.Text = sr.ReadToEnd()
                    Else
                        If Student_Menu.TopicSelect.Text = "Communications & Networks" Then
                            Dim strFile As String = "C:\Users\Sales\Documents\Visual Studio 2010\Projects\gcsecomputingtask\textfiles\Introduction\Hardware.txt"
                            Dim sr As New IO.StreamReader(strFile)
                            IntroductionLabel.Text = sr.ReadToEnd()
                        End If
                    End If
                End If
            End If
        End If
    End If
    IntroductionLabel.Font = font


End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
    Me.Hide()

    Student_Menu.Show()

    Student_Menu.TopicSelect.ResetText()
    Student_Menu.OptionBox.ResetText()


End Sub

我需要做什么才能更新此信息,以使程序不会再次跳过表单。

【问题讨论】:

    标签: vb.net windows forms combobox


    【解决方案1】:

    那里有很多重复的代码。这是一种减少它的方法(请参阅DRY)并公开一种更改主题的方法。在一个模块中:

    Public Enum Topics
        ComputerSystems
        Hardware
        Software
        Data
        Database
        Networks
    End Enum
    

    然后以显示文字的形式:

    Friend Sub DisplayTopic(topic As Topics)
        Dim text As String
        Dim filname As String = ""
    
        Select Case topic
            Case Topics.ComputerSystems
                filname = "ComputerSystems.txt"
            Case Topics.Database
                filname = "Databases.txt"
                '... etc
        End Select
    
        ' attach path
        filname = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                             "gcsecomputingtask", filname)
    
        text = File.ReadAllText(filname)
        IntroductionLabel.Text = text
    End Sub
    

    顺便说一句,VB 确实有一个ElseIf,它可以避免您在代码中看到的"arrow" anti pattern。至少,过多的缩进很烦人。

        If topic = Topics.ComputerSystems Then
            '...
        ElseIf topic = Topics.Software Then
            '...
        End If
    

    使用表单类的实例正常显示该表单:

    Public Class MenuForm    ' a form is just a class
        ' declare an object variable to use
        Private info As Form2     ' whatever its name is (Explanation???)
        ....
        Private Sub MenuForm_Load(...)
           ' create an instance to be used later:
           info = New Form2
    

    然后调用该方法来告诉它要显示哪个主题。显示主题信息是与首先加载表单不同的方法,因为表单加载事件仅发生一次。一种专门的方法来做我们想做的事情更有意义,因为它们之间真的没有任何关系,并且更容易看到代码是如何工作的:

    info.DisplayTopic(Topics.ComputerSystems)
    info.Show
    

    这样,您不会让一个表单与另一个表单上的控件发生冲突,但仍然可以清楚地传达要显示的主题。

    请注意,主题文件的位置有些不同。您需要MyDocuments 中的“gcsecomputingtask”文件夹来存放文件。 VS 项目文件夹不是一个好地方,文件夹位置可能会根据您正在运行的机器(您的机器或计算机实验室等)而改变。它们也可以存储为资源以跳过该部分。

    【讨论】:

    • 由于用户可以通过多种方式退出表单,一旦您执行info.Show(),home/main/picker 表单可能会自行重置选择。 SelectedIndex = -1 时忽略对 CBO 的更改
    • 好的,所以现在我为主题创建了一个新模块,我已将 DisplayTopic 子放在显示文本的介绍表单中。我将私人信息作为新的介绍并放入 info.displaytopic - 但是我如何结束该私人信息,它应该是私人子信息。
    • 我的代码中没有private sub info。我展示了使用实例使用表单的正确方法。只需将Private info As New Form???(使用您的表单名称)放在菜单表单的顶部即可。我猜你的班级名称是 Explanation 但那是一个 class 你应该创建它的一个实例来显示(info 在我的回答中)
    • 我扩展了答案的那部分......他们不是在你们学校教创建表单实例吗?
    • 我现在已在我的学生菜单表单中的公共课程位下方添加了私人信息作为新介绍。当我放入公共子显示主题(ByVal 主题作为主题)时,我将它放在介绍表单的公共类下,并且出现错误说“主题”无法通过“介绍”类在项目外部公开类型“Module1.Topics” .
    猜你喜欢
    • 2013-05-25
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 2018-10-13
    • 2022-01-06
    相关资源
    最近更新 更多