0

I have a function in my WinForm application to translate all the Form controls from English to another language.

    public static void SetControls(Control control)
    {
        try
        {
            foreach (Control c in control.Controls)
            {
                var TextProperty = c.GetType().GetProperty("Text");
                if (TextProperty != null)
                {
                    string currentValue = (string)TextProperty.GetValue(c);
                    if (!String.IsNullOrEmpty(currentValue))
                    {
                        if (m_dictionary != null && m_dictionary.ContainsKey(currentValue))
                        {
                            string translatedValue = m_dictionary[currentValue];
                            if (!String.IsNullOrEmpty(translatedValue))
                            {
                                TextProperty.SetValue(c, translatedValue, null);
                            }
                        }
                    }
                }
                SetControls(c);
            }
        }
        catch (Exception ex)
        {
            // error handler
        }
    }

The function loops recursively through all the controls and changes its Text property.

m_dictionary is just a dictionary that contains original and translated text:

private static Dictionary<string, string> m_dictionary;

If I call the function from the form constructor:

    public MainForm()
    {
        InitializeComponent();
        SetControls(this);
    }

that works fine. But if I call the function from a button handler, when the form is up I get crash error:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

If I disable the form while changing that works better but anyway failed in 20% of tries:

this.Enabled = false;
SetControls(this);
this.Enabled = true;

What could it be and how can I solve this strange error?

folibis
  • 12,048
  • 6
  • 54
  • 97
  • You are trying to change control properties via reflection, while they are might be at exactly same time in use while drawing. Bad idea. Do not call your method overs displayed form, rather re-create it again. – Sinatr Sep 04 '17 at 15:26
  • Thanks @Sinatr, it makes sense. Is there some way to disable redrawing temporary? – folibis Sep 04 '17 at 15:28
  • Check this [Stackoverflow page](https://stackoverflow.com/questions/596413/attempted-to-read-or-write-protected-memory), changing the .Net version to something newer or running the command "netsh winsock reset" seems to be the most upvoted answers to all the other similar questions. – Bernard Walters Sep 04 '17 at 15:29
  • Couldn't reproduce the problem using .NET 4.5 or above. It's better to post a sample code that reproduce the problem. Your method works fine for me even with quiet rapid calls repeatedly. – Reza Aghaei Sep 04 '17 at 16:23
  • By the way, to set `Text` property of `Control` you don't need to use reflection. It's a public property of `Control` class. – Reza Aghaei Sep 04 '17 at 16:25
  • Getting an AccessViolationException on code like this is *very* unusual. You must at least show us the stack trace of the exception with unmanaged debugging enabled. – Hans Passant Sep 04 '17 at 17:50

0 Answers0