Show message from string table in C#

1. Run Microsoft Visual Studio 2010 and create a new windows application(File->New->Project)

2. Select Windows FormsApplication and enter application Name into Name field “TestMessage”

3.  Add a button from Toolbox, enter ‘Show Message’ into Text property, ‘btnShowMessage’ into Name property

4. Double click on the button to go to the class file

5. Add tow reference:

using  System.Resources;

using System.Reflection;

6. Go to the design view and right click on the project solution name in solution view, select Add->New Item, select Resource File and click on ‘Add’ button. A file named ‘Resource1.resx’ will add to your project

7. Double click on the resource file and enter “This is a test” into value field. default name field is String1. save the file

8. Add following code into Form1 class scope:

public partial class Form1 : Form
    {

        private static ResourceManager RM;
       
        public static void initialise()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            RM = new ResourceManager(“TestMessage.Resource1”, assembly);
        }
        public static string Getstring(string key)
        {
            return RM.GetString(key);
        }

}

9. To initialize the file add following code into default constractor of the class:

public Form1()
        {
            InitializeComponent();
            initialise();

        }

10. Double click on the button and add the code:

private void btnShow_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Getstring(“String1”));
        }

11. Compile and run the project, click on the button.It will display message from string table

Advertisements