1. Trang chủ
  2. » Công Nghệ Thông Tin

Giáo trình C Nâng Cao

508 837 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 508
Dung lượng 7,54 MB

Nội dung

Microsoft .Net Framework 2.0 Windows-Based Client Development 1 2 3 4 5 6 7 8 11 12 15 Contents Windows Forms and the User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Configuring Controls and Creating the User Interface . . . . . . . . . . . . . . 49 Advanced Windows Forms Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Tool Strips, Menus, and Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Configuring Connections and Connecting to Data . . . . . . . . . . . . . . . . 197 Working with Data in a Connected Environment . . . . . . . . . . . . . . . . . . 251 Create, Add, Delete, and Edit Data in a Disconnected Environment . . 329 Implementing Data-Bound Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 Advanced Topics in Windows Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521 Enhancing Usability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555 Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663 Crystal report 687 3-tier architecture . 699 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 1 Windows Forms and the User Interface This chapter introduces you to Windows Forms. Windows Forms are the basis for most Microsoft Windows applications and can be configured to provide a variety of user interface (UI) options. The developer can create forms of various sizes and shapes and customize them to the user’s needs. Forms are hosts for controls, which provide the main functionality of the user interface. Special controls called container controls can be used to control the layout of the user interface. Exam objectives in this chapter: ■ Add and configure a Windows Form. ❑ Add a Windows Form to a project at design time. ❑ Configure a Windows Form to control accessibility, appearance, behavior, configuration, data, design, focus, layout, style, and other functionality. ■ Manage control layout on a Windows Form. ❑ Group and arrange controls by using the Panel control, GroupBox control, TabControl control, FlowLayoutPanel control, and TableLayoutPanel control. ❑ Use the SplitContainer control to create dynamic container areas. ■ Add and configure a Windows Forms control. ❑ Use the integrated development environment (IDE) to add a control to a Windows Form or other container control of a project at design time. ❑ Add controls to a Windows Form at run time. Lessons in this chapter: ■ Lesson 1: Adding and Configuring Windows Forms. . . . . . . . . . . . . . . . . . . . . . 3 ■ Lesson 2: Managing Control Layout with Container Controls. . . . . . . . . . . . . 24 1 3 Lesson 1: Adding and Configuring Windows Forms Lesson 1: Adding and Configuring Windows Forms This lesson describes how to create and configure Windows Forms. You will learn how to create forms and refer to them in code, alter the visual properties of the form, and control the behavior of the form at run time. After this lesson, you will be able to: ■ Add a Windows Form to a project at design time. ■ Add a new Windows Form at run time. ■ Resize a window at design time or run time. ■ Identify and set the properties that determine a form’s appearance and behavior at run time. ■ Refer to the default instance of a form in code. ■ Create a non-rectangular form. Estimated lesson time: 45 minutes Overview of Windows Forms Windows Forms are the basic building block of the user interface. They provide a container that hosts controls and menus and allow you to present an application in a familiar and consistent fashion. Forms can receive user input in the form of key- strokes or mouse interactions and can display data to the user through hosted con- trols. Although it is possible to create applications that do not contain forms, such as console applications or services, most applications that require sustained user interaction will include at least one Windows Form, and complex applications fre- quently require several forms to allow the program to execute in a consistent and logical fashion. When you create a new Windows Forms project, a form named Form1 is added to your project by default. You can edit your form by adding controls and other visual elements in the designer, which is a graphic representation of a designable, visual ele- ment (such as a Form) that appears in the Visual Studio Integrated Development Envi- ronment (IDE). The Visual Studio IDE is shown in Figure 1-1. 4 Chapter 1 Windows Forms and the User Interface Figure 1-1 A Windows Form in the Visual Studio IDE Adding Forms to Your Project Most projects will require more than one form. You can add and configure additional forms at design time, or you can create instances of pre-designed forms in code at run time. � To add a new form to your project at design time 1. From the Project menu, select Add Windows Form. The Add New Item dialog box opens. 2. Select Windows Form and type a name for the new form in the Name box. Click Add to add the form to the development environment. You can add and configure as many forms as your application needs at design time. You can also create new instances of forms in your code. This method is most often employed when you want to display a form that has already been designed. In Visual Basic, you can access default instances of a form by referring to that form by name. For example, if you have a form named Form1 in your application, you can refer to it directly by its name, Form1. 5 Lesson 1: Adding and Configuring Windows Forms � To access the default instance of a form at run time (Visual Basic only) 1. Refer to the form by its name. You can call methods or access properties from this default instance. For example: ' VB Form1.Text = "This is my form" Form1.Show() 2. If referring to a form from within that form’s code, you cannot use the default instance. You must use the special keyword Me (Visual Basic) or this (C#) to access the form’s properties and methods. � To access a form’s methods and properties from inside its code 1. Use the keyword Me (Visual Basic) or this( C#). For example: ' VB Me.Text = "J and J's Wine Shop – Main Page" // C# this.Text = "J and J's Wine Shop – Main Page"; 2. You can also create new instances of forms at run time by declaring a variable that represents a type of form and creating an instance of that form. � To add a form to your application at run time 1. Declare and instantiate a variable that represents your form. This example assumes that you have already designed a form named Form1 in your project: ' VB Dim myForm As Form1 myForm = New Form1() ' Displays the new form myForm.Show() // C# Form1 myForm; myForm = new Form1(); // Displays the new form myForm.Show(); Properties of Windows Forms The visual appearance of your user interface is an important part of your application. A user interface that is poorly designed is difficult to learn and will, therefore, increase training time and expense. You can modify the appearance of your user interface by using Windows Forms properties. 6 Chapter 1 Windows Forms and the User Interface Windows Forms contain a variety of properties that allow you to customize the look and feel of the form. You can view and change these properties in the Properties win- dow of the designer, as shown in Figure 1-2. Figure 1-2 The Properties window Table 1-1 summarizes some of the Windows Forms properties that are important in the look and feel of the application. Note that this is not an exhaustive list of all Windows Forms properties but, rather, a selected subset. Table 1-1 Some Properties of the Form Class Property Description (Name) Sets the name of the Form class shown in the designer. This property can be set only at design time. Backcolor Indicates the background color of the form. BackgroundImage Indicates the background image of the form. BackgroundImageLayout Determines how the image indicated by the Background- Image property will be laid out on the form. If no back- ground image is selected, this property has no effect. ControlBox Determines whether the form has a Control/System menu box. 7 Lesson 1: Adding and Configuring Windows Forms Table 1-1 Some Properties of the Form Class Property Description Cursor Indicates the cursor that appears when the cursor is moved over the form. Enabled Determines whether the form is able to receive user input. If Enabled is set to False, all controls contained by the form are likewise disabled. Font Sets the default font for the form. All controls contained by the form will also adopt this font unless their Font property is set separately. ForeColor Indicates the forecolor of the form, which is the color used to display text. All controls contained by the form will also adopt this forecolor unless their forecolor prop- erty is set separately. FormBorderStyle Indicates the appearance and behavior of the form bor- der and title bar. HelpButton Indicates whether the form has a Help button. Icon Indicates the icon that is used to represent this form. Location When the StartPosition property is set to Manual, this property indicates the starting location of the form rela- tive to the upper left-hand corner of the screen. MaximizeBox Indicates whether the form has a MaximizeBox. MaximumSize Determines the maximum size for the form. If this prop- erty is set to a size of (0,0) the form has no upper size limit. MinimizeBox Indicates whether the form has a MinimizeBox. MinimumSize Determines the minimum size to which the user can resize the form. 8 Chapter 1 Windows Forms and the User Interface Table 1-1 Some Properties of the Form Class Property Description Opacity Represents the opacity, or conversely the transparency of the form from 0% to 100%. A form with 100% opacity is completely opaque, and a form with 0% opacity is completely transparent. Size Gets and sets the initial size of the form. StartPosition Indicates the position of the form when the form is first displayed. Text Determines the text caption of the form. TopMost Indicates whether the form always appears above all other forms that do not have this property set to True. Visible Determines whether the form is visible when running. WindowState Determines whether the form is minimized, maximized, or set to the size indicated by the Size property when first shown. Modifying the Look and Feel of the Form You can use the Property Grid to set properties of the form at design time. Properties set in this manner will retain their values until the application starts, at which time they can be set in code. Most properties of a form can also be set at run time. The generalized scheme for set- ting a simple property is to use the assignment operator (=) to assign a value to a prop- erty. The following example demonstrates how to set the Text property of a form. ' VB Form1.Text = "This is Form 1" // C# Form1.Text = "This is Form 1"; Some properties, such as the Font or Size properties, are more complex. Their value is represented by an instance of a class or structure. For these properties, you can either set the property to an existing instance of the class, or create a new instance that speci- 9 Lesson 1: Adding and Configuring Windows Forms fies any subvalues of the property and assign it to the property as shown in the fol- lowing pseudocode example: ' VB PropertyY = New Class(value,value) // C# PropertyY = new Class(value,value); The (Name) property, which represents the name of the Form class, is an exception. This property is used within the namespace to uniquely identify the class that the Form is an instance of and, in the case of Visual Basic, is used to access the default instance of the form. Setting the Title of the Form The name of the form is the name that is used to refer to the Form class or the default instance of a form (Visual Basic only) in code, but it is also useful for the form to have a title that is visible to users. This title might be the same as the name of the form but is more often a description of the form itself, such as Data Entry. The title can also be used to convey information to the user, such as “Processing Entries — My Application” or “Customer Entry — My Application”. The title appears in the title bar and on the taskbar. You can change the title of a form by changing the Text property. To change the title of a form at design time, set the Text property of the form in the Property Grid. To change the title of a form at run time, set the Text property of the form in code, as shown in the following code: ' VB Form1.Text = "Please enter your address" // C# Form1.Text = "Please enter your address"; Setting the Border Style of the Form The border style of a form determines how the border of the form looks and, to a certain extent, how a form behaves at run time. Depending on the setting, the FormBorderStyle property can control how the border appears, whether a form is resizable by the user at run time, and whether various control boxes appear (although these are also deter- mined by other form properties). The FormBorderStyle property has seven possible val- ues, which are explained in Table 1-2. [...]... the contained controls Note that you can still manually change any property inside a contained control, but if the container is disabled, all controls inside that container will be inaccessible regardless of their individual prop­ erty settings Lesson 2: Managing Control Layout with Container Controls 25 The Controls Collection Each form and container control has a property called Controls, which represents... the Dock property, click the section of the interface that corresponds to where you want your control to dock For example, to dock your control to the right-hand side of the form, click the bar on the right of the interface To release docking, choose None Clicking the center of the Dock property interface sets the Dock property to a value of Fill, which means the the control will dock to all four sides... control The parent control can be a form or a container control such as a Panel control or a TabControl control Lesson 2: Managing Control Layout with Container Controls 27 Like the Anchor property, the Dock property provides a special visual interface that allows you to graphically choose the property value This interface is shown in Figure 1-7 Figure 1-7 Choosing the Dock property To set the Dock... the collection of controls contained by that form or control When a control is added to a form or container control at design time, the Designer automatically adds it to the controls collection of that form or container control and sets the location property as appropriate You can also dynamically add a new control at run time by manually cre­ ating a new control and adding the control to the controls... cells You can drag controls from the Toolbox into each of the cells Generally, only one con­ trol can be hosted in a single cell although, for complicated user interface designs, you can nest other container controls inside TableLayoutPanel cells, each of which can host multiple controls At run time, the appearance of the cells is determined by the CellBorderStyle property This property can be set... style is set to Relative Columns and rows can also be set to AutoSize When set to this value, the col­ umns and rows will automatically adjust to the correct size Column and row styles can also be set manually in code by accessing the ColumnStyles and RowStyles collections in code You can access the style for a particular column or row by the index of that column or row Styles can be set as shown in... no cell borders, or to a variety of different visual styles ColumnCount Property Indicates the number of columns You can add or remove columns by incrementing or decrementing the ColumnCount property Columns Property Represents the collection of columns Available only at design time; accessing this prop­ erty launches the Columns And Rows Styles editor ColumnStyles Property Represents the collection... TabPage controls, which themselves contain controls The user can switch between tab pages (and the controls contained therein) by clicking the tabs on the TabControl The most important property of the TabControl is the TabPages property TabPage con­ trols are specialized container controls that are hosted only inside TabControl con­ trols Each TabPage has its own set of properties, and you can access... Group and arrange controls with the FlowLayoutPanel control ■ Group and arrange controls with the TableLayoutPanel control ■ Create dynamic container areas with the SplitContainer control Estimated lesson time: 45 minutes Overview of Container Controls Container controls are specialized controls that serve as a customizable container for other controls Examples of container controls include the Panel,... You can create non-rectangular forms by creating a new instance of the Region class and then setting the form’s Region property to that new instance 24 Chapter 1 Windows Forms and the User Interface Lesson 2: Managing Control Layout with Container Controls This lesson describes how to add and configure container controls You will learn how to add controls to a form or to a container control and to configure . property to the new region Me.Region = myRegion // C# System.Drawing.Drawing2D.GraphicsPath myPath = new System.Drawing.Drawing2D.GraphicsPath(); // This line of code adds an ellipse to the. the form. � Exercise 2: Create a Non-Rectangular Windows Form 1. In this exercise, you will create a triangular Windows Form. 2. Open Visual Studio 20 05 and create a new Windows Forms project Microsoft .Net Framework 2. 0 Windows-Based Client Development 1 2 3 4 5 6 7 8 11 12 15 Contents Windows Forms and the User Interface . . . . . . . . . . . .

Ngày đăng: 17/11/2014, 11:06

TỪ KHÓA LIÊN QUAN

w