Add Splitters
Here is an example of how to add splitters in SharpDevelop:
- add a control, for instance a ListBox
- set its Dock property to Left
- add a splitter and set its Dock property to Left as well
Now the form probably shows a ListBox on the left the same height as the form with a splitter to the right, also the same height as the form. If the ListBox is not on the left of the Splitter then right click the ListBox and choose SendToBack, this should put it on the left
- Set the width of the splitter to some sensible value, notice that only the right edge is draggable.
- Add another control, for instance, a TextBox to the right of the splitter and set its Dock property to Left.
Notice that it probably ends up to the left of the ListBox. Right click and choose BringToFront, its should now be to the right of the Splitter.
Run the project. Drag the Splitter and notice that the ListBox is resized and the TextBox moves.
The key is to realize that the zOrder (SendToBack, BringTofront) determines the order in the 'docking sequence'. If you examine the generated code you should see that the TextBox is created first and added to the form's Controls list, then the Splitter and then the ListBox.
If you rearrange the code so that the ListBox is created first, then the Splitter and then the TextBox the ListBox and the TextBox should swap places, TextBox on the left, then Splitter and then ListBox.
Remember that the zOrder is not a property of a control it is merely a way of setting the order in which controls are added to the form's Controls list (or other container) at design time. When you examine the code you will see that this is simply an indirect way of setting the order in which controls are added to the controls collection of a container.
You will notice that only the left most control is sized. The one on the right of the splitter moves but does not fill all the space to the right. To make this happen set the Dock property of the right most control to Fill.
SharpDevelop versus Visual Studio
Apart from the obvious USD700 difference and the fact that SharpDevelop is a one man effort whereas Visual Studio has the might of Microsoft behind it there are some more down to earth points worth making. One is that the form designers produce different code. In the InitializeComponent function created by SharpDevelop each control is added to the controls collection of the container by line of code similar to this:
this.Controls.Add(pictureBox);immediately after the code that sets up the control. Visual Studio instead waits until all the controls have been created and uses the following construction:
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.splitter1, this.listBox1, this.panel1});
In both cases the zOrder is determined by the order in which the items are added to the collection: left-most and top-most first.
Example Code
Here is the code created for the discussion above:
// project created on 2003-01-24 at 12:12 using System; using System.Windows.Forms; namespace MyFormProject { class MainForm : Form { private System.Windows.Forms.ListBox listBox; private System.Windows.Forms.Splitter splitter; private System.Windows.Forms.TextBox textBox; public MainForm() { InitializeComponents(); } // This method is used in the forms designer. // Change this method on you own risk void InitializeComponents() { // // Set up generated class MainForm // this.SuspendLayout(); this.Name = "MainForm"; this.Text = "This is my form"; // // Set up member listBox // listBox = new System.Windows.Forms.ListBox(); listBox.Name = "listBox"; listBox.Size = new System.Drawing.Size(72, 264); listBox.Dock = System.Windows.Forms.DockStyle.Left; listBox.TabIndex = 0; this.Controls.Add(listBox); // // Set up member splitter // splitter = new System.Windows.Forms.Splitter(); splitter.Name = "splitter"; splitter.TabIndex = 1; splitter.Size = new System.Drawing.Size(16, 273); splitter.Location = new System.Drawing.Point(72, 0); this.Controls.Add(splitter); // // Set up member textBox // textBox = new System.Windows.Forms.TextBox(); textBox.Name = "textBox"; textBox.TabIndex = 2; textBox.Dock = System.Windows.Forms.DockStyle.Left; textBox.Location = new System.Drawing.Point(88, 0); textBox.Size = new System.Drawing.Size(56, 20); textBox.Text = "textBox"; this.Controls.Add(textBox); this.ResumeLayout(false); } [STAThread] public static void Main(string[] args) { Application.Run(new MainForm()); } } }
No comments:
Post a Comment