views:

163

answers:

4

I wrote a program that accepts and outputs Hebrew (i.e. right-to-left) text.

In lieu of a Hebrew keyboard, the program has 22 buttons that allow the typing of Hebrew letters into one of the text boxes. (This has nothing to do with the problem, I think.)

This program works fine under Windows using Microsoft .NET and Mono.

However, in Mac OS X and Linux all Hebrew text is the wrong way around and typing Hebrew text also adds letters to the right of existing text rather than to the left.

I figure I can catch all "text changed" events of all text fields and reverse those strings before replacing the original textField.text with the new string. But since there are about 100 text fields on the form, this would be tiresome.

Is there a more general event I should use or another solution I overlooked?

(Testing for the underlying OS is easy since Mono returns a PlatformID of PlatformID.Unix on Mac OS X as well as Linux.)

+2  A: 

Are you basically going to do the same thing for each text box? If so, write one method to do it, then hook it up to all the "text change" events by filtering the Controls collection to just TextBoxes, e.g. with Controls.OfType<TextBox>().

Use the "sender" parameter to work out which text box you're interested in. Shouldn't be too hard:

foreach (TextBox tb in Controls.OfType<TextBox>())
{
    tb.TextChanged += ReverseText;
}
...

private static void ReverseText(object sender, EventArgs e)
{
    TextBox tb = (TextBox) sender;
    tb.Text = ReverseText(tb.Text);
}

(You'd then need to write ReverseText, but I assume you already have this. Calling ToCharArray and reversing the contents is a simple way of doing it, but may have issues for you in terms of combining characters etc.)

Jon Skeet
Why reflection? What's wrong with traversing the control tree and filtering using `.OfType<TextBox>`?
Mehrdad Afshari
Mehrdad: Control tree sounds good! Can you phrase it as a response so I can upvote it? Jon: Hooking up one method to all text change events seems weird to me. I mentioned it in the question as something I want to avoid.
Andrew J. Brehm
@Andrew: Why would it be weird? You want them all to respond in the same way don't you?
Jon Skeet
@Mehrdad: Good point. Have edited appropriately.
Jon Skeet
This looks right. I'll try this out and check as accepted answer if I can get it to work. (Reversing the string itself is not a problem.) What does += do in this context? Problem is that updating the text box changes the contents of the text box and calls the reverse method again...
Andrew J. Brehm
This is not good. Updating the text box changes the contents and triggers the reverse method again. Any ideas?
Andrew J. Brehm
A: 

You could loop through all the textboxes adding an event to the text changed events

Stephen lacy
Loop through? Do controls within a window have indices, i.e. can I access all controls of a window as an array of controls?
Andrew J. Brehm
Jon Skeet has the answer!
Stephen lacy
+1  A: 

You could create your own text box inheriting from the WinForms textbox.

Jeff Hornby
A: 

If you are getting PlatformID.Unix on OSX, then you are using an old version of Mono. Try updating first. Then, if the text rendering is still broken, file a bug with Mono. Workarounds will break when this bug gets noticed and fixed.

skolima
My version of Mono is current (2.4.something). PlatformID returns PlatformID.Unix for compatibility reasons. Only Silverlight returns a Mac OS X platform id afaik.
Andrew J. Brehm
http://go-mono.com/forums/#nabble-to21236514|a21284918
Andrew J. Brehm