Thursday, May 14, 2020

What Are VB.Net Resources and How Are They Used

After Visual Basic students learn all about loops and conditional statements and subroutines, one of the next things that they often ask is, How do I add a bitmap, a .wav file, a custom cursor, or some other special effect? One answer is resource files. When you add a resource file to your project, its integrated for maximum execution speed and minimum hassle when packaging and deploying your application. Using resource files isnt the only way to include files in a VB project, but it has real advantages. For example, you could include a bitmap in a PictureBox control or use the mciSendString Win32 API.   Microsoft defines a resource as any nonexecutable data that is logically deployed with an application. The easiest way to manage resource files in your project is to select the Resources tab in the project properties. You bring this up by double-clicking My Project in Solution Explorer or in your project properties under the Project menu item. Types of Resource Files StringsImages  IconsAudioFilesOther Resource Files Simplify Globalization Using resource files adds another advantage: better globalization. Resources are normally included in your main assembly, but .NET also lets you package resources into satellite assemblies. This way, you accomplish better globalization because you include only the satellite assemblies that are needed. Microsoft gave each language dialect a code. For example, the American dialect of English is indicated by the string en-US, and the Swiss dialect of French is indicated by fr-CH. These codes identify the satellite assemblies that contain culture-specific resource files. When an application runs, Windows automatically uses the resources contained in the satellite assembly with the culture determined from Windows settings. VB.Net Add Resource Files Because resources are a property of the solution in VB.Net, you access them just like other properties: by name using the My.Resources object. To illustrate, examine this application  designed to display icons for Aristotles four elements: air, earth, fire,  and water. First, you need to add the icons. Select the Resources tab from your Project Properties. Add icons by choosing Add Existing File  from the Add Resources drop-down menu. After a resource is added, the new code looks like this: Private Sub RadioButton1_CheckedChanged( ...Handles MyBase.LoadButton1.Image My.Resources.EARTH.ToBitmapButton1.Text EarthEnd Sub Embedding With Visual Studio If youre using Visual Studio, you can embed resources directly into your project assembly. These steps add an image directly to your project: Right-click the project in the Solution Explorer. Click Add and  then click Add Existing Item.Browse to your image file and click Open.Display the properties for the image that was just added.Set the Build Action property to Embedded Resource. You can then use the bitmap directly in code like this (where the bitmap was the third one, index number 2 in the assembly). Dim res() As String GetType(Form1).Assembly.GetManifestResourceNames()PictureBox1.Image New System.Drawing.Bitmap( _GetType(Form1).Assembly.GetManifestResourceStream(res(2))) Although these resources are embedded as binary data directly in the main assembly or in satellite assembly files, when you build your project in Visual Studio, theyre referenced by an XML-based file format that uses the extension .resx. For example, heres a snippet from the .resx file you just created: assembly aliasSystem.Windows.Forms nameSystem.Windows.Forms,Version2.0.0.0, Cultureneutral, PublicKeyTokenb77a5c561934e089 /data nameAIRtypeSystem.Resources.ResXFileRef,System.Windows.Formsvalue..\Resources\CLOUD.ICO;System.Drawing.Icon,System.Drawing, Version2.0.0.0,Cultureneutral,PublicKeyTokenb03f5f7f11d50a3a/value/data Because they are just text XML files, a .resx file cant be used directly by a .NET framework application. It has to be converted to a binary .resources file, adding it to your application. This job is accomplished by a utility program named Resgen.exe. You might want to do this to create the satellite assemblies for globalization. You have to run resgen.exe from a command prompt. Source Resources Overview. Microsoft, 2015.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.