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.variableEnd 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 SubEnd 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 OnPublic Class Preferences#Region "Properties" Public Property FirstName As String Public Property LastName As String#End Region Public Sub New() End SubEnd 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 SubEnd Class我希望这能让你开始,如果你不明白什么,请告诉我.I hope this get's you started, if you do not understand something please let me know. 这篇关于直接在表单之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 13:35
查看更多