2016-06-10 129 views
1

我正在製作一個「首選項表單」,它將保存所有用戶首選項,以及何時應用/保存我希望將新值傳回主窗體並更新並關閉表單2。在過去,我已經這樣做了這樣的:在表單之間傳遞數據DIRECTLY

Private Sub PreferencesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PreferencesToolStripMenuItem.Click 
    Preferences.Show() 
End Sub 

,當我點擊「應用/保存」按鈕將其關閉之前,我會轉移所有的數據是這樣的:

form1.textbox.text = form2.textbox.text 

這有什麼錯這樣做?

我一直在閱讀是我應該做這樣的:

Private Sub PreferencesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PreferencesToolStripMenuItem.Click 
    Dim dialog As New Preferences 
    dialog.ShowDialog() 
End Sub 

當他們點擊「應用/保存」,將採取從Form2的所有的值,並將其存儲在私有變量時(或財產)的窗體2和當窗體關閉然後我會像這樣訪問值:

Private Sub PreferencesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PreferencesToolStripMenuItem.Click 
    Dim dialog As New Preferences 
    dialog.ShowDialog() 

    form1.textbox.text = dialog.variable 
End Sub 

爲什麼會變成這樣這樣做的更好的辦法?

UPDATE ....查看下面的代碼,這只是我將擁有的所有選項的一個小樣本。在序列化時將數據收集到對象中的最佳方式是什麼?

<Serializable> 
Public Class Preference 

#Region "Properties" 
    Public Property ScaleLowest As String = "5" 
    Public Property ScaleHighest As String = "200" 
    Public Property ScaleInc As String = "5" 
    Public Property ThickLowest As Double = 0.125 
    Public Property ThickHighest As Double = 4 
    Public Property ThickInc As Double = 0.125 
    Public Property WidthLowest As Double = 0.125 
    Public Property WidthHighest As Double = 0.6 
    Public Property WidthInc As Double = 0.125 
    Public Property LengthLowest As Double = 1 
    Public Property LengthHighest As Double = 96 
    Public Property LengthInc As Double = 1 
    Public Property FractionON As Boolean = False 
    Public Property DecimalON As Boolean = True 
    Public Property ColorSelection As String = "Colors" 
    Public Property FinalColor As String = "255, 255, 0" 
    Public Property roughColor As String = "255, 255, 100" 
    Public Property SplashON As Boolean = False 
    Public Property LogInON As Boolean = False 
#End Region 

    Public Sub New() 
     'for creating new instance for deserializing 
    End Sub 

    Public Sub GatherAllData() 

     'Save Defaults 
     SaveSerializeObj() 

    End Sub 

    Public Sub SaveSerializeObj() 
     'Get Changes????? 

     'Serialize object to a text file. 
     Dim objStreamWriter As New StreamWriter("C:\Users\Zach454\Desktop\test.xml") 
     Dim x As New XmlSerializer(Me.GetType) 
     x.Serialize(objStreamWriter, Me) 
     objStreamWriter.Close() 

    End Sub 

    Public Function LoadSerializeObj() As Preference 

     'Check if new file need created 
     If File.Exists("C:\Users\454\Desktop\test.xml") = False Then 
      SaveSerializeObj() 
     End If 

     'Deserialize text file to a new object. 
     Dim objStreamReader As New StreamReader("C:\Users\454\Desktop\test.xml") 
     Dim newObj As New Preference 
     Dim x As New XmlSerializer(newObj.GetType) 
     newObj = CType(x.Deserialize(objStreamReader), Preference) 
     objStreamReader.Close() 

     Return newObj 
    End Function 
+1

如果您有很多用戶選項,您可能希望有一個班負責加載,保存,顯示和編輯它們。然後,任何需要了解「SaveFolder」或其他任何角色的演員都可以從該類中獲取它。一旦你使用OOP,在表單之間傳遞數據很少發生。另外,您最終會使用默認表單實例來敲擊磚牆。 – Plutonix

+0

在form1和form2之外創建一個不同的類? – goomba454

+0

是的。每個用戶選項都可以是一個屬性(或某些情況下的方法),以便其他演員可以在不加載某些表單的情況下獲取所需的任何內容;表單就是應用程序/代碼如何與用戶進行交互。對於一小部分選項,您可以使用內置的'設置' – Plutonix

回答

0

最好的選擇是創建一個具有表單控件屬性的類。然後您可以存儲這些屬性,然後在需要時訪問這些屬性。還有沒有理由來回傳遞數據,你可以將這些數據存儲在某個地方(數據庫,文件,mysettings等),然後將這些數據加載到一個類中。然後你可以存儲和檢索這個類的數據。然後,如果您需要將數據保存回某個地方,則需要使用類對象。

下面是一個簡短的例子,展示如何創建另一個表單(首選項)單擊保存,然後將這些值顯示回另一個表單(調用表單)。

這是主要的形式

Public Class Form1 
    Public _frm2 As Form2 

    Private Sub btnShowPreferences_Click(sender As Object, e As EventArgs) Handles btnShowPreferences.Click 
     Using _frm2 As New Form2() 
      'if we clicked save on the form then show the values in the 
      'controls that we want to 
      If _frm2.ShowDialog() = Windows.Forms.DialogResult.OK Then 
       txtFirstName.Text = _frm2._Preferences.FirstName 
       txtLastName.Text = _frm2._Preferences.LastName 
      End If 
     End Using 
    End Sub 

End Class 

下面是一個例子(首)類

這個類持有偏好的所有屬性。這是一個例子,你可以改變任何你需要的來滿足你的需求。

Option Strict On 

Public Class Preferences 

#Region "Properties" 
    Public Property FirstName As String 
    Public Property LastName As String 
#End Region 

    Public Sub New() 

    End Sub 

End Class 

第二Form可能是你的(偏好)的形式與用戶需要與之交互的所有控件。

Public Class Form2 

    Public _Preferences As New Preferences 'create class variable you can use for later to store data 

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click 
     'set your properties of the class from your form. this will then hold everything you can get from 
     'the first form... 
     With _Preferences 
      .FirstName = txtFirstName.Text 
      .LastName = txtLastName.Text 
     End With 

     Me.DialogResult = Windows.Forms.DialogResult.OK 'this is used to determine if user clicked a save button... 

    End Sub 
End Class 

我希望這個得到你的開始,如果你不明白的東西請讓我知道。

+0

感謝您的建議Zaggler,我添加了一個更新的代碼片段,將這些數據收集到這個類或對象中的最佳方式是什麼?我感謝您的幫助! – goomba454

+0

@ goomba454如果您在上面查看'Form2'的部分,您將看到我使用變量'_Preferences'設置控件屬性的位置。這就是你需要做的一切。例如'.ScaleLowest = txtScaleLowest.Text',你可以從控件值設置你的屬性... – Codexer

+0

好吧,我明白了,所以我可能有很多變量,我必須至少硬編碼一次!謝謝你的幫助! – goomba454

0

要直接回答您的問題,您的兩個代碼示例的主要區別在於第二個代碼示例使用ShowDialog以模態方式打開表單,而第一個示例允許您在打開第二個示例時與父窗體進行交互。
從用戶流量控制的角度來看,第二種方法可能會更好。如果真正的問題是將數據推回到主窗體還是從對話框中提取數據,最好從對話框中拉出。這種方法使得對話可以從其他表單重用。