问题描述
我需要为实习项目提供帮助.
I need some help for the project I am working on for my internship.
在VB项目中,我有一个Windows窗体(Form1.vb),其中包含一个名为WaveForm1(其作用类似于示波器图)的自定义用户控件.我可以运行VB程序并为通道分配值,以在项目运行时在WaveForm1用户控件中获取波形.
In the VB project, I have a windows form (Form1.vb) with a custom user control called WaveForm1 (which acts like the graph of an oscilloscope). I can run the VB program and assign values to the channels to get the wave forms in the WaveForm1 user control when the project is running.
然后,我需要使用SaveFileDialog将WaveForm1和在图形中绘制的通道一起保存在自定义文件扩展名(.gph,.wfm,..)下.
Then, I need to save the WaveForm1 together with the Channels plotted in the graph under a custom file extension (.gph, .wfm ,..) using a SaveFileDialog.
使用OpenFileDialog通过按钮(btnOpen)打开保存的文件时,应该能够在vb项目中打开该文件.
The saved file should be able to open in the vb project when it is opened with a button (btnOpen) by using a OpenFileDialog.
只要可以查看已保存的图形,文件在项目中的打开方式就无关紧要. (例如,可以在Form1.vb中的另一个WaveForm2控件中查看保存的文件,或者可以在单独的窗口窗体中查看保存的文件.)如果可以保存整个窗体,然后使用btnOpen从项目中再次打开它,也可以.按钮控制.
How the file is opened in the project doesn't matter as long as the saved graph can be viewed. ( Example, the saved file can be viewed in another WaveForm2 control in Form1.vb or it can be viewed in a separate window form.) It would also be fine if the whole formed can be saved and opened again from the project with the btnOpen button control.
我搜索了有关自定义文件的创建内容&保存文件,我所能找到的就是如何使用StreamWriter/Reader,binaryReader/Writer等保存文本文件,excel文件或图像.
I have searched about custom file creations & saving files and all I could find were how to save text files, excel files or images using a StreamWriter/Reader, binaryReader/Writer, and so on.
对于保存文本文件或图形以外的任何内容,我将深表感谢.
I would really appreciate any help regarding saving anything other than text files or drawings.
如果您不清楚我的问题,请随时与我确认.
Please feel free to confirm with me if you are not clear about my questions.
推荐答案
您应尝试创建具有某些属性的类,然后可以使用BinarryFormatter
保存该类.您可以将自定义扩展名提供给该类,并通过SaveFileDialog
保存它,然后通过OpenFileDialog
打开它.
You should try to create a class with some properties then you can save that class using BinarryFormatter
. You can give your custom extension to that class and save it through SaveFileDialog
and open it by 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
这篇关于如何保存具有自定义扩展名的表单的内容(非文本或图像)并打开保存的自定义文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!