我正在为我的实习项目提供一些帮助。
在VB项目中,我有一个Windows窗体(Form1.vb),其中包含一个名为WaveForm1的自定义用户控件(其作用类似于示波器的图形)。我可以运行VB程序并为通道赋值,以便在项目运行时在WaveForm1用户控件中获取波形。
然后,我需要使用SaveFileDialog将WaveForm1与图表中绘制的频道一起保存在自定义文件扩展名(.gph,.wfm,..)下。
当使用OpenFileDialog通过按钮(btnOpen)打开时,保存的文件应该可以在vb项目中打开。
只要可以查看保存的图表,如何在项目中打开文件并不重要。 (例如,保存的文件可以在Form1.vb中的另一个WaveForm2控件中查看,也可以在单独的窗口中查看。)如果可以使用btnOpen从项目中保存并再次打开整个窗体,也可以。按钮控制。
我搜索过自定义文件创建&保存文件,我所能找到的是如何使用StreamWriter / Reader,binaryReader / Writer等保存文本文件,Excel文件或图像。
我非常感谢有关保存除文本文件或绘图之外的任何内容的任何帮助。
如果您不清楚我的问题,请随时与我确认。
答案 0 :(得分:2)
您应该尝试创建一个包含某些属性的类,然后使用BinarryFormatter
保存该类。您可以将自定义扩展程序提供给该类,并通过SaveFileDialog
保存,然后按OpenFileDialog
打开。
<强>类强>
<Serializable()>
Public Class myGraph
Private _value1 As String
Public Property Value1 As String
Get
Return _value1
End Get
Set(value As String)
_value1 = value
End Set
End Property
Private _value2 As String
Public Property Value2 As String
Get
Return _value2
End Get
Set(value As String)
_value2 = value
End Set
End Property
Private _value3 As String
Public Property Value3 As String
Get
Return _value3
End Get
Set(value As String)
_value3 = value
End Set
End Property
End Class
保存和加载文件的方法
''To Save file
Public Sub SaveFile(GRAPH_1 As myGraph)
''To Save File
Dim dlgSave As New SaveFileDialog
dlgSave.Filter = "My File|*.grp"
dlgSave.DefaultExt = ".gpr"
If dlgSave.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim formatter As New BinaryFormatter
Using stream As New MemoryStream
formatter.Serialize(stream, GRAPH_1)
Using sw As New FileStream(dlgSave.FileName, FileMode.Create)
Dim data() As Byte = stream.ToArray()
sw.Write(data, 0, data.Length)
End Using
End Using
End If
End Sub
''To Load fie
Public Function LoadFile() As myGraph
Dim GRAPH_2 As myGraph = Nothing
Dim dlgOpen As New OpenFileDialog
dlgOpen.Filter = "My File|*.grp"
If dlgOpen.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim formatter As New BinaryFormatter
Using stream As New FileStream(dlgOpen.FileName, FileMode.Open)
GRAPH_2 = TryCast(formatter.Deserialize(stream), myGraph)
End Using
End If
Return GRAPH_2
End Function
如何使用文件
''To Save File
Dim GRAPH_1 As New myGraph
With GRAPH_1
.Value1 = "ABC"
.Value2 = "XYZ"
.Value3 = "PQR"
End With
SaveFile(GRAPH_1)
''ToLoad File
Dim GRAPH_2 As myGraph = LoadFile()
If GRAPH_2 IsNot Nothing Then
''Place your code here
''And assign values to your graph from that (myGraph)class.
End If