450 likes | 559 Views
Building Rich Client Experience. Lesson: Creating a Form. Windows Forms vs. Web Forms How to Create a Form How to Set Form Properties Form Life Cycle How to Handle Form Events Windows Form Designer-Generated Code. Feature. Windows Forms. Web Forms. Deployment.
E N D
Lesson: Creating a Form • Windows Forms vs. Web Forms • How to Create a Form • How to Set Form Properties • Form Life Cycle • How to Handle Form Events • Windows Form Designer-Generated Code
Feature Windows Forms Web Forms Deployment Can be run without altering the registry No download required Graphics Includes GDI+ Interactive or dynamic graphics require round trips to the server for updates Responsiveness Provide the quickest response speed for interactive applications Can take advantage of the browser's dynamic HTML to create rich UI Platform Requires .NET Framework running on the client computer Require only a browser Programming model Based on a client-side, Win32-based message-pump mode Applications components are invoked via HTTP Security Code-based and role-based security Role-based security Windows Forms vs. Web Forms
How to Create a Form • A base form is created when you create a new project • To create a new form 1. Right-click the project in Solution Explorer 2. Click Add 3. Click Add Windows Forms
Events Button Form Name Categorized Button Alphabetic Button Description Pane How to Set Form Properties
Form Life Cycle 1. Form1 Show 5. Form2 Load 7.Form2 GotFocus 4. Form2 Show 2. Form1 Load 8. Form2 Activated 3. Form1 Activated 10. Form2 LostFocus 6. Form1 Deactivate 9. Focus shifts back to Form1 11. Form2 Deactivate 12. Form1 Activated 13. Close Form2 14. Form1 Deactivate 15. Form2 GotFocus 21. Form1 Activated 16. Form2 Activated 17. Form2 Closing 24. Form1 Closing 23. Exit Application 18. Form2 Closed 25. Form1 Closed 19. Form2 LostFocus 26. Form1 LostFocus 20. Form2 Deactivate 27. Form1 Deactivate 22. Form2 Disposed 28. Form1 Disposed
How to Handle Form Events Events
Lesson: Adding Controls to a Form • How to Add Controls to a Form • How to Add Menus to a Form • How to Customize the Controls Toolbox • Practice: Creating a Form and Adding Controls
Right-click the Toolbox 1 Click Customize Toolbox 2 Select the required control on the .NET Framework Components page 3 How to Customize the Controls Toolbox
Lesson: Creating an Inherited Form • Access Modifiers • How to Create an Inherited Form
Access Modifier Description Private Read-only to a child form, all of its property values in the property browser are disabled Protected Accessible within the class and from any class that inherits from the class that declared this member Public Most permissive level. Public controls have full accessibility Access Modifiers
How to Create an Inherited Form Create an inherited form by using the Inheritance Picker dialog box Create an inherited form programmatically public class Form2 : Namespace1.Form1
Lesson: Organizing Controls on a Form • How to Arrange Controls on a Form by Using the Format Menu • How to Set the Tab Order for Controls • How to Anchor a Control in Windows Forms • How to Dock a Control in Windows Forms
How to Set the Tab Order for Controls • To set the tab order for controls • On the View menu, select Tab Order • Click a control to change its tab order -- OR -- • Set the TabIndex property • Set the TabStop property to True
How to Anchor a Control in Windows Forms • Anchoring • Ensures that the edges of the control remain in the same position with respect to the parent container • To anchor a control to the form • Set its Anchor property • Default value: Top, Left • Other Styles: Bottom, Right
How to Dock a Control in Windows Forms • Docking • Enables you to glue the edges of a control to the edges of its parent control • To dock a control • Set the Dock property
Lesson: Creating MDI Applications • SDI vs. MDI Applications • How to Create MDI Applications • How Parent and Child Forms Interact • Practice: Creating an MDI Application
SDI vs. MDI Applications SDI MDI • Only one document is visible • Displays multiple documents at the same time • You must close one document before you open another • Each document is displayed in its own window
How to Create MDI Applications • To create a parent form • Create a new project • Set the IsMdiContainer property to True • Add a menu item to invoke the child form • To create a child form • Add a new form to the project • To call a child form from a parent form protected void MenuItem2_OnClick(object sender, System.EventArgs e) { Form2 NewMdiChild = new Form2(); // Set the Parent Form of the Child window. NewMdiChild.MdiParent = this; // Display the new form. NewMdiChild.Show(); }
How Parent and Child Forms Interact • To list the available child windows that are owned by the parent • Create a menu item (Windows) and set its MdiList property to True • To determine the active MDI child • Use the ActiveMdiChild property • To arrange child windows on the parent form • Call the LayoutMdi method Form activeChild = this.ActiveMdiChild;
Lesson: Adding ADO.NET Objects to and Configuring ADO.NET Objects in a Windows Forms Application • ADO.NET Objects • What Is a DataSet? • What Is a Typed DataSet? • How to Add ADO.NET Objects to and Configure ADO.NET Objects in a Windows Forms Application
ADO.NET Objects DataSet DataAdapter Data Source DataTable SelectCommand Fill Update Connection UpdateCommand DataAdapter DataTable SelectCommand Fill Update UpdateCommand
What Is a DataSet? • Datasets can include multiple DataTables • Relationships between tables are represented using DataRelations • Constraints enforce primary and foreign keys • Use the DataRow and DataColumn to access values in Tables DataColumn DataRow DataTable DataRelation
What Is a Typed DataSet? • Typed datasets • Derive from the base DataSet class • Provide type checking at compile time • Provide faster access to tables and columns in the dataset • Generated from XML Schema (.xsd) files by using the XSD.exe tool • To access tables and columns • Untyped dataset PubsDataSet.Tables("Titles"); • Typed dataset PubsDataSet.Titles;
Drag an OleDbDataAdapter or SqlDataAdapter object from the Toolbox to a form 1 Specify connection and SQL command information 2 Select the adapter or adapters that will be used to transfer data between the data source and the dataset 3 On the Data menu, choose Generate Dataset 4 Select New and then specify a name for the new dataset 5 How to Add ADO.NET Objects to and Configure ADO.NET Objects in a Windows Forms Application
Lesson: Accessing and Modifying Data by Using DataSets • How to Populate a Dataset • How to Update Data in a Dataset • How to Update Data to a Data Source • How to Create Database Schema on the Client • How to Read and Write XML Data into a DataSet
How to Populate a Dataset • Use the DataAdapter object to fill the dataset SqlDataAdapter storesSQLDataAdapter; SqlCommand storesSelectSQLCommand; storesSelectSQLCommand.CommandText = "SELECT * FROM stores"; storesSelectSQLCommand.Connection = SqlConnection1; storesSQLDataAdapter.SelectCommand = storesSelectSQLCommand; storesSQLDataAdapter.Fill(storesDataSet.Tables["Stores"]);
How to Update Data in a DataSet • Adding rows • Editing rows • Deleting data DataRow newDataRow = pubsDataSet.Tables["Titles"].NewRow(); newDataRow["title"] = "New Book"; newDataRow["type"] = "business"; pubsDataSet.Tables["Titles"].Rows.Add(newDataRow); changeDataRow.BeginEdit( ); changeDataRow["Title"] = changeDataRow["Title"].ToString() + " 1"; changeDataRow.EndEdit( ); DataRow deleteDataRow = pubsDataSet.Tables["Titles"].Rows[0]; pubsDataSet.Tables["Titles"].Rows.Remove(deleteDataRow);
How to Update Data to a Data Source • Explicitly specifying the updates SqlCommand insertTitlesCommand = new SqlCommand ("Insert titles (title_id, title, type) values (@title_id,@title,@type)"); insertTitlesCommand.Parameters.Add ("@title_id", SqlDbType.VarChar, 6, "title_id"); insertTitlesCommand.Parameters.Add ("@title", SqlDbType.VarChar, 80, "title"); insertTitlesCommand.Parameters.Add ("@type", SqlDbType.Char, 12, "type"); titlesSQLDataAdapter.InsertCommand = insertTitlesCommand; titlesSQLDataAdapter.Update(pubsDataSet, "titles");
How to Create Database Schema on the Client • XML Schema (.xsd) files enforce data integrity on the client • Use the XML Designer to create and modify XML Schema files 1. Determine the schema design 2. On the Project menu, click Add New Item 3. Add the schema 4. Create the schema
How to Read and Write XML Data into a DataSet • Use ReadXML to load data from a file or stream • Write data and schema information from a DataSet to a file or stream by using the WriteXML method purchaseDataSet.ReadXml ("C:\\sampledata\\PurchaseData.xml", XmlReadMode.IgnoreSchema); purchaseDataSet.WriteXml ("C:\\sampledata\\CurrentOrders.xml", XmlWriteMode.IgnoreSchema);
Lesson: Binding Data to Controls • How to Perform Simple Binding by Using the DataBindings Property • How to Perform Complex Data Binding by Using the DataBound Windows Forms Controls • How to Maintain the Currency of a Control by Using CurrencyManager • How to Format and Parse Data Bound Values
Property of the control to which data is bound txtCustomerAddress.DataBindings.Add("Text", dsNorthwindData1.Customers, "Address"); txtCustomerCity.DataBindings.Add("Text", dsNorthwindData1.Customers, "City"); Table from the data source Column in the table How to Perform Simple Binding by Using the DataBindings Property To use the DataBindings Collection to bind a control to a data source, set the DataBinding property of the control to the data source
How to Perform Complex Data Binding by Using the DataBound Windows Forms Controls • Complex data binding • Bind a control property to a data table • Use with combo boxes, list boxes, data grids • Can bind controls to DataSets, Arrays, and ArrayLists • Complex databinding at design time • Set the DataSource and DataMember properties • Complex databinding at run time DataGrid1.DataSource = productsDataSet; DataGrid1.DataMember = "Products";
How to Maintain the Currency of a Control by Using CurrencyManager TextBox1 Currency Manager1 TextBox2 Data Source 1 Currency Manager2 Datagrid Data Source 2 CurrencyManager cm; cm = (CurrencyManager)this.BindingContext[pubsDataSet, "Authors"]; cm.Position += 1;
How to Format and Parse Data Bound Values 10 $10 Format TextBox1 10 Parse $10 Data Source Binding Object
Lesson: Persisting Data • How to Persist Data in Files • How to Serialize Objects • How to Persist Application Settings
Class Description BinaryReader and BinaryWriter These classes read and write primitive types as binary values in a specific encoding to and from a stream. StreamReader and StreamWriter The implementations of these classes are designed for character input and output. StringReader and StringWriter The implementations of these classes are designed for string input and output. How to Persist Data in Files • Use readers and writers for persisted data
Add the Serializable attribute Serialize the object to a file 1 1 Serialize the object to a file Deserialize the file into an object 2 2 Deserialize the file into an object 3 How to Serialize Objects • Binary serialization • XML serialization
How to Persist Application Settings • Choose a technique • Use a DataSet object Good for tabular or relational data • Use reader/writer objects Complete control, but developer must writeand maintain more code • Use serialization Good choice when application stores state in objects • Choose a storage location • The file system