Title: Building Rich Client Experience
1Building Rich Client Experience
2Lesson 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
3Windows Forms vs. Web Forms
4How 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
5How to Set Form Properties
6Form 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
7How to Handle Form Events
Events
8Windows Forms Designer-Generated Code
9Lesson 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
10How to Add Controls to a Form
11How to Add Menus to a Form
12How to Customize the Controls Toolbox
13Lesson Creating an Inherited Form
- Access Modifiers
- How to Create an Inherited Form
14Access Modifiers
15How 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
16Lesson 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
17How to Arrange Controls on a Form by Using the
Format Menu
18How 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
19How 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
20How 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
21Lesson Creating MDI Applications
- SDI vs. MDI Applications
- How to Create MDI Applications
- How Parent and Child Forms Interact
- Practice Creating an MDI Application
22SDI 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
23How 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 // D
isplay the new form. NewMdiChild.Show()
24How 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
25Lesson 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
26ADO.NET Objects
DataSet
DataAdapter
Data Source
DataTable
SelectCommand
Fill
Update
Connection
UpdateCommand
DataAdapter
DataTable
SelectCommand
Fill
Update
UpdateCommand
27What 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
28What 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
PubsDataSet.Tables("Titles")
PubsDataSet.Titles
29How to Add ADO.NET Objects to and Configure
ADO.NET Objects in a Windows Forms Application
30Lesson 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
31How 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.SelectComma
nd storesSelectSQLCommand storesSQLDataAdapter
.Fill(storesDataSet.Tables"Stores")
32How to Update Data in a DataSet
- Adding rows
- Editing rows
- Deleting data
DataRow newDataRow pubsDataSet.Tables"Titles"
.NewRow() newDataRow"title" "New Book" new
DataRow"type" "business" pubsDataSet.Tables
"Titles".Rows.Add(newDataRow)
changeDataRow.BeginEdit( ) changeDataRow"Title"
changeDataRow"Title".ToString() " 1"
changeDataRow.EndEdit( )
DataRow deleteDataRow pubsDataSet.Tables"Titles
".Rows0 pubsDataSet.Tables"Titles".Rows.Rem
ove(deleteDataRow)
33How to Update Data to a Data Source
- Explicitly specifying the updates
SqlCommand insertTitlesCommand new SqlCommand
("Insert titles (title_id, title, type)
values (_at_title_id,_at_title,_at_type)")
insertTitlesCommand.Parameters.Add ("_at_title_id
", SqlDbType.VarChar, 6, "title_id")
insertTitlesCommand.Parameters.Add
("_at_title", SqlDbType.VarChar, 80, "title")
insertTitlesCommand.Parameters.Add
("_at_type", SqlDbType.Char, 12, "type")
titlesSQLDataAdapter.InsertCommand insertTitle
sCommand titlesSQLDataAdapter.Update(pubsDataSet
, "titles")
34How 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
35How 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\\Purch
aseData.xml",
XmlReadMode.IgnoreSchema)
purchaseDataSet.WriteXml ("C\\sampledata\\Curren
tOrders.xml", XmlWriteMode.IgnoreSchema)
36Lesson 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
37How 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
38How 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"
39How 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.Bi
ndingContextpubsDataSet, "Authors" cm.Positi
on 1
40How to Format and Parse Data Bound Values
10
10
TextBox1
Format
10
Parse
10
Binding Object
Data Source
41Lesson Persisting Data
- How to Persist Data in Files
- How to Serialize Objects
- How to Persist Application Settings
42How to Persist Data in Files
- Use readers and writers for persisted
data
43How to Serialize Objects
44How 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
45Questions?