Lecture 7 Dialog Controls MDI Parent/Child Interaction.

11
Lecture 7 Dialog Controls MDI Parent/Child Interaction

Transcript of Lecture 7 Dialog Controls MDI Parent/Child Interaction.

Page 1: Lecture 7 Dialog Controls MDI Parent/Child Interaction.

Lecture 7Dialog Controls

MDI Parent/Child Interaction

Page 2: Lecture 7 Dialog Controls MDI Parent/Child Interaction.

Multiple Document Interface Text Editor

Page 3: Lecture 7 Dialog Controls MDI Parent/Child Interaction.

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]

Page 4: Lecture 7 Dialog Controls MDI Parent/Child Interaction.

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

Page 5: Lecture 7 Dialog Controls MDI Parent/Child Interaction.

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

Page 6: Lecture 7 Dialog Controls MDI Parent/Child Interaction.

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 andgives it a unique name.

called when File-> New is clicked...

also called when File->Open is clicked and filename returned from openFileDlgreturns DialogResult.OK.

in this case the name of the frmChild is changed to openFileDlg.FileName.

Page 7: Lecture 7 Dialog Controls MDI Parent/Child Interaction.

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.

Cascade TileHorizontal TileVertical>

3 fo

rms

3 fo

rms

Page 8: Lecture 7 Dialog Controls MDI Parent/Child Interaction.

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

Page 9: Lecture 7 Dialog Controls MDI Parent/Child Interaction.

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

Page 10: Lecture 7 Dialog Controls MDI Parent/Child Interaction.

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 Colorsno default font specified

One of the most difficult tasks of applications development is the prediction and effective management of atypical User Behavior

Page 11: Lecture 7 Dialog Controls MDI Parent/Child Interaction.

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.