Results 1 to 13 of 13

Thread: Embed a file on to a form for user use

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Embed a file on to a form for user use

    I have an Excel file or it could be any file that I want to display on a for for people to launch. What is the best way to embed that file in the project? I don't want to have it on a disk and link it, I want it embedded on the form some how.

    Thank you to all who reply.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Embed a file on to a form for user use

    You could add some things as a resource, and that might work, but you mention that it could be "any file", and that certainly won't work. An image could be displayed on a form, a document would have to be displayed in a control, a spreadsheet would probably be better off not being displayed in a form at all, and some other types of files have no meaningful way to represent them. So, you probably need to explain the objective a bit more to really get a meaningful answer.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Embed a file on to a form for user use

    I gues I would like it to display an icon for people to click on and open the file or save it to a location only. Hence any file. It is an open or save process only.

  4. #4
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,191

    Re: Embed a file on to a form for user use

    If the file isn't going to change after you compile then adding as a resource seems best. If it can change then I don't see how you could embed a new different file without recompiling. You would need to change the path in Settings, for example, which would be linking to a file on disk that you claim you don't want.

    One option that would be sort of a hybrid but not very robust (and potentially dangerous) is to include whatever the file is in a subfolder under the exe (or network shared folder) and the program attempts to open whatever file is there. So the path is "embedded" but you would need to look for filename, which may not be present or return multiple.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Embed a file on to a form for user use

    The file will not change. It is a template.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Embed a file on to a form for user use

    I don't really want to have the file on disk. I would like it embedded in the program since it will not change unless I need to update it then I would recompile. For users, they will never change it.

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Embed a file on to a form for user use

    Then I would included it as a resource, perhaps changing the name of the file extension to one VB doesn't recognize so that it gets added as a raw array of bytes to the resource. Then you could just use IO.File.WriteAllBytes with the resource to write it to a file when needed.
    For instance, I added renamed .wav files to the resource, naming them .vaw before adding, so that they would be added and compiled into the exe as a byte array resource. Then would write them out, essentially like this:
    Code:
    Dim b() as Byte = {0}  'initialized to avoid uninitialized warning
    
    b = My.Resources.AlienHit
    System.IO.File.WriteAllBytes(filePath, b)
    You wouldn't necessarily have to have a local byte array. I assume you could put My.Resources.AlienHit in place of b in the WriteAllBytes method call, but in my code, there were a number of files handled independently, so there was a case statement that set b to the selected resource, and then did a write after the Select Case block, rather than have a bunch of IO.File.WriteAllBytes, one for each embedded file.
    Last edited by passel; Apr 16th, 2019 at 08:57 AM.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Embed a file on to a form for user use

    OK, Can you guide me thorugh that? I added it to the solution but from there on I am lost. Thanks

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Embed a file on to a form for user use

    I guess I don't usually add them to the solution myself, but they probably get added when I select them as a resource. But, you should be OK doing it from where you have them. I guess you can try adding one without changing the extension first to see if it is stored as a byte array, or something else.

    I usually just right click on the solution in the solution explorer window and select "Properties".
    From the window that appears, I select the Resources tab.
    I think you could drag and drop the file in the window, but I usually select the dropdown on the Add Resource menu item, and select "Add existing file..."

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Embed a file on to a form for user use

    I have it added as an embedded resource but when I try to drag it to the form it doesn't allow.

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Embed a file on to a form for user use

    Resources are not visual objects like controls, they are objects to use in code. Within VB you can use My.Resources to refer to them.

    As for what you put on the form, you can use a button or picturebox etc, and put code behind the relevant event.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Embed a file on to a form for user use

    OK, to start over, I add en existing reference to the file and make it embedded? Then Add a picture box to launch the excel file. Sounds good. How would I do the code to the resource?

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Embed a file on to a form for user use

    Got it Thanks

    Dim filepath As String = My.Computer.FileSystem.SpecialDirectories.Temp + "\Item_Master_Import_Template.xlsx"
    IO.File.WriteAllBytes(filepath, My.Resources.Item_Master_Import_Template)

    Process.Start(filepath)

Posting Permissions

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



Click Here to Expand Forum to Full Width