hi there,

i'm designing a form in Right-to-Left mode. there are some autosize labels getting their text from the string table of the project's resources. however, when the text is inserted into the label at runtime, it enlarges to the left and goes out of the form. logically, the label should be extended to the right, since the Right-to-Left property is set to true.

i tried to change the Anchor as well as TextAlign to TopRight instead of the default value, TopLeft. but it didn't help either.

any workaround?!

regards,
Sean

So the *left* coordinate of the label should be fixed and it should *always* grow right? Or only grow right when it can't grow left anymore?

in other words, the right coordinate of the label should be fixed and it grows up to the left. (by default it grows up to right)

I have a form set up as Right-To-Left and the label grows to the left -- not the right.
>>"when the text is inserted into the label at runtime, it enlarges to the left and goes out of the form"

So you want the Left coordinate fixed just like if it was a regular form, right?

I have a form set up as Right-To-Left and the label grows to the left -- not the right.

weird! i've started a new solution, set the RightToLeft and RightToLeftLayout to yes/true and the label is still growing up to the right!
did you set anything else in order to get that behavior?!

I don't think so. Here is my 2 files, try it:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmRightToLeft : Form
  {
    public frmRightToLeft()
    {
      InitializeComponent();
    }
  }
}
namespace daniweb
{
  partial class frmRightToLeft
  {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
      if (disposing && (components != null))
      {
        components.Dispose();
      }
      base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.panel1 = new System.Windows.Forms.Panel();
      this.label1 = new System.Windows.Forms.Label();
      this.button1 = new System.Windows.Forms.Button();
      this.button2 = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // panel1
      // 
      this.panel1.Location = new System.Drawing.Point(125, 88);
      this.panel1.Name = "panel1";
      this.panel1.Size = new System.Drawing.Size(200, 100);
      this.panel1.TabIndex = 0;
      // 
      // label1
      // 
      this.label1.AutoSize = true;
      this.label1.Location = new System.Drawing.Point(229, 493);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(246, 13);
      this.label1.TabIndex = 1;
      this.label1.Text = "label1asda2sss34234234234sd123aaaaaaa22222";
      this.label1.TextAlign = System.Drawing.ContentAlignment.TopRight;
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(344, 467);
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size(171, 23);
      this.button1.TabIndex = 2;
      this.button1.Text = "button1";
      this.button1.UseVisualStyleBackColor = true;
      // 
      // button2
      // 
      this.button2.Location = new System.Drawing.Point(267, 467);
      this.button2.Name = "button2";
      this.button2.Size = new System.Drawing.Size(75, 23);
      this.button2.TabIndex = 3;
      this.button2.Text = "button2";
      this.button2.UseVisualStyleBackColor = true;
      // 
      // frmRightToLeft
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(518, 515);
      this.Controls.Add(this.button2);
      this.Controls.Add(this.button1);
      this.Controls.Add(this.label1);
      this.Controls.Add(this.panel1);
      this.Name = "frmRightToLeft";
      this.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
      this.RightToLeftLayout = true;
      this.Text = "frmRightToLeft";
      this.ResumeLayout(false);
      this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
  }
}

Oh, the RightToLeft layout property I bet is the trick :)

this.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.RightToLeftLayout = true;

i'm really confused. you just set the RightToLeft and RightToLeftLayout settings of the form to yes/true the same way i do but my label doesn't grow up to the left!

i presume that the label handle in the form designer (the small square to the top-left of controls) should move to the top-right position, so it enlarges to the left. but mine is still at the top-left of the label.

anyhow, can someone elaborate why the label grows up to the right no matter how RightToLeft and RightToLeftLayout of the form is set?

TIA,
Sean

i found the problem... when the label is inside a panel or groupbox, it grows up to the right, like when the RightToLeft settings are not activated!!!

any workaround?

I would just write a method for SetLabelText(label1, "abc"); and inside of the method note the location, change the text, and then move it. You could have the method check label1.Parent and if its a form then don't move it since it works properly

does it mean that VS2008 can't handle right-to-left forms properly?!

would you please tell me how you managed to make the label behave correctly when it's placed inside a panel/groupbox?

Correct, it doesn't seem to handle them properly.

I made a little label fixer class. It seems to work just fine:

internal class LabelFixer : IDisposable
  {
    private Form parent;
    private Dictionary<Label, int> labelPosition;
    private LabelFixer()
    {
      labelPosition = new Dictionary<Label, int>();
    }
    public LabelFixer(Form f)
      : this()
    {
      if (f == null)
        throw new ArgumentNullException("f");
      this.parent = f;
      LogLabelPosition();
    }
    private void LogLabelPosition()
    {
      for (int i1 = 0; i1 < parent.Controls.Count; i1++)
      {
        FindLabels(parent.Controls[i1]);
      }
    }
    private void FindLabels(Control c)
    {
      Label lbl = (c as Label);
      if ((lbl != null) && !(lbl.Parent is Form))
      {
        lbl.Resize += new EventHandler(lbl_Resize);
        labelPosition.Add(lbl, lbl.Right);
      }
      for (int i1 = 0; i1 < c.Controls.Count; i1++)
        FindLabels(c.Controls[i1]);
    }

    void lbl_Resize(object sender, EventArgs e)
    {
      Label lbl = (sender as Label);
      int correctRight;
      if (labelPosition.TryGetValue(lbl, out correctRight))
      {
        lbl.Left -= (lbl.Right - correctRight);
      }
      else
      {
        throw new Exception("Something went wrong...");
      }
    }
    #region IDisposable Members
    public void Dispose()
    {
      foreach (Label lbl in labelPosition.Keys)
      {
        lbl.Resize -= new EventHandler(lbl_Resize);
      }
    }
    #endregion
  }

Calling it:

public partial class frmRightToLeft : Form
  {
    private LabelFixer fixer;

    public frmRightToLeft()
    {
      InitializeComponent();
    }
    private void frmRightToLeft_Load(object sender, EventArgs e)
    {
      fixer = new LabelFixer(this);
    }

    private void button1_Click(object sender, EventArgs e)
    {
      label1.Text += "!"; //on the form
      label2.Text += "!"; //in a panel
    }

    private void frmRightToLeft_FormClosing(object sender, FormClosingEventArgs e)
    {
      fixer.Dispose();
    }

    private void button2_Click(object sender, EventArgs e)
    {
      label1.Text = label1.Text.Substring(0, label1.Text.Length - 1);
      label2.Text = label2.Text.Substring(0, label2.Text.Length - 1);
    }
  }
This article has been dead for over six months. Start a new discussion instead.