CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
+ Reply to Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2012
    Posts
    6

    [RESOLVED] Help with saving a vb.net form

    I developed a vb.net standalone application and everything is working fine. Now i want to save the data on the form
    as a file ie. like a word document with a custom extension. But when i try to open the file it should open as vb.net
    form ie. as a runtime executable form. So when i run the application and click save button, each time a new file has to be created and the data should save in that file. When i open the file, all the saved data should appear in the
    textboxes and it should be as a vb.net form.
    I am unable to do this. Any help is greatly appreciated.



    Thanks

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,474

    Re: Help with saving a vb.net form

    Well what you have to do if I am understanding you correctly is register the file extension to your exe. There are registery entries for each registered file type that tell windows what program should be used to open this type of file. Your program would have to be there under the extension you wish to use. You can do this through windows explorer tools menu, or the open with options or the registery.

    Of course you have to write the code to write to the file, read from the file and populate your form as well as code to accept incoming parameters.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    10,792

    Re: Help with saving a vb.net form

    The proper way to do it is to make use of an installer. This can set all the File Associations for you once the program is installed. It is a bit more professional at the end of the day as well. It does everything the Great DataMiser mentioned

    You'd just need to add a Setup project to your existing project, then follow these steps :
    • Open up your solution in Visual studio.
    • Add a Setup Project to your solution by clicking on File -> Add Project ->New project -> Setup & Deployment Projects ->Setup project
    • Right-click on your Setup Project in Solution Explorer and Select view, then select file types.
    • Right-click on "File types on target machine". Click Add "File Type"
    • You will notice that "New document Type#1" was added, and "&open" item underneath it.
    • The "new document type#1" can be anything you want - change it to something descriptive. Be careful not to overwrite existing file types!
    • In the "properties" window for new document type#1, you will need to change a few properties.:
    • Command:Change to the application that you want to run. ADD YOUR OUTPUT FILE
    • Description: This is the description of the file type
    • Extensions:This your list of extensions for you chosen Program. For example htg Separate each one with a ","
    • Icon:This will associate the icon with your file type,This shows up in the window explorer.
    • Click on &open. This is an action that is available if your right-click on the file.The default action("&Open" is currently set as the default) is what happens when you double click on the file. Right click on your "New document type#1" to add actions,but for the moment,lets define our "&open" action
    • Click on "&Open".You will see in the properties window "Name","Arguments","Verbs". Verb is hidden from the user,but is the key that is stored in the registry.Leave it same as the name,But without the "&".

    This is the Setup project part. Now, obviously, you'd need to add the Open parameter to your project. The best way is through a Sub Main procedure :

    It will look something like :

    Code:
    Imports System.IO
    
    Module Module1
        Sub Main(ByVal args() As String)
    
           If args.Length > 0 Then
                Application.Run(New frmHTG_Associate(args(0)))
            Else
                Application.Run(New frmHTG_Associate())
            End If
        End Sub
    End Module
    Now, when you navigate to a htg file, you will see the appropriate icon, and when you double click on it, it should open that file as well.

    I am attaching these samples that can help you get started. In my main project, I do have a Save and Open menu :

    Code:
    Imports System.IO
    
    Public Class frmHTG_Associate
    
        Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
            'Write data to  file
            Dim myWriter As StreamWriter
            Dim myStream As FileStream
    
            myStream = New FileStream(("c:\File1.htg"), FileMode.Create)
            myWriter = New StreamWriter(myStream)
    
            Dim myItem As Object
            For Each myItem In rtbHTG_Associate.Text
                myWriter.Write(myItem.ToString & Environment.NewLine)
            Next
            myWriter.Close()
            myStream.Close()
        End Sub
    
        Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
            'rtbHTG_Associate.LoadFile("c:\File1.htg", RichTextBoxStreamType.PlainText)
    
            Dim textline As String
            FileOpen(1, "c:\File1.htg", OpenMode.Input)
            While Not EOF(1)
                textline = LineInput(1)
                rtbHTG_Associate.Text = rtbHTG_Associate.Text + textline + Chr(13) + Chr(10)
            End While
        End Sub
    End Class
    You just need to Build the projects.

    I hope this helps
    Attached Files

  4. #4
    Join Date
    Jul 2012
    Posts
    6

    Re: Help with saving a vb.net form

    Hannes...thanks for explaining step by step very clearly and it works great. My problem is resolved half way. Saving the file with our
    custom extension is perfect. But while opening, i need to go to the location of the file where it is saved and double click it and it should
    open automatically as a vb.net form with the data in it.

    Appreciate for your help!!

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    10,792

    Re: Help with saving a vb.net form

    Hmm, OK, I'll need to have a look why it doesn't happen for you. What OS are you using?

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    10,792

    Cool Re: Help with saving a vb.net form

    rockers, there is something seriously wrong with me these days....
    This is the second thread on this forum, in the past week, where I forget everything I want to write in a reply...

    Anyways. With a shock I realised that my program is incomplete - I am so sorry about that Blame it on age or on a burn out.

    Everything I mentioned was correct, we just need to tell our program to load the file that was double clicked. The obvious place to include that is Form_Load. I added these now :

    Code:
        'This is a function to to get the filename used
        Private Function GetDoubleClickedFileName(ByVal StrFullPath As String) As String
            Dim intPos As Integer
    
            intPos = InStrRev(StrFullPath, "\") 'Get last Index of "\"
            GetDoubleClickedFileName = Mid$(StrFullPath, intPos + 1) 'Get the full name
        End Function
    
        Private Sub OpenFilesByDoubleClicking()
            Dim strFileExt As String 'Get File Extension
    
            Dim strFileContents As String 'Get Contents of File
    
            Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs 'Get Command Line Arguments ( &open )
    
            For i As Integer = 0 To CommandLineArgs.Count - 1 'Loop through all arguments
    
                strFileExt = System.IO.Path.GetExtension(CommandLineArgs(i)) 'Get the filename extension
    
                Select Case strFileExt
    
    
                    Case ".htg" 'This is to open a file with associated extension *.htg
    
                        strFileContents = My.Computer.FileSystem.ReadAllText(CommandLineArgs(i)) 'Read content
    
                        rtbHTG_Associate.Text = strFileContents 'Load content into RTB
    
                        Me.Text = GetDoubleClickedFileName(CommandLineArgs(i)) & " - HTG_AssociateFiles" 'Name of program + file opened
    
                    Case Else 'This is to open any other file extensions that its extension not .htg
                        'Just including it here, in case you have more than one variant of the extension you want
    
                        strFileContents = My.Computer.FileSystem.ReadAllText(CommandLineArgs(i))
                        rtbHTG_Associate.Text = strFileContents
    
                        Me.Text = GetDoubleClickedFileName(CommandLineArgs(i)) & " - HTG_AssociateFiles" 'Name of program + file opened
    
                End Select
            Next
        End Sub
    
        Private Sub frmHTG_Associate_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            OpenFilesByDoubleClicking() 'Open from command line ( Double clicking )
    
        End Sub
    Uninstall the previous version of the program.

    Build the program and setup that I am giving you now, and double click a saved file from My Computer. Voila! It works like a charm!!

    Please let me know if you come right or if you do not understand any of my logic

    Hannes
    Attached Files

  7. #7
    Join Date
    Jul 2012
    Posts
    6

    Re: Help with saving a vb.net form

    Hannes, your code works great this is what i was looking for...it solved my problem.. appreciate for all your help..

    i had to make little changes to your code for it to work in my application. will post you again if i find any difficulty implementing in my
    project..

    Thanks again

  8. #8
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    10,792

    Re: Help with saving a vb.net form

    You're welcome!

    Please mark your thread Resolved if you feel your problem has been solved.

    Have a good day

    Hannes

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts



HTML5 Development Center

Click Here to Expand Forum to Full Width