2011-06-16 24 views
6

我使用微軟splitcontainer控件在我的WinForms桌面應用程序(.net 4/c#/ vs 2010)。Splitcontainer控件的「按鈕」收起

我希望有一個小按鈕(或任何漂亮的用戶界面元素)之間的控制面板控制器坍塌其中一個面板。例如。包含兩部分的'按鈕',如果我點擊一個部分,則右側面板會摺疊,如果我點擊其他部分,左側面板會摺疊。

我該如何解決這個問題?

Thx提前!

+0

和用戶界面框架是... – Snowbear 2011-06-16 23:40:55

+0

我給你一個線索麪板的大小調整,SplitContainer的不WPF框架存在...它的形式:http://msdn.microsoft.com/en-us/library/system.windows.forms.splitcontainer.aspx ;-) – Smudge202 2011-06-16 23:43:34

+0

只需使用兩個按鈕,左側面板中的一個錨定在右側,另一個在右側面板中。 – 2011-06-17 01:20:15

回答

8

你必須爲此編寫自己的事件。你必須決定設計。希望你需要像下面這樣的東西。

private void radButton1_Click(object sender, EventArgs e) 
{ 
    splitPanel1.Collapsed = !splitPanel1.Collapsed; 
} 

編輯1

有像你想象沒有簡單的方法。看看herehere得到一個想法。

EDIT 2

您可以添加兩個toolStrips到兩個面板上設計有Dock中:頂部和兩個按鈕添加如下面的圖片看起來相當不錯英寸只是一個想法...

enter image description here

EDIT3

Splitter是你另一種選擇。 看一看here

+1

Thx,我的主要問題是關於UI設計的。我希望有一個簡單的解決方案來顯示這種按鈕。我已經在幾個用戶界面上看到了它,但正如我所見,MS splitcontainer沒有將其作爲內置功能。 – Tom 2011-06-17 00:21:36

+1

@Keller Zoltam:不幸的是沒有這麼簡單的方法。請參閱我的編輯瞭解更多詳情。 – CharithJ 2011-06-17 00:36:30

+0

可悲的消息對我來說:(Thx,無論如何! – Tom 2011-06-17 00:40:51

3

我在我的實施中使用了這個解決方案,可能對您來說太遲了,但可能會幫助其他人。

在我的實現中,我也將控件從一個面板移動到另一個面板,這就是爲什麼我只是將面板摺疊狀態更改爲最後一個操作。

由於我不能發佈的任何圖像,只要嘗試根據以下圖弄明白(在[<]和[>]是按鈕):

╔════════════╤═════════════╗ 
║   [<]│[>]   ║ 
║   │    ║ 
║   │    ║ 
║   │    ║ 
║   │    ║ 
║   │    ║ 
╚════════════╧═════════════╝ 

以下是表示用於執行左面板(面板1),右面板也使用了類似的功能。

private void setSplitterLeftPanelCollapsedState(bool collapse) 
    { 
     splitContainer1.SuspendLayout(); 

     // Collapse the left panel 
     if (collapse) 
     { 
      if (!splitContainer1.Panel1Collapsed) 
      { 
       // restoring the panel in the end to apply layout changes 
       buttonOpenPanel1.Text = ">"; 
       splitContainer1.Panel1Collapsed = true; 
      } 
     } 
     // Open the left panel 
     else 
     { 
      if (splitContainer1.Panel1Collapsed) 
      { 
       // collapsing the panel in the end to apply layout changes 
       buttonOpenPanel1.Text = "<"; 
       splitContainer1.Panel1Collapsed = false; 
      } 
     } 

     splitContainer1.ResumeLayout(); 

     comboBoxSearchText.Focus(); 
    } 
2

受Lotus Notes佈局的啓發,我設計了一些我認爲在這種情況下會很有用的東西。它只在面板之間包含一個按鈕,用於切換單個面板的展開/摺疊狀態,但可以輕鬆修改爲使用兩個按鈕來控制右側和左側面板。它使用兩個拆分容器,一個停靠在另一個容器內,以及「中間」面板的mouseMove事件來模擬拖動拆分器(Moving a control by dragging it with the mouse in C#)。 此外,我使用容器的ClientSizedChanged事件來處理切換按鈕圖像的邏輯,而不是摺疊/展開面板的方法(Detect when SplitContainer collapsed changes)。

設計:

splitContainer1 
╔════════════╤═════════════════════════════════╗ 
║   │ splitContainer2 (docked fill) ║ 
║   │ ╔════════════╤════════════════╗ ║ 
║   │ ║   │    ║ ║ 
║   │ ║ Button(s) │    ║ ║ 
║   │ ║ [<>]  │    ║ ║ 
║   │ ║   │    ║ ║ 
║   │ ╚════════════╧════════════════╝ ║ 
╚════════════╧═════════════════════════════════╝ 

splitContainer2.Dock = DockStyle.Fill; 
splitContainer1 = splitContainer2.IsSplitterFixed = true; 
splitContainer2.Panel1.Cursor = Cursors.VSplit; 

錨按鈕向左或向右(或停靠的tableLayout控制內部幾個按鈕)。只要確保仍有部分面板可用於點擊/拖動即可。
將中間面板的最大值設置爲較小的數字。尺寸取決於你需要按鈕的寬度。

代碼:

面板會切換到相反狀態
如果你真的需要兩個部分,而不是兩個按鈕或切換按鈕,你將需要獲得鼠標一個鍵座標點擊,並有不同的充邏輯取決於點擊發生的位置。

private void btnExpand_Click(object sender, EventArgs e) 
{ 
    splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed; 
} 

Handel邏輯與展開/摺疊相關聯。我選擇使用這個事件,因爲我的程序中有幾種方法可以讓用戶摺疊/展開面板。

private void splitContainer1_Panel2_ClientSizeChanged(object sender, EventArgs e) 
    { 

     if (splitContainer1.Panel1Collapsed) 
     { 
      splitContainer2.Panel1.Cursor = Cursors.Default; 
      this.btnExpand.Image = imageExpand; 
     } 
     Else 
     { 
      splitContainer2.Panel1.Cursor = Cursors.VSplit; 
      this.btnExpand.Image = imageCollapse; 
     } 
    } 

亨德爾由於移動人造分流

private void splitContainer2_Panel1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (e.Button == System.Windows.Forms.MouseButtons.Left) 
     { 

    /* All you really need is this: 
       splitContainer1.SplitterDistance += e.X; 

    Note: Splitter distance must be a positive integer and e.X will be negitive when dragging to the left of the splitContainer. You could handel this check here or on the splitterMoving event. 

     The code I have below shows how to 「snap」 a panel closed if the splitter is moved close enough to the edge 
    Or prevent a panel from being hidden from view (which could also be accomplished by setting the minimum size of a panel). 

    */ 

      if (e.X + splitContainer1.SplitterDistance < 40) 
      { 

       while (splitContainer1.SplitterDistance > 1) 
        splitContainer1.SplitterDistance--; 
       splitContainer1.Panel1Collapsed = true; 

      } 
      else if ((e.X + splitContainer1.SplitterDistance) * 1.00/this.Width * 1.00 < .75) 
       splitContainer1.SplitterDistance += e.X; 
      else 
       Cursor.Current = Cursors.No; 


     } 
}