2013-05-14 4 views
-2

도구 모음을 코드별로 (공백을 두지 않고) 왼쪽으로 정렬 할 수있는 방법이 있습니까? enter image description here도구 모음을 코드로 왼쪽 맞춤

PS 또한
이 상황을합니다 (단추 2를 클릭하면 말) enter image description here

enter image description here
는 "왼쪽 정렬"이란하려면
enter image description here

+0

왼쪽 정렬 도구 모음 당신은 그런 Menustrip 뭔가의 위치보다를 변경해야하는 것을 의미 정렬 왼쪽으로 사용할 수 있습니다.? –

+0

첫 번째 이미지의 도구 모음을 두 번째 이미지의 도구 모음처럼 정렬해야합니다. – serhio

+0

그냥 문제를 이해했는지 확인할 수 있습니다. 도킹 (DockStyle) 할 때 너비를 적용 할 수 없습니까? 그리고 너비가 너보다 훨씬 넓어. – Dave

답변

0

업데이트

기준에 따라 크게 달라 지므로 여기에 몇 가지 코드가 있습니다. 완벽하지도 않고 완벽하지도 않습니다!

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; } 
} 
+0

내 편집 참조 ... 또한 도구 모음의 변수 번호가있을 수 있습니다 ... – serhio

+0

@serhio, 내 대답을 업데이 트했습니다. 이것은 완벽하지는 않지만 좋은 시작 – Dave

+0

은 내가 취한 해결책이 아니지만 대답으로 표시 할 것입니다 ... – serhio

0

당신이 당신의 ToolStrip에

toolStrip1.Location = new Point(0, toolStrip1.Location.Y); 
+0

그냥 이름을 바꾸어서 toolStrip2와 toolStrip3에 동일한 코드를 사용합니다 .. 나는 테스트를 마쳤습니다.) – Jexfer

+0

내 편집을 참조하십시오. 그리고 이것은 다양한 수의 툴바에서 작동하지 않습니다 ... – serhio

관련 문제