Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7 Dialog Controls MDI Parent/Child Interaction.

Similar presentations


Presentation on theme: "Lecture 7 Dialog Controls MDI Parent/Child Interaction."— Presentation transcript:

1 Lecture 7 Dialog Controls MDI Parent/Child Interaction

2 Multiple Document Interface Text Editor

3 private void fontToolStripMenuItem_Click(object sender, EventArgs e) { if (this.ActiveMdiChild != null) { FontDialog fontdlg = new FontDialog(); fontdlg.Font = this.ActiveMdiChild.Controls[0].Font; fontdlg.ShowDialog(); this.ActiveMdiChild.Controls[0].Font = fontdlg.Font; } FontDialog An instance of the FontDialog class has fields and methods for displaying and selecting font types, sizes and styles. In an MDI application we can direct actions onto the ActiveMdiChild from the MdiParent The widgets on a form are called Controls and can be accessed by their index value. For example the richtextbox of New-File-0 is this.ActiveMdiChild.Controls[0]

4 private void fontColorToolStripMenuItem_Click(object sender, EventArgs e) { if (this.ActiveMdiChild != null) { ColorDialog cdlg = new ColorDialog(); cdlg.Color = this.ActiveMdiChild.Controls[0].ForeColor; cdlg.ShowDialog(); this.ActiveMdiChild.Controls[0].ForeColor = cdlg.Color; } The ColorDialog can be used to select a standard color or to create a custom color that can be applied to any object that has a Color property. ColorDialog

5 private void backgroundColorToolStripMenuItem_Click(object sender, EventArgs e) { if (this.ActiveMdiChild != null) { ColorDialog cdlg = new ColorDialog(); cdlg.Color = this.ActiveMdiChild.Controls[0].BackColor; cdlg.ShowDialog(); this.ActiveMdiChild.Controls[0].BackColor = cdlg.Color; } ColorDialog

6 private void makeNewMdiChild() { MDIEditor.frmChild child = new MDIEditor.frmChild(this); child.Show(); this.ActiveMdiChild.Text = "New-File-" + Convert.ToString(newindex); newindex += 1; } private void newToolStripMenuItem_Click(object sender, EventArgs e) { makeNewMdiChild(); } private void openFile() { OpenFileDialog openFileDlg = new OpenFileDialog(); openFileDlg.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; openFileDlg.FilterIndex = 1; if (openFileDlg.ShowDialog() == DialogResult.OK) { makeNewMdiChild(); this.ActiveMdiChild.Text = openFileDlg.FileName; StreamReader r = new StreamReader(openFileDlg.FileName); this.ActiveMdiChild.Controls[0].Text = r.ReadToEnd(); r.Close(); } private void openToolStripMenuItem_Click(object sender, EventArgs e) { openFile(); } makeNewMdiChild( ) A new frmChild Form is created each time this method is called. A class-level variable, newindex is used to create a unique name for each new Form New-File-#. creates a new instance of frmChild and gives it a unique name. called when File-> New is clicked... also called when File->Open is clicked and filename returned from openFileDlg returns DialogResult.OK. in this case the name of the frmChild is changed to openFileDlg.FileName.

7 private void cascadeToolStripMenuItem_Click(object sender, EventArgs e) { this.LayoutMdi(MdiLayout.Cascade); } private void horizontalToolStripMenuItem_Click(object sender, EventArgs e) { this.LayoutMdi(MdiLayout.TileHorizontal); } private void verticalToolStripMenuItem_Click(object sender, EventArgs e) { this.LayoutMdi(MdiLayout.TileVertical); } Arrange - Cascade, Horizontal, Vertical when there are more than three child forms, the Arrange operations do not produce results implied by their names. CascadeTileHorizontalTileVertical >3 forms 3 forms

8 private void saveFileAs() { SaveFileDialog saveFileDlg = new SaveFileDialog(); saveFileDlg.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; saveFileDlg.FilterIndex = 1; if (saveFileDlg.ShowDialog() == DialogResult.OK) { StreamWriter w = new StreamWriter(saveFileDlg.OpenFile()); w.Write(this.ActiveMdiChild.Controls[0].Text); w.Close(); this.ActiveMdiChild.Text = saveFileDlg.FileName; } private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { if (this.ActiveMdiChild != null) saveFileAs(); else MessageBox.Show("Nothing to Save"); } Save As - SaveFileDialog

9 private void saveFile() { SaveFileDialog saveFileDlg = new SaveFileDialog(); saveFileDlg.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; saveFileDlg.FilterIndex = 1; saveFileDlg.FileName = this.ActiveMdiChild.Text; StreamWriter w = new StreamWriter(saveFileDlg.OpenFile()); w.Write(this.ActiveMdiChild.Controls[0].Text); w.Close(); } private void saveToolStripMenuItem_Click(object sender, EventArgs e) { if (this.ActiveMdiChild != null) { if (this.ActiveMdiChild.Text.Substring(0, 9) == "New-File-") saveFileAs(); else saveFile(); } else MessageBox.Show("Nothing to Save"); } private void saveFileAs() { SaveFileDialog saveFileDlg = new SaveFileDialog(); saveFileDlg.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; saveFileDlg.FilterIndex = 1; if (saveFileDlg.ShowDialog() == DialogResult.OK) { StreamWriter w = new StreamWriter(saveFileDlg.OpenFile()); w.Write(this.ActiveMdiChild.Controls[0].Text); w.Close(); this.ActiveMdiChild.Text = saveFileDlg.FileName; } Save - SaveFileDialog

10 Copy Style Paste Style public Color fontColor = Color.Black; public Color bkgColor = Color.White; public Font font; : private void copyStyleToolStripMenuItem_Click(object sender, EventArgs e) { if (this.ActiveMdiChild != null) { fontColor = this.ActiveMdiChild.Controls[0].ForeColor; bkgColor = this.ActiveMdiChild.Controls[0].BackColor; font = this.ActiveMdiChild.Controls[0].Font; } private void pasteStyleToolStripMenuItem_Click(object sender, EventArgs e) { if (this.ActiveMdiChild != null) { this.ActiveMdiChild.Controls[0].ForeColor = fontColor; this.ActiveMdiChild.Controls[0].BackColor = bkgColor; if (font != null) this.ActiveMdiChild.Controls[0].Font = font; } class-level variables default Colors no default font specified One of the most difficult tasks of applications development is the prediction and effective management of atypical User Behavior

11 private void closeToolStripMenuItem_Click(object sender, EventArgs e) { if(this.ActiveMdiChild != null) this.ActiveMdiChild.Dispose(); } Closing a Form private void closeToolStripMenuItem_Click(object sender, EventArgs e) { if(this.ActiveMdiChild != null) this.ActiveMdiChild.Close(); } We commonly refer to the action of removing a child form as Closing the Form. Actually what we are doing is disposing the form: Dispose( ) releases the object and prevents further use. Its memory resources are available for garbage collection, but are not necessarily returned immediately to the heap. Close( ) hides the object from view while leaving its properties, fields, methods, etc available.


Download ppt "Lecture 7 Dialog Controls MDI Parent/Child Interaction."

Similar presentations


Ads by Google