3 Replies - 721 Views - Last Post: 03 January 2013 - 12:03 PM Rate Topic: -----

#1 charqus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-February 10

Label matrix align

Posted 03 January 2013 - 05:27 AM

Hello guys. I have an 2D array ( n x m ) with int values. Each line I transform it into a string, and I'm trying to align them correctly. Everything it's ok when all elements has same size ( 1, 5, 3; 22, 31, 44; etc. ), but when one element has the lenght not equal with the rest of elements, the labels aren't aligned correctly. Look what I mean:
This is good: http://img839.images...4120/goodzj.png
This is bad: http://img267.images...7/9770/badz.png

And this is my code:
n = GV.a[0].GetUpperBound(0) + 1;
            m = GV.a[0].GetUpperBound(1) + 1;

            label1.Location = new Point(this.Width / 2 - label1.Text.Length * 4, label1.Location.Y);

            int width  = this.Width / 2 + m * 8;
            int height = label1.Location.Y + 20;
            string text, space = "     ";

            for (int i = 0; i < n; ++i)
            {
                text = string.Empty;
                for (int j = 0; j < m; ++j)
                {
                    text += GV.a[type][i, j].ToString() + space;
                    text = text.PadRight(10 - (GV.a[type][i, j].ToString().Length * 2));
                }
                Label val = new Label();
                val.AutoSize = true;
                val.Location = new Point(width / 2, height);
                val.Text = text;
                this.Controls.Add(val);

                height += 20;
            }


Is This A Good Question/Topic? 0
  • +

Replies To: Label matrix align

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Label matrix align

Posted 03 January 2013 - 07:22 AM

You'll need to loop through the array and find the one with the longest string representation then use PadLeft() to align them.
Was This Post Helpful? 0
  • +
  • -

#3 charqus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-February 10

Re: Label matrix align

Posted 03 January 2013 - 07:50 AM

Hey Momerath, I made a simple way, can you help me with this code?
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int[][] array = 
        {
            new int[3] {-1, 2, 3},
            new int[3] {4, 53, 6}, 
            new int[3] {7, 8, -9} 
        };

        public Form1()
        {
            InitializeComponent();
            Load += new EventHandler(Form1_Load);
        }

        void Form1_Load(object sender, EventArgs e)
        {
            string myString;
            int width = 10, height = 10;
            int maxLenght = array[0][0];

            Label[] labels = new Label[3];
            for (int i = 0; i < 3; ++i)
            {
                myString = string.Empty;
                for (int j = 0; j < 3; ++j)
                {
                    myString += array[i][j].ToString() + "  ";
                }
                labels[i] = new Label();
                labels[i].AutoSize = true;
                labels[i].Location = new Point(width, height += 20);
                labels[i].Text = myString + "  ";

                this.Controls.Add(labels[i]);
            }
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3647
  • View blog
  • Posts: 11,416
  • Joined: 05-May 12

Re: Label matrix align

Posted 03 January 2013 - 12:03 PM

Ummm... I know you asked for help with aligning labels, but why not just put the data into a readonly DataGridView? Or if you really want to use labels, put all your Labels into a TableLayoutPanel and you need only twiddle the column widths based on your widest string.

This post has been edited by Skydiver: 03 January 2013 - 12:04 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1