C# 控件 SplitContainer






2.80/5 (28投票s)
.Net Framework 2.0 分割容器
假设
本文假定您对 TreView 和 ListView 高级控件有所了解。我现在使用的是 Visual Studio 2005 Beta2,您可以在 Visual Studio 2003 中通过合并部分类来打开它,并且肯定可以使用 .Net Framework 2.0。引言
基于 Windows 的桌面界面可以分为三种类型:1) SDI - 单文档界面,如 Microsoft Notepad;2) MDI - 多文档界面,如 Microsoft Word;以及 3) WE - Windows 资源管理器树形视图和列表视图风格界面。使用分割容器设计像“Windows 资源管理器”这样的窗体现在变得容易多了。我将通过一个简单的示例向您演示如何在应用程序中使用高级控件“分割容器”。
描述
SplitContainer
是一个高级的 Windows 控件,或者说是旧分割器控件的扩展版本。您可以水平或垂直放置它,方法是指定 Orientation 属性,每个属性分别代表左/上和右/下面板。
属性
1) Dock(停靠)
允许您将分割容器的边框停靠到其容器上。当您将分割容器拖放到窗体上时,Dock 属性的默认值为“填充”,这意味着分割容器将获得窗体的整个区域。在本例中,我将其设置为“无”,以便演示分割容器的功能。
2) Panel1Collapsed / Panel2Collapsed(面板1折叠 / 面板2折叠)
用于设置或获取面板1或面板2是否折叠或展开。如果您将 true 分配给 Panel1Collapsed,则左侧或上侧面板将折叠,如果您将 false 分配给 Panel1Collapsed,则左侧或上侧面板将展开。Panel2Collapsed 属性的行为相同。
以下是切换折叠和展开行为的代码行
/**
* Collapse left or top panel and expande right or bottom panel
*/
splitContainer1.Panel1Collapsed = true;
splitContainer1.Panel2Collapsed = false;
/**
* Collapse right or bottom panel and expande left or top panel
*/
splitContainer1.Panel2Collapsed = true;
splitContainer1.Panel1Collapsed = false;
3) Orientation(方向)
用于设置或获取分割容器面板的方向。
/**
* set orintation of slpitcontainer to Horizantal
*/
splitContainer1.Orientation = Orientation.Horizontal;
/**
* set orintation of slpitcontainer to Vertical
*/
splitContainer1.Orientation = Orientation.Vertical;
4) Border Style(边框样式)
用于设置或获取分割容器的边框样式。
/**
* set border style of splitcontainer to Fixed3D
*/
splitContainer1.BorderStyle = BorderStyle.Fixed3D;
/**
* set border style of splitcontainer to FixedSingle
*/
splitContainer1.BorderStyle = BorderStyle.FixedSingle;
/**
* set border style of splitcontainer to None
*/
splitContainer1.BorderStyle = BorderStyle.None;
5) Splitter Width(分割器宽度)
分割器是一个水平或垂直的条,用于设置面板大小。分割器的默认大小为 4 像素,但您可以通过操作 splitterwidth 属性以编程方式设置或获取分割器宽度值。
/**
* In this example numericupdown controll is used to demonstarte that when you
* change the value of numericaupdown its changeValue apply to splitterwidth
* property of splitcontainer
*/
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
NumericUpDown nm = (NumericUpDown)sender;
splitContainer1.SplitterWidth =(int) nm.Value ;
}
事件
1) SplitterMoving(分割器移动中)
当分割控件处于移动状态时,将发生此事件。
private void splitContainer1_SplitterMoving(object sender, SplitterCancelEventArgs e) { if(splitContainer1.Orientation == Orientation.Vertical ) Cursor.Current = Cursors.NoMoveVert; else Cursor.Current = Cursors.NoMoveHoriz; }
2) SplitterMoved(分割器移动后)
当分割控件移动后,将发生此事件。
private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e) { Cursor.Current = Cursors.Default; }
用法
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace SplitContainerDemo
{
public partial class MySplitContainer : Form
{
public MySplitContainer()
{
InitializeComponent();
//Loads values to TreeView and ListView control
this.loadValuesInto();
}
private void loadValuesInto()
{
// Create root node in TreeView
TreeNode tnRoot = treeView1.Nodes.Add("root", "/");
treeView1.Nodes[treeView1.Nodes.IndexOfKey("root")].Nodes.Add("Node-1", "Node-1");
//Finde Node in collection
TreeNode[] nodes = treeView1.Nodes.Find("Node-1", true);
//Assume that node will always be in TreeView then Add Childs to Node
addChildTo(nodes[0]);
treeView1.Nodes[treeView1.Nodes.IndexOfKey("root")].Nodes.Add("Node-2", "Node-2");
nodes = treeView1.Nodes.Find("Node-2", true);
addChildTo(nodes[0]);
treeView1.Nodes[treeView1.Nodes.IndexOfKey("root")].Nodes.Add("Node-3", "Node-3");
nodes = treeView1.Nodes.Find("Node-3", true);
addChildTo(nodes[0]);
}
//Adds childs to specific node
private void addChildTo(TreeNode tn)
{
for (int i = 1; i <= 10; i++)
tn.Nodes.Add(tn.Name + ".Child-" + i, "Child-" + i);
}
//Occurs when Left panel need to collapse
private void rdoLeft_CheckedChanged(object sender, EventArgs e)
{
splitContainer1.Panel1Collapsed = true;
splitContainer1.Panel2Collapsed = false;
}
//Occurs when Right panel need to collapse
private void rdoRight_CheckedChanged(object sender, EventArgs e)
{
splitContainer1.Panel2Collapsed = true;
splitContainer1.Panel1Collapsed = false;
}
//Occurs when Defaul behaviour is required of splitcontainer
private void rdoDefault_CheckedChanged(object sender, EventArgs e)
{
splitContainer1.Panel1Collapsed = false;
splitContainer1.Panel2Collapsed = false;
}
//Occurs when Horizantal orientation is required
private void rdoHorizantal_CheckedChanged(object sender, EventArgs e)
{
splitContainer1.Orientation = Orientation.Horizontal;
}
//Occurs when Vertical orientation is required
private void rdoVertical_CheckedChanged(object sender, EventArgs e)
{
splitContainer1.Orientation = Orientation.Vertical;
}
//Occurs when Fixed3D border style is required
private void rdoFix3d_CheckedChanged(object sender, EventArgs e)
{
splitContainer1.BorderStyle = BorderStyle.Fixed3D;
}
//Occurs when FixedSingle border style is required
private void rdoFixSingle_CheckedChanged(object sender, EventArgs e)
{
splitContainer1.BorderStyle = BorderStyle.FixedSingle;
}
//Occurs when None border style is required
private void rdoNone_CheckedChanged(object sender, EventArgs e)
{
splitContainer1.BorderStyle = BorderStyle.None;
}
//Occurs when TreeView node get focus, it first clears all listview items and
//add node's childs to listview controll
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
listView1.Items.Clear();
for (int i = 0; i < e.Node.Nodes.Count; i++)
listView1.Items.Add(e.Node.Nodes[i].Name);
}
//Occurs when value is changed in NumericUpDown controll
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
NumericUpDown nm = (NumericUpDown)sender;
splitContainer1.SplitterWidth = (int)nm.Value;
}
//Occurs when splitter is moving
private void splitContainer1_SplitterMoving(object sender, SplitterCancelEventArgs e)
{
if (splitContainer1.Orientation == Orientation.Vertical)
Cursor.Current = Cursors.NoMoveVert;
else
Cursor.Current = Cursors.NoMoveHoriz;
}
//Occurs when splitter has moved
private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
{
Cursor.Current = Cursors.Default;
}
}
}
结论
我希望这能让您了解分割容器,我非常感谢任何反馈、评论和建议。