View Full Version : [C#]Windows Forms - ToolStrips, LayoutPanels, SplitContainer, Menus, StatusStrips
neverland87
03-05-2007, 10:50 PM
Đây là bài thực hành về toolStrip, SplitContainer, Menus,... ở trang MSDN của Microsoft, mình đọc thấy hay, post lên để mọi người cùng làm, tuy bằng tiếng Anh, nhưng nhờ có hình ảnh minh họa rõ ràng, các bạn sẽ làm được ngay thôi mà. Sau đây là các bước thực hiện:
1.Open Visual Studio 2005 and create a new Windows Application project named "ToolStripsAndLayoutPanels".
2.Drag a MenuStrip from the Toolbox onto Form1.
By default, MenuStrip controls dock along the top of the form.
3.Set the Name property to "standardMenuStrip".
The MenuStrip presents two design interfaces. The first is in place, on the Form, through ghosted TextBoxes reading "Type Here". The second is through the MenuStrip Items Collection Editor, accessible in the Properties window in Items, or through the MenuStrip's smart tag menu.
VB (javascript:void(0))C# (javascript:void(0))J#http://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/images/clear.gifhttp://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/Library/PanelStrips.1.js.jpg
4.In the TextBox reading "Type Here", type "File".
New TextBox controls appear, a second directly to the right of the first, and a third below and to the right. File submenu items may be added by typing into the third TextBox. A second top-level menu may be added by typing into the second box. As menus are created by typing into the TextBox controls, new controls appear, making it very easy to build quickly a series of nested menus.
5.Right-click the File menu item created in the previous step and select Delete. Access the the MenuStrip Tasks menu by clicking the Smart Tag icon http://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/Library/SmartTagGlyph.jpg and click Insert Standard Items to auto-populate a standard menu.
http://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/Library/PanelStrips.2.jpg
6.To the right of Help click standardMenuStrip to expand the dropdown.
http://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/Library/PanelStrips.3.jpg
Using in-place menu authoring, three ToolStripItem controls are supported: MenuItem (e.g. File, Edit, Tools and Help), ComboBox and TextBox.
7Click TextBox to add a TextBox to standardMenuStrip. In the Properties window, set the following:
Alignment = Right
Text = "Help Text"
neverland87
03-05-2007, 10:51 PM
8.Right-click standardMenuStrip and select Embed in ToolStripContainer.
This puts the toolstrip into a ToolStripContainer as opposed to directly on the form giving you additional functionality, as you will see later in this walkthrough.
http://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/Library/PanelStrips.4.jpg
9.In the ToolStripContainer Tasks menu, click Dock Fill in Form.
10.Drag a ToolStrip control from the Toolbox so that it sits just below the MenuStrip control. Set the Name property to "standardToolStrip".
11.In the standardToolStrip Tasks menu click Insert Standard Items.
http://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/Library/PanelStrips.5.jpg
ToolStrip is a powerful and very customizable control for building highly functional professional-looking toolbars. Like the MenuStrip control, it provides two interfaces for designing toolbars: an in-place interactive interface and the Items Collection Editor for ToolStrips.
12.Select standardToolStrip, and in the dropdown that appears, click the arrow to display the dropdown list.
http://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/Library/PanelStrips.6.jpg
The items in the list are all ToolStripItems that you can add to a ToolStrip control. In addition, it is possible through code to add other control types. The ToolStripControlHost is used to host a non-ToolStripItem control on a ToolStrip. This makes it possible to display custom controls on ToolStrips.
13.In the dropdown list, select DropDownButton.
The ToolStripDropDownButton control supports adding dropdown menus to ToolStrips. These menus are as functional as those of the MenuStrip.
14.Using the in-place interface, add the following menu items:
Option 1
Option 2
Option 3
15.Click Option 2, and in the Properties window, set the following:
Checked = True
CheckOnClick = True
16.Click toolStripDropDownButton1, and in the Properties window, set the following:
DisplayStyle = Text
Text = "Options"
VBC# (javascript:void(0))J# (javascript:void(0))http://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/images/clear.gifhttp://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/Library/PanelStrips.7.vb.jpg
VB (javascript:void(0))C#J# (javascript:void(0))http://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/images/clear.gifhttp://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/Library/PanelStrips.7.cs.jpg
neverland87
03-05-2007, 10:53 PM
17.Drag a ToolStrip control from the Toolbox onto the right rafting panel, creating toolStrip1. In the smart tag menu, click Insert Standard Items.
http://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/Library/PanelStrips.8.jpg
ToolStrips may be rafted in any of a ToolStripContainer's four rafting panels, or unrafted to display in the ToolStripContainer's Content panel. You can write an event handler to respond to changing rafting conditions, for example to change the ToolStrip's TextDirection property dynamically as it is rafted around the form.
18.Drag a StatusStrip from the Toolbox onto an empty area in the Form1 component tray. Do not drop the StatusStrip control on one of the controls in the component tray.
19.Set the Dock property for the StatusStrip to bottom.
VBC# (javascript:void(0))J# (javascript:void(0))http://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/images/clear.gifhttp://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/Library/PanelStrips.9.vb.jpg
VB (javascript:void(0))C#J# (javascript:void(0))http://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/images/clear.gifhttp://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/Library/PanelStrips.9.cs.jpg
VB (javascript:void(0))C# (javascript:void(0))J#http://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/images/clear.gifhttp://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/Library/PanelStrips.9.js.jpg
The StatusStrip control, being a ToolStrip control, presents both the in-place and the Items Collection Editor interfaces for adding constituent ToolStripItems.
20.In statusStrip1, select StatusLabel in the dropdown control. Using the Properties window, set Text to "Status". As well, select ProgressBar and set its Value property to "40".
Now that you have seen some of the benefits provided by the new menu's and tool strip controls you will now examine some of the new layout controls.
21.Drag a TableLayoutPanel from Toolbox onto toolStripContainer1's ContentPanel in the middle of Form1. In the Properties window, set Dock to Fill.
http://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/Library/PanelStrips.10.jpg
22.In TableLayoutPanel Tasks menu, click the Edit Rows and Columns menu command.
23.In Column and Row Styles, add two columns. Change the properties of all the columns to match the following:
Member
Size Type
Value
Column1
Absolute
75
Column2
Percent
50%
Column3
Absolute
75
Column4
Percent
50%
http://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/Library/PanelStrips.11.jpg
neverland87
03-05-2007, 10:54 PM
24.Change the dropdown that reads Columns to Rows. Add two rows, then change the properties of all the rows to match the following, then click OK:
Member
Size Type
Value
Row1
Absolute
24
Row2
Absolute
24
Row3
Absolute
24
Row4
Percent
100%
25.From the Toolbox drag multiple Label and TextBox controls, as well as one ComboBox control, onto tableLayoutPanel1, creating the following form:
http://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/Library/PanelStrips.12.jpg
Controls contained in a TableLayoutPanel are given two additional properties: ColumnSpan and RowSpan. These properties allow a contained TextBox, for example, to span multiple columns or rows. This functionality is analogous to the RowSpan and ColumnSpan attributes of an HTML table cell.
26.In the Properties window for the following controls, set Anchor to Left, Right:
First Name
Last Name
Address
City
State
27.In the Properties window for the Address TextBox, set ColumnSpan to "3".
28.In the Properties window for the Comment TextBox, set the following properties:
Anchor = Top, Bottom, Left, Right
ColumnSpan = "3"
Multiline = True
http://msdn.microsoft.com/vstudio/tour/vs2005_guided_tour/vs2005pro/Library/PanelStrips.13.jpg
29.Select the Debug | Start Debugging menu command (F5) to run the application.
30.In the application resize the form to see how the TableLayoutPanel control dynamically lays out its contained controls according to the row and column configuration specified in Column and Row Styles.
As the form is resized, the controls within it are resized to grow and shrink with the form. The columns containing the labels were assigned an absolute width of 75 pixels, so their width remains fixed. The columns containing the controls, however, were assigned relative widths-percentages of the form width. As the form is resized, these percentages cause the controls to vary in relative size.
Similarly, the first three rows were assigned absolute heights, and so do not change height during form resizing. The fourth and last row, however, were given a relative height of 100%, which effectively fills the remaining form height after the first three rows.
The Address and Comment TextBoxes were set to span three columns, and so expand from the second column across the full width of the form.
In this walkthrough you learned how easy it can be to build rich user interfaces with the new menu, status strips, tool strip, and layout panel controls. Thereby saving time in development when compared with previous releases of Visual Studio.
tronghan
28-06-2007, 11:27 AM
Bạn oi học như này thì hay quá giá như bạn làm thêm được về phần kết nối và lọc dữ liệu thì hay quá
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd. Administrer by Kevin Hoang