[英]Update one form based on selections from another 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
我需要做些什么才能更新此信息,以便程序不會再次跳過該表單。
那里有很多重復的代碼。 這是減少它的一種方法(請參閱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
,它可以避免您在代碼中看到的“箭頭”反模式 。 至少,過度的縮進是令人討厭的。
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項目文件夾不是一個理想的選擇,文件夾位置可能會根據運行的計算機(您或計算機實驗室等)而變化。 也可以將它們存儲為資源以跳過該部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.