In principle, My.Resources is completely different from my.computer or my.application in the previous introduction, and he brings another convenience. My.Resource is not a class library, but the only child namespace in my namespace. What is his function? Let us first recall the situation of using resources in the .NET Framework1.1 era. First we have to add resources such as pictures, text or sounds to the resource list, compile into a resource file, and then embedded in our program. When we want to use resources, you must extract resources from the program to the program centralized by System.Resources.ResourceManager, and then determine the type of resources, do appropriate transformation and use. For example, remove the ID for Greeting from the resources of Form1, you need to write so many code:
Dim Manager as new system.resources.resourceManage (_ "myapp.form1", me.gettype (). Assmbly) DIM S as string = manager.getstring ("Greeting")
And the interface of the editor of RESX is not intuitive. It is not convenient to add a string resource. If you want to add files such as pictures, music to the resource file and take it in the program. To now, many Visual Basic .NET programmers also always ask how to embed pictorial music into the exe file. .NET Framework 2.0 introduces a new feature to solve this problem - strong type resource. First, ResourceManager adds a getStream () method to facilitate access to the image, the resource of the sound, followed by generating a packaging class according to the content of the resource, by which it can be directly intended to read some of the resources within the program. Visual Basic 2005 combines strong type resources with the characteristics of VB's IDE, which has become a convenient MY.Resources.
Before actually using My.Resources, let's take a look at the new resource editor of the Visual Basic 2005, which has now inherited into the project properties. Open the project properties and switch to the Resources tab, we can see the following resource editor.
From the top drop-down list box, you can select the type of resource - sound, image, text, files, icons, or other custom content. Select the corresponding category, the following editor changes to accommodate the current type of resources. Each category can add any number of resources. For example, we add a string name Greeting and let it be equal to "hello", then switch to the code window, enter my.resource. What is it? Greeting bombed it. Now we need a line of code with this resource. Is it convenient to compare the old way to just mentioned?
DIM S as string = My.Resources.greeting
If we add a picture resource, you can also use My.Resources to access directly, and it is the bitmap type. How do you want to use it, such as:
BackgroundImage = my.resources.mypic
One line of code can easily set the window background as a picture in the resource. If the sound resource is stored, then combined with my.computer.audio's function, the sound in the playback resources is so simple:
My.computer.Audio.PlaySound (My.Resources.Mysound)
Imagine how much code you want to complete this task! With my.resource, the use of resources is very simple, you will change the views of the use of resources, and in love with resources in your own procedure. My.use is the smallest member in my namespace, but don't look at him, the function is not simple for .NET novices. If you first contact .NET development, what should I get to get the user name and user group of the current login user? Who thinks it is actually related to the Thread.CurrentPrincipal property? My.user provides you with the username and role information to you. If you want to get the user name of the current login, you only need to enter my.user.Identity.