Jump to content

Using Recources in VB.NET

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
You can store Strings, Images, Icons, Audio, Files etc. as resources in VB.NET, if you can do you can then access them by your code, this is a good way if you want to include images etc. in your program without have to make sure the files exists at the right path on the users computer. To go to the resources window right click on "My Project" in the solution explorer and then click "Open":


[ATTACH]2303[/ATTACH]




Now click on Resources in the list to the left:


[ATTACH]2304[/ATTACH]


If you have made it right you should now see something like this:
[ATTACH]2305[/ATTACH]



Adding Resources

The easiest way to add a resource is to do import it from a file, to do this click at "Add Resource" and then "Add Existing File...". Now select your file(s) and it will be added, observe that the resources will be sorted by type so if you don't find the one you added you have to change the type in the top left corner. After adding some resources it could look like this:


[ATTACH]2306[/ATTACH]






Using Resources


To access the resources you write My.Resources.[the resource's name], like this:

[highlight=VB.NET] My.Resources.test1[/highlight]

So this will give us the Resource called test1. To use it we can do something like this:


[highlight=VB.NET] Me.BackgroundImage = My.Resources.test1[/highlight]

but if we only do this it doesn't seem so useful since we could just have added the image to be the background of the form in the design view, but if we want to have different backgrounds, like this:



[highlight=VB.NET] Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Select Case Now.Minute
Case Is < 15
Me.BackgroundImage = My.Resources.test1
Case Is < 30
Me.BackgroundImage = My.Resources.test2
Case Is < 45
Me.BackgroundImage = My.Resources.test3
Case Is < 60
Me.BackgroundImage = My.Resources.test4
End Select
End Sub[/highlight]

You've probably already got the idea. If you want to get one of the resources you've already added to be able to modify it or something similar you can find it in your project folder in the sub folder called resources.


That was everything about resources in VB.NET, hope you've learned something and understood why it's good to use them. :)

Attached Files



#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Nicely done. +rep

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Nice +rep
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
arkanion

arkanion

    Newbie

  • Members
  • PipPip
  • 10 posts
thanks man

#5
pyaephyokyaw1992

pyaephyokyaw1992

    Newbie

  • Members
  • Pip
  • 7 posts
thanks

#6
HappyTear

HappyTear

    Newbie

  • Members
  • Pip
  • 1 posts
thanks ! but
what if i want to launch/run file from my resurces ?
what should i do ?
:)

#7
Floating

Floating

    Newbie

  • Members
  • Pip
  • 1 posts
Very nice one... I think this is a great idea; placing resources in the app. Thank you. :)