Use resources to hold images, text, and other data, and load it at run time in C#

Resources are images, strings, text files, and other pieces of data that you can build into an application. Your program can load the resources at run time to display new pictures, text, or whatever.

To add resources to the project, open the Project menu and select the Properties command at the very bottom. On the Properties page, click the Resources tab. Now you can use the Add Resource dropdown menu to add existing or new files to the program's resources.

When you add a resource to the program, C# creates a typed variable representing it named namespace.Properties.Resources.resourcename where namespace is the program's namespace and resourcename is the resource's name. For example, in this example I added a jpg file named Earth.jpg so the program can use the Bitmap variable Properties.Resources.Earth.

The example program displays a series of RadioButtons labeled Mercury, Venus, Earth, and so forth. When you click one, code similar to the following executes to display the corresponding resource.

private void radMercury_CheckedChanged(object sender, EventArgs e)
{
picPlanet.Image = Properties.Resources.Mercury;
}

private void radVenus_CheckedChanged(object sender, EventArgs e)
{
picPlanet.Image = Properties.Resources.Venus;
}

private void radEarth_CheckedChanged(object sender, EventArgs e)
{
picPlanet.Image = Properties.Resources.Earth;
}

// ... Other event handlers omitted ...

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.