0

i am working with a winform application , and in the richbox_textchange i would like to detect whether the entered text is English or not because if it is english i`ll perform LeftToRight typing else RightToLeft typing .

I used that code :

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
        if (CultureInfo.CurrentCulture.TextInfo.IsRightToLeft)
        {
            label1.Text = "RTL";
        }
        else
        {
            label1.Text = "LTR";
        }
    }

but i always get : LTR only , label1 never change text to RTL even if i typed arabic !!!

EDIT : ANSWERED !!

Firstly Thanks to everybody for helping me here and especially Oded , here is the solution i could figure out

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
        if (InputLanguage.CurrentInputLanguage.Culture.TextInfo.IsRightToLeft)
        {
            label1.Text = "RTL";
        }
        else
        {
            label1.Text = "LTR";
        }
    }
j0k
  • 22,600
  • 28
  • 79
  • 90
R.Vector
  • 1,669
  • 9
  • 33
  • 41

2 Answers2

2

You need to add the correct namespace to the top of your class:

using System.Globalization;

At this point the CultureInfo and TextInfo classes will be available directly.


Update:

It appears that you are trying to find out the current input language. Take a look at the InputLanguage class and its methods. It is in the System.Windows.Forms namespace.

InputLanguage.CurrentInputLanguage.Culture.TextInfo.IsRightToLeft
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Actually it didn`t work , okay i found both Cultureinfo and TextInfo but i can`t use CultureInfo.TextInfo.IsRightToLeft , they wont fit in the same line – R.Vector Jan 31 '12 at 20:09
  • @R.Vector - I have no idea what that means. – Oded Jan 31 '12 at 20:09
  • Okay it was : CultureInfo.CurrentCulture.TextInfo.IsRightToLeft – R.Vector Jan 31 '12 at 20:10
  • @R.Vector - And what exactly is the problem with this? – Oded Jan 31 '12 at 20:11
  • If the correct namespace were not imported, then the code would not have compiled in the first place, correct? – Cody Gray - on strike Jan 31 '12 at 20:15
  • no the problem was just when i typed the code : CultureInfo.TextInfo.IsRightToLeft , it gave me syntax error but i changed it to CultureInfo.CurrentCulture.TextInfo.IsRightToLeft and it became correct , but now i have problem , please check my question as i added code to it and thank you very much in advance . – R.Vector Jan 31 '12 at 20:16
1

The problem is that CultureInfo.CurrentCulture.TextInfo.IsRightToLeft is returning information about the current system setting, not the specific text that was typed into the textbox.

It has no idea if you've typed English, or Arabic, or Cyrillic into the textbox, and it doesn't care. All it cares about is what your computer is configured to display, that's why it never changes.

Unfortunately, I don't believe it's possible to obtain the language of a particular string of text. You might have some luck with the Text.EncodingInfo.CodePage property, but it's unlikely that anything will tell you the language of text with absolute certainty. Another possible approach is to iterate through the characters in the string, checking them for information. Something like that is described here.

All things considered, it's probably better to just ask the user. What do other applications do that support multiple input languages?

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Yes the best solution right NOW is to ask him , but i just wanted this to be dynamic – R.Vector Jan 31 '12 at 20:24
  • 1
    I discovered the [`InputLanguge`](http://msdn.microsoft.com/en-us/library/system.windows.forms.inputlanguage.aspx) class. – Oded Jan 31 '12 at 20:25