200 likes | 322 Vues
In Week 4 of CST238, we will discuss project ideas and address any questions or concerns. Remember that Test #1 is scheduled for next Monday, 4/28/14. We will recap Take Home Lab #3 and explore new topics including user-defined dialogs, timer control, the DateTime object, and working with rich text boxes. Additional topics will cover MDI (Multiple Document Interface), image handling with PictureBox, and menu integration. Don't miss in-class exercise #4 and a recap of Take Home Lab #4. Start building your understanding of dialogs and arrays in C#.
E N D
CST238 Week 4 • Questions / Concerns? • Announcements • Start thinking about project ideas • Test#1 next Monday, 4/28/14 • Recap • Check-off Take Home Lab#3 • New topics • Dialogs (user-defined dialogs) • Timer control • DateTime object • Richtextbox • MDI (Multiple Document Interface) • Parent/child form relationship • Richtextbox – open, save, cut, copy, paste • In-Class Exercise #4 • Take Home Lab#4
Recap • Picturebox • Image property • Load method • Layout controls • Table • Flow layout • Menu • Toolbar • Status bar • Working with C# arrays
Dialog • Forms that: • Provide information to the user, or • Request information from the user • Generally modal • Cannot switch forms until dialog is closed. • Several dialogs included with Windows Forms • MessageBox, Color, OpenFile, SaveFile, Print, etc. • You can create your own custom dialog
Working with Dialogs • Simple Dialogs • Just a function call • MessageBox.Show() • Common/Custom Dialog • Create instance of dialog • Set properites • Show dialog using ShowDialog method • Returns a meber of the DialogResult enumeration • Take action based on dialog result
Working with Dialog OpenFieDialog openFile1 = new OpenFileDialog(); openFile1.Filter = “JPEG Files (*.jpg)|*.jpg|PNG Files (*.png)|*.png|BMP Files (*.bmp)|*.bmp|All files (*.*)|*.*”; if (openFile1.ShowDialog() == DialogResults.OK) { //Do something with OpenFile1.FileName //which is the filename selected by the user if (openFile1.ShowDialog() == DialogResult.OK) { pictureBox1.Load(openFile1.FileName); } }
Working with Color Dialog ColorDialog colorDialog1 = new ColorDialog(); if (colorDialog1.ShowDialog() == DialogResult.OK) pictureBox1.BackColor = colorDialog1.Color; //selected color
Custom Dialog • Create form as you would any other • Set form properties to add dialog look, feel and behavior • Set FormBorderStyle to FixedDialog • Disables resizing of dialog • Set ControlBox property to false • Removes minimize, maximize and close buttons from title bar • Set AcceptButton and CancelButton properties • AcceptButton - pressing Enter is the same as clicking the button • CancelButton – pressing Escape is the same as clicking the button • Set dialog return value on button clicks • In event handler, or • Using the DialogResult property of the button
Timer Control • Add a timer control to the form • Set its interval – 1000 ms = 1 second • Set its Tick event handler to handle the timer event at every interval
DateTime • DateTime object in C# returns the current date and time. • Custom Date and Time Format Strings: http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx • Example: private DateTimetimeOfDay; Private string timeString; private string hour; private string minute; timeOfDay= DateTime.Now; timeString = timeOfDay.ToString("hh:mm:ss"); hour = timeOfDay.Hour.ToString("00"); minute = timeOfDay.Minute.ToString("00");
Richtextbox • Supports • Cut • Copy • Paste • SelectAll • SaveFile(filename) • LoadFile(filename) • SelectionColor • SelectionFont
MDI Apps • Main Form • Set IsMdiContainer to true • Use ActiveMdiChild to get reference to active child form • Use MdiChildren to get collection of child forms • Use LayoutMdi to arrange child forms • Child forms • Set MdiParent to main form on creation • Use MdiParent to get reference back to main form
MDI • Windows menu • Arrange the windows in Cascade • Arrange the windows in Tile Horizontally • Arrange the windows in Tile Vertically • Menustrip Control • Set MdiWindowList • This will show a list of active child windows
Access the Active Child form if (this.ActiveMdiChild != null) { //cast the ActiveMdiChild to the right form type ChildForm child=(ChildForm)this.ActiveMdiChild; //do something with childform’s control child.documentTextbox.Undo(); } documentTextboxis private. It needs to be internal. Change its modifier in the Properties window.
private vs. internal • All controls on a form are default to private modifier. • Only available to the containing form • In order for the Main Form to access the Childform’s control, the control’s modifier would have to be changed. • internal means that they are in the same assembly (.exe or .dll)
In-Class Lab#4 • Finish the MDI application by finishing the Open, Save, Cut, Copy,Paste, Change Font and Change Color menu options. • When open a new childForm window, set its title appropriately. • Be sure to change the childForm window title to the saved/opened filename.
Take-Home Exercise #4 • Build a Tix Clock • This is a Tix Clock showing 15:43.
Tix Clock • Use pictureboxes to simulate color LEDs. • Use random number to randomly color squares. • Tix clocks are updated every 4 seconds. • Use DateTime to get the current time. Be sure to grab 2 digits for hour and 2 digits for minute.