3

Although this question is general enough to apply to the web, I'm interested in WinForms in particular.

The application UI switches between LTR and RTL languages without incident. The only obstacle is placement of labels that are associated with input controls such as text boxes.

Left to Right:
Left to Right

Right to Left:
Right to Left

The label placement on the RTL image should also change accordingly.

Is there a generalized, programmatic way to achieve this?

Laurel
  • 5,965
  • 14
  • 31
  • 57
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168

1 Answers1

4

Option 1 - Mirror Form (mirrors titlebar too)

If both the RightToLeftLayout and RightToLeft properties are true, mirroring will be turned on for the form, and control placement and text flow will be right-to-left. So set RightToLeftLayout to true and set RightToLeft to yes to have a complete right to left layout.

This way also the form title bar will be mirrored and control box will be shown at left.

Option 2 - Mirror Panel (doesn't mirror title-bar)

If you don't like to have right to left title bar and the control box at left, you should create your right to left container yourself and put controls in it and then set RightToLeftLayout of the container to true and set RightToLeft of the container to yes to have a complete right to left layout in the container without changing the layout of title bar and control box:

using System;
using System.ComponentModel;
using System.Windows.Forms;
public class ExPanel : Panel
{
    const int WS_EX_LAYOUTRTL = 0x400000;
    const int WS_EX_NOINHERITLAYOUT = 0x100000;
    private bool rightToLeftLayout = false;

    [Localizable(true)]
    public bool RightToLeftLayout
    {
        get { return rightToLeftLayout; }
        set
        {
            if (rightToLeftLayout != value)
            {
                rightToLeftLayout = value;
                this.RecreateHandle();
            }
        }
    }
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams CP;
            CP = base.CreateParams;
            if (this.RightToLeftLayout &&
                this.RightToLeft == System.Windows.Forms.RightToLeft.Yes)
                CP.ExStyle = CP.ExStyle | WS_EX_LAYOUTRTL | WS_EX_NOINHERITLAYOUT;
            return CP;
        }
    }
}

Screenshot

Here is a screenshot of Option 1. Look at Close button at left side of title bar:

enter image description here

Here is a screenshot of Option 2. Look at Close button at right side of title bar:

enter image description here

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • If you don't mind me asking, which one would you consider most appropriate for correctly localized software? I'm assuming that, since you are Iranian, you would have some opinion about what is normal and customary. Doesn't the operating system use Option 1 for all of its built-in dialogs? Why would an application ever want to do something different? – Cody Gray - on strike Jun 15 '16 at 11:27
  • 1
    @CodyGray If the user use a right-to-left OS version option 1 is completely preferred. But for people who use left-to-right OS version having close button at left side is really annoying. Currently here in Iran, most users use OS with en-US settings and usually they prefer option 2. – Reza Aghaei Jun 15 '16 at 11:41