2015-11-03 94 views
0

我想使它在MenuStrip上按鈕的一些對齊到MenuStrip的右側。例如,關注並重點關上的菜單條的右側:如何將MenuStrip的LayoutStyle設置爲Flow,將某些MenuItem對齊到右側?

MenuStrip

我能得到這個,如果我設置的MenuStrip的的LayoutStyle到StackWithOverFlow打工,但那時如果窗口尺寸減小時,菜單項目將隨之而來:

MenuStrip with LayoutStyle set to StackWithOverFlow

我怎樣才能讓這個我可以對齊菜單項與MenuStrip中的LayoutStyle正確設置流量?這樣,當表單尺寸減小時,菜單項會轉到下一行?

另外,當MenuStrip爲更多菜單項創建新行時,如何才能使其他控件被按下一點點?

回答

2

爲了右對齊一些菜單項,你需要將項目的對齊值設置爲。但是,右對齊僅適用於StackWithOverflow佈局樣式。如果您使用流程對齊樣式,則項目將始終從左到右流動。

此外,當您在StackWithOverflow佈局樣式右對齊項目,來自外部的項目流程,因此,如果您的原始佈局是1 2 3 4 5,你的右對齊項目將1 2 3 <gap> 5 4

你的問題的解決方案由兩個部分組成:

  1. 軌道的SizeChanged將事件,以確定是否需要流量StackWithOverflow基於所有菜單項的寬度和可用窗戶的寬度。

  2. 如果您必須更改佈局樣式,請交換右對齊的項目,以使它們以任一佈局樣式以正確的順序出現。

    private void Form1_SizeChanged(object sender, EventArgs e) 
    { 
        int width = 0; 
    
        // add up the width of all items in the menu strip 
        foreach (ToolStripItem item in menuStrip1.Items) 
         width += item.Width; 
    
        // get the current layout style 
        ToolStripLayoutStyle oldStyle = menuStrip1.LayoutStyle; 
    
        // determine the new layout style 
        ToolStripLayoutStyle newStyle = (width < this.ClientSize.Width) 
         ? menuStrip1.LayoutStyle = ToolStripLayoutStyle.StackWithOverflow 
         : menuStrip1.LayoutStyle = ToolStripLayoutStyle.Flow; 
    
        // do we need to change layout styles? 
        if (oldStyle != newStyle) 
        { 
         // update the layout style 
         menuStrip1.LayoutStyle = newStyle; 
    
         // swap the last item with the second-to-last item 
         int last = menuStrip1.Items.Count - 1; 
         ToolStripItem item = menuStrip1.Items[last]; 
         menuStrip1.Items.RemoveAt(last); 
         menuStrip1.Items.Insert(last - 1, item); 
        } 
    } 
    

交換右對齊項目的進程將不得不更仔細地適應,如果你有兩個以上的項目。上面的代碼只需交換它們,但如果您有三個或更多項目,則需要完全顛倒它們的順序。

+0

哇,謝謝!這工作完美。我非常感謝幫助! – Caleb

相關問題