直接在表单之间传递数据 [英] Passing data between forms DIRECTLY

查看:29
本文介绍了直接在表单之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个首选项表单",它将保存所有用户的首选项,当他们去应用/保存时,我希望新值传输回主表单并更新和关闭表单 2.过去我是这样做的:

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

当他们单击应用/保存"时,它将从 Form2 中获取所有值并将它们存储在 Form2 中的私有变量(或属性)中,当该表单关闭时,我将像这样访问该值:

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

推荐答案

最好的选择是创建一个具有表单控件属性的类.然后您可以存储这些属性,然后在需要时访问这些属性.

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.

此外,真的没有理由来回传递数据,您可以将这些数据存储在某个地方(数据库、文件、mysettings 等),然后将这些数据加载到一个类中.然后您可以从这个类中存储和检索数据.然后,如果您需要将数据保存回某个地方,您可以使用一个类对象.

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).

这是主要形式

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

这是一个示例(首选项)类

这个类将保存您的所有首选项属性.这是一个示例,您可以根据需要更改任何内容.

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

第二个 Form 可以是您的(首选项)表单,其中包含用户需要与之交互的所有控件.

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.

这篇关于直接在表单之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆