简体   繁体   中英

Update one form based on selections from another form

I apologise if the title is a bit vague, i've only been on here a day.

So my problem is I have a menu form in which I input the options from the comboboxes. And then I go to the next form which shows the relevant imported text file info. However when I click the 'back' button to return to the menu and input different information in the comboboxes, it doesn't take me to the correct text file info, it just shows the info from the previous selection.

here is the student menu pic

here is the text file form

below is the code for the student menu next button:

    If OptionBox.Text = "Introduction" Then

        Introduction.Show()

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


End Sub

below is the code for the text file form load page and the back button

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

what do i need to do in order to update this information so that the program doesn't skip going through the form again.

There is a lot of repeated code there. Here is a way to reduce it (see DRY ) and expose a method to change the topic. In a Module:

Public Enum Topics
    ComputerSystems
    Hardware
    Software
    Data
    Database
    Networks
End Enum

Then in the form that shows the text:

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

By the way, VB does has an ElseIf which can avoid the "arrow" anti pattern you can see in your code. At the very least, the excessive indentation is annoying.

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

Show that form normally using an instance of the form class:

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

Then invoke the method to tell it which topic to display. Displaying topic info is a separate method from loading a form first because the form load event happens only once. A specialized method to do what we want makes more sense since they really have nothing to do with one another, and makes it easier to see how the code works:

info.DisplayTopic(Topics.ComputerSystems)
info.Show

This way, you dont have one form fiddling with the controls on another, but still have a clear way of communicating which topic to display.

Note that the location of the topics file(s) is a bit different. You'd want a "gcsecomputingtask" folder in MyDocuments for the files. The VS project folder is not a good place for it, the folder location could change depending on which machine you are running on (yours or computer lab etc). They could also be stored as a resource to skip that part too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM