articles

Home / DeveloperSection / Articles / What is the use of Resource File? How to use it on your project?

What is the use of Resource File? How to use it on your project?

What is the use of Resource File? How to use it on your project?

Anonymous User 862 08-Jul-2021

Resource Files are the common files which are used in the application to use common value in the entire project through the use of 'Key-Value' pair.

Let's understand it with the help of an example :

Step 1 : Open Visual Studio 2019 on your machine and select a new Window Form Application and named the project and click on Next Button. This will create a new Project on your machine.

What is the use of Resource File? How to use it on your project?

Step 2 : Now, copy and paste the snippet of code on your Visual Studio.

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApplication2.Properties;
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = Resources.blue;
            textBox1.ForeColor = Color.Blue;
            label1.Text = Resources.mind;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = Resources.pink;
            textBox1.ForeColor = Color.Pink;
            label1.Text = Resources.mind;
        }
    }
}

And let's understand the logic behind it. Here, we are creating a Window Application where the two buttons are there, Blue and Pink. When user click the Blue button then the textbox value will set to 'You select blue color' or when the user select Pink color the textbox value will set to 'You select pink color'. So, now you think what is the role here of resource file. Don't worry you'll get your answer, the selected textbox value is coming through the Resource file. Now let's start to create Resource File.

What is the use of Resource File? How to use it on your project?


Step 3 : Go the Solution Explorer on the right side of the Visual Studio and right click on the Project name('WindowFormApplication2' is in the below image) and click on the Properties on end of the pop up menu.

What is the use of Resource File? How to use it on your project?

Step 4 :  Now select the Resource option located on the left side of Sidebar. And create a new resource by click on the middle screen of the Visual Studio.

What is the use of Resource File? How to use it on your project?

Step 5 : Now, Start adding the Key Value pair value as shown in the image below. And save the File by pressing 'Ctrl+S' shortcut key on your keyboard.

What is the use of Resource File? How to use it on your project?

Step 6 : Now, run the application by using shortcut key F5 on your keyboard or by clicking the 'Start' button at the top.

What is the use of Resource File? How to use it on your project?

Hurrah! You've successfully build your First Application by using Resource File.

Note : While copying the code remind that you must add your 'using project_name.Properties;' at the top of your code otherwise, it'll show errors as shown in the below image. So must import the Resource File at the top of your project.

What is the use of Resource File? How to use it on your project?


Hope you have enjoyed and if you face any query while creating this file. So, be sure to leave a comment with your problem. Thank you!


Updated 08-Jul-2021
I am a content writter !

Leave Comment

Comments

Liked By