Search code examples
c#winformswindows-forms-designerresx

How to compare original this.text from resource file to an overwritten text


I have a windows form. Where the title will be overwritten every time a piece of code is hit. The title will be by default in resx file of the form until it is overwritten.

Form1.resX

 <data name="$this.Text" xml:space="preserve">
    <value>Report</value>
  </data>

Form2.cs

public void Report2()
{
Form1 frm = new Form1();
frm.Text="Report2"
//Some code
}

public void Report3()
{
Form1 frm = new Form1();
frm.Text="Report3"
//Some code
}

Here the this.Text gets overwritten when Report2() and Report3() executes

In Form1.cs

private void Report_ColumnClick(Object eventSender, ColumnClickEventArgs eventArgs)
{
if(this.Text!="Report")
{
//Some code
}
}

So in Form1.cs i am hard coding the resx original value to compare with an overwritten value. Is there anyway i can do it dynamically instead of hard coding.


Solution

  • I'm not sure if this question is the same as yours?

    According to that post it should be as simple as:

    
    ResourceManager rm = new System.Resources.ResourceManager(typeof(Form1));
    string formText = rm.GetString("Form1.Text");
    
    

    OR:

    If that doesn't work you can try reading the information from the manifest embedded in the assembly.

    Get the embedded resource names using reflection typeof(Form1).Assembly.GetManifestResourceNames(); this will give you a list on names if there are any.

    use the correct name to extract the embedded stream.

    var resourceStream = typeof(Form1).Assembly.GetManifestResourceStream("Your_NamesPace_Here.Form1.resources");
    using (System.Resources.ResourceReader resReader = new ResourceReader(resourceStream))
    {
      var dictEnumerator = resReader.GetEnumerator();
      while (dictEnumerator.MoveNext())
      {
        var ent = dictEnumerator.Entry;
        //ent will be a key:value pair, so use it like this                     
        if (ent.Key as string  == "$this.Text")
        {
          string originalFormName = ent.Value as string;
        }
      }
    }