-2

Is there a way to left align the toolbars (without leaving the "spaces") by code? enter image description here

(say, when clicking to the button2) enter image description here

PS
Also this situation

enter image description here
To "left align" means :
enter image description here

serhio
  • 28,010
  • 62
  • 221
  • 374
  • Left Align Toolbars means You need to change the postion of Menustrip something like that.? – Akshay Joy May 14 '13 at 10:22
  • I need that the toolbars from the first image be aligned like the toolbars in the second image. – serhio May 14 '13 at 10:23
  • Just so I can make sure I've understood the issue, is it that when you dock (DockStyle) you can't then apply a width? And as such, the width is much wider than you'd like. – Dave May 14 '13 at 10:52
  • @serhio Your question keeps changing! Is it possible the strips will overlap by "row". You've pictured it where everything lines up neatly on the X axis. – Dave May 14 '13 at 12:19
  • Dave, the question does not change. I need Align the toolbars to left, that means, as much to left possible, no "empty spaces" in a row. – serhio May 14 '13 at 13:06

2 Answers2

0

Update

Based upon your criteria changing quite dramatically, here is some code which will get you going. It is not perfect nor even close to being perfect!

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        _otherStrips = new List<OtherStrips>();
    }

    private int _currentHeight = 0;
    private List<OtherStrips> _otherStrips;

    private void button1_Click(object sender, EventArgs e)
    {
        foreach (var c in panel1.Controls)
        {
            if (c is ToolStrip)
            {
                ToolStrip ts = (ToolStrip)c;
                _otherStrips.Add(new OtherStrips() { Top = ts.Top, Width = ts.Width, Name = ts.Name });
                MoveToPosition(ts);
            }
        }
    }

    private void MoveToPosition(ToolStrip toolStrip)
    {
        bool isInline;
        toolStrip.Left = GetWidth(toolStrip, out isInline); 

        if (isInline)
            toolStrip.Top = GetTop(toolStrip);
    }

    private int GetWidth(ToolStrip ts, out bool isInline)
    {
        int result = 0;
        isInline = false;
        foreach (var item in _otherStrips)
        {
            if (item.Name == ts.Name)
                continue;

            if (item.Top == ts.Top)
            {
                isInline = true;
                break;
            }
        }

        if (!isInline)
            return result;

        foreach (var item in _otherStrips)
        {
            if (item.Width == ts.Width)
                result += item.Width;
        }

        return result + 22;//hack since the width is out by about 22pixels. Not going to spend any time fixing this
    }

    private int GetTop(ToolStrip ts)
    {
        foreach (var item in _otherStrips)
        {
            if (item.Name == ts.Name)
                continue;

            if (item.Top == ts.Top)
                return item.Top;
        }

        _currentHeight += ts.Height;
        return _currentHeight;
    }
}

struct OtherStrips
{
    public int Top { get; set; }
    public int Width { get; set; }
    public string Name { get; set; }
}
Dave
  • 8,163
  • 11
  • 67
  • 103
0

You can use this to left align your ToolStrip

toolStrip1.Location = new Point(0, toolStrip1.Location.Y);
Jexfer
  • 21
  • 3
  • use the same code for toolStrip2 and toolStrip3 by just replacing the name.. i have tested, its working :) – Jexfer May 14 '13 at 11:15