cst238 week 7 questions / concerns? announcements – hw#2 due today (project concept/preliminary...

14
CST238 Week 7 • Questions / Concerns? • Announcements – HW#2 due today (project concept/preliminary design) – Check-off Take Home lab#6 • Friday is the last day to withdraw from class. • GUI Bloopers presentations (#3&#4) • New topics – Data Binding • Coming up: – GUI Presentation #5 & #6 next Monday – WPF vs Windows Forms – Work on Final Project • Take Home Lab#7

Upload: ezra-mccormick

Post on 28-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CST238 Week 7 Questions / Concerns? Announcements – HW#2 due today (project concept/preliminary design) – Check-off Take Home lab#6 Friday is the last

CST238 Week 7• Questions / Concerns?• Announcements

– HW#2 due today (project concept/preliminary design)– Check-off Take Home lab#6

• Friday is the last day to withdraw from class. • GUI Bloopers presentations (#3&#4)• New topics

– Data Binding• Coming up:

– GUI Presentation #5 & #6 next Monday– WPF vs Windows Forms– Work on Final Project

• Take Home Lab#7

Page 2: CST238 Week 7 Questions / Concerns? Announcements – HW#2 due today (project concept/preliminary design) – Check-off Take Home lab#6 Friday is the last

Why Data Binding?• Windows Forms Problem– There is no clear separation of user interface and

data. – Controls and data are tightly coupled.

• Data binding doesn’t address this problem of tightly coupled UI and data, but it saves programmers some work:– Don’t have to write code to move data in and out

of controls.

Page 3: CST238 Week 7 Questions / Concerns? Announcements – HW#2 due today (project concept/preliminary design) – Check-off Take Home lab#6 Friday is the last

Data Binding• One-way: Automatically populating controls

with data– Set a few properties

• Two-way: Automatically propagating changes to data– From control to data source

Page 4: CST238 Week 7 Questions / Concerns? Announcements – HW#2 due today (project concept/preliminary design) – Check-off Take Home lab#6 Friday is the last

Data Binding• Core capability of .NET controls– Web and Windows– Built in at base Control level

• Interface-based– An interface contains only the signatures of

methods, properties, or events . – A class or struct that implements the interface

must implement the members of the interface that are specified in the interface definition.

Page 5: CST238 Week 7 Questions / Concerns? Announcements – HW#2 due today (project concept/preliminary design) – Check-off Take Home lab#6 Friday is the last

Data Binding• Simple data binding

• Bind a single value from a data collection to a single control property• Ex: customer name to Text property on Textbox

• Complex data binding• Bind collection of data to control that presents multiple

values from collection• Ex: customers table in grid, customer name values in

combo box.

Page 6: CST238 Week 7 Questions / Concerns? Announcements – HW#2 due today (project concept/preliminary design) – Check-off Take Home lab#6 Friday is the last

Data Binding• Simple data binding– Create a Binding object– Add to DataBindings collection of control

• Complex data binding– Set data source– Set data member– DisplayMember and ValueMember for ComboBox

and List Box controls

Page 7: CST238 Week 7 Questions / Concerns? Announcements – HW#2 due today (project concept/preliminary design) – Check-off Take Home lab#6 Friday is the last

C#: Fields & Properties of an object• Fields – Internal data / state of an object. (ex. age)– Fields should not be public. – How do you access the field then?• Through methods (GetAge, SetAge)

• Method– Behavior. – Getters/setters are not really behavior though.

• Property– Another way to access the field

Page 8: CST238 Week 7 Questions / Concerns? Announcements – HW#2 due today (project concept/preliminary design) – Check-off Take Home lab#6 Friday is the last

C#: Propertyclass Student{ private int age; //private field

public int Age //Public property { get {return age;} set { age = value;} }…

Student aStudent = new Student();aStudent.age = 35;aStudent.Age = 35; //value is 35 if (aStudent.Age == 35) …

Page 9: CST238 Week 7 Questions / Concerns? Announcements – HW#2 due today (project concept/preliminary design) – Check-off Take Home lab#6 Friday is the last

C#: Propertyclass Student{ private int age; //private field

public int Age //Public property { get {return age;} set { age = value;} }…

public int Age {get; set;}

Page 10: CST238 Week 7 Questions / Concerns? Announcements – HW#2 due today (project concept/preliminary design) – Check-off Take Home lab#6 Friday is the last

Simple Data Binding• Binding object which binds control properties to object properties.

aStudent = new Student("Smith", "Gail", "918111111", true); LastnameBox.DataBindings.Add("Text", aStudent, "Lastname");FirstnameBox.DataBindings.Add("Text", aStudent, "Firstname");IDBox.DataBindings.Add("Text", aStudent, "ID");RegisteredCheckbox.DataBindings.Add("Checked", aStudent, "Registered");

aStudent

Lastname SmithFirstname Gail

ID 918111111Registered true

Page 11: CST238 Week 7 Questions / Concerns? Announcements – HW#2 due today (project concept/preliminary design) – Check-off Take Home lab#6 Friday is the last

Simple Data Binding• Binding object which binds control properties to object properties.

aStudent = new Student("Smith", "Gail", "918111111", true); LastnameBox.DataBindings.Add("Text", aStudent, "Lastname“, true, DataSourceUpdateMode.OnPropertyChanged);FirstnameBox.DataBindings.Add("Text", aStudent, "Firstname“, true, DataSourceUpdateMode.OnPropertyChanged);IDBox.DataBindings.Add("Text", aStudent, "ID“, true, DataSourceUpdateMode.Never);RegisteredCheckbox.DataBindings.Add("Checked", aStudent, "Registered“, true, DataSourceUpdateMode.OnPropertyChanged);

Lastname SmithFirstname Gail

ID 918111111Registered true

Page 12: CST238 Week 7 Questions / Concerns? Announcements – HW#2 due today (project concept/preliminary design) – Check-off Take Home lab#6 Friday is the last

Complex Data Binding• Bind a collection of data to a control• Collection must implement IList or IListSource interface

• Student Major List demo– Tab Control– Combo box & binding

• DisplayMember (MajorName object property) , ValueMember (MajorCode object property)

• DataSource

– Listbox, query & binding• DataSource, DisplayMember

– Datagrid view• DataSource

Page 13: CST238 Week 7 Questions / Concerns? Announcements – HW#2 due today (project concept/preliminary design) – Check-off Take Home lab#6 Friday is the last

Take-Home Lab #7• Create a Category class with 2 properties:– CategoryID (int)– CategoryName (string)

• Create a Product class with 4 properties:– ProductName (string)– CategoryID (int)– Unit Price (float)– OnSale (bool)

Page 14: CST238 Week 7 Questions / Concerns? Announcements – HW#2 due today (project concept/preliminary design) – Check-off Take Home lab#6 Friday is the last

Take Home Lab #7• Create a form:– Combo box to show the list of product categories– Listbox to show the products– Textboxes and checkbox bound to selected

product.