How to save the contents (Not Texts Nor Images) of a form with a custom extension and open the saved custom file

蹲街弑〆低调 提交于 2019-12-24 00:34:56

问题


I need some help for the project I am working on for my internship.

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.

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.

The saved file should be able to open in the vb project when it is opened with a button (btnOpen) by using a OpenFileDialog.

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.

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.


回答1:


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.

Class

<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

Method to to Save and Load File

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

How to Use File

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


来源:https://stackoverflow.com/questions/24027703/how-to-save-the-contents-not-texts-nor-images-of-a-form-with-a-custom-extensio

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