Passing data between forms DIRECTLY

无人久伴 提交于 2021-02-19 09:16:26

问题


I'm making a "Preference form" that will hold all the users preferences and when they go to Apply/Save I want the new values to transfer back to the main form and updateand close the form2. In the past I have done this like this:

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

and when I click the "Apply/Save" button before it closes I would Transfer all data like this:

form1.textbox.text = form2.textbox.text

Is there anything wrong doing it this way??

What I have been reading is I should be doing it like this:

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

And when when they click "Apply/Save" it would take all the values from Form2 and store them in a private variable (or Property) in Form2 and when that form closes I would then access the value like this:

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

Why would this be a better way of doing this?

UPDATE....Looking at the code below this is just a SMALL sample of all the options I will have. What is the best way to collect of the data into the object to use when serializing?

    <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:


The best option is to create a class that would have properties for your form controls. Then you can store these properties and then access these when needed.

Also there's really no reason to be passing data back and forth, you can store this data off somewhere (database, file, mysettings etc) and then load this data up into a class. Then you can store and retrieve data from this class. Then if you need to save data back to somewhere you have a class object to use.

Here is a short example to show how you can create another form (Preferences) click save and then show those values back on the other form (calling form).

This is the main form

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

Here is an example (Preferences) class

This class would hold all your properties for the preferences. This is an example, you can change anything you need to suit your needs.

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

The second Form could be your (Preference) form with all the controls a user would need to interact with.

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

I hope this get's you started, if you do not understand something please let me know.




回答2:


To directly answer your question, the main difference in your two code samples is that the second uses ShowDialog to open the form modally, vs the first sample which lets you interact with the parent form while the second is open.
The second approach may be better from the view of user flow control. If your real question is whether to push data back to the main form or pull data from the dialog, it is probably better to pull from the dialog. This approach makes the dialog reusable from other forms.



来源:https://stackoverflow.com/questions/37738223/passing-data-between-forms-directly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!