[英]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.
这是减少它的一种方法(请参阅DRY ),并提供了一种更改主题的方法。 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. 顺便说一句,VB确实有一个
ElseIf
,它可以避免您在代码中看到的“箭头”反模式 。 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. 您需要
MyDocuments
中的“ gcsecomputingtask”文件夹作为文件。 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). VS项目文件夹不是一个理想的选择,文件夹位置可能会根据运行的计算机(您或计算机实验室等)而变化。 They could also be stored as a resource to skip that part too.
也可以将它们存储为资源以跳过该部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.