Search code examples
c#winformstextbox

Making a textbox accept a language


I’m working on a data entry project(visual studio windowsform) and there are two main languages that the datas have to entered in, english and arabic, i want some fields to show errorprovider if the user enters in English language in an arabic field and vice versa, is that possible? Thanks.


Solution

  • Just check if all letters in the entered text are part of the english alphabet.

      string text = "abc";
    
      char[] englishAlphabet = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
      bool english = true;
      foreach (char c in text.ToLower())
        if (!englishAlphabet.Contains(c))
        {
          english = false;
          break;
        }
    
      if (english)
        // Do some stuff
      else
        // Show error
    

    Same for the arabic alphabet.