23
Dr Dat Tran - Week 4 Lectur Dr Dat Tran - Week 4 Lectur e Notes e Notes 1 ToolStrip ToolStrip Programming Graphical User Interfaces PG Programming Graphical User Interfaces PG (7110) (7110) University of Canberra University of Canberra School of Information Sciences & School of Information Sciences & Engineering Engineering

Dr Dat Tran - Week 4 Lecture Notes 1 ToolStrip Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences &

Embed Size (px)

Citation preview

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 11

ToolStripToolStrip

Programming Graphical User Interfaces PG (7110)Programming Graphical User Interfaces PG (7110)

University of CanberraUniversity of CanberraSchool of Information Sciences & EngineeringSchool of Information Sciences & Engineering

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 22

ToolStripToolStripToolStrip class is aToolStrip class is abstract base class for MenuStrip, StatusStrip, and ContextMenuStripToolStrip control provides many members that manage ToolStrip control provides many members that manage painting, mouse and keyboard input, and drag-and-drop painting, mouse and keyboard input, and drag-and-drop functionality functionality Use ToolStrip controls to create toolbars that have a WindowsXP appearance with support for overflow and run-time item reordering ToolStrip controls handle events consistently for all ToolStrip controls handle events consistently for all containers and contained items, in the same way you handle containers and contained items, in the same way you handle events for other controls.events for other controls.You can drag items from one ToolStrip to another or within a You can drag items from one ToolStrip to another or within a ToolStripToolStrip

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 33

A Simple Text Editor A Simple Text Editor

We use an MDI form and a ToolStrip control We use an MDI form and a ToolStrip control to build a simple text editor.to build a simple text editor.The ToolStrip control contains standard items The ToolStrip control contains standard items such as New, Open, Save, Cut, Copy, Paste, such as New, Open, Save, Cut, Copy, Paste, and Printand PrintImplementation code is also added to Implementation code is also added to implement the standard itemsimplement the standard items

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 44

1. Add ToolStrip to Form1. Add ToolStrip to Form

drag & dropdrag & drop

rename itrename it

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 55

2. Insert Standard Items2. Insert Standard Items

click click

click click

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 66

3. Change Render Mode3. Change Render Mode

click click

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 77

4. Create Event Handlers4. Create Event Handlers

double-click each itemdouble-click each item

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 88

5. Change the Form to MDI Form5. Change the Form to MDI Form

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 99

6. Add Implementation Code 6. Add Implementation Code

int count;int count;Form mdiChild;Form mdiChild;TextBox editTextBox;TextBox editTextBox;

public MyNotePad()public MyNotePad(){{ InitializeComponent();InitializeComponent(); count = 1;count = 1;}}

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 1010

7. The New Item 7. The New Item private void private void newnewToolStripButton_Click(object ToolStripButton_Click(object sender, EventArgs e)sender, EventArgs e)

{{ mdiChild = new Form();mdiChild = new Form(); mdiChild.Text = "Document"mdiChild.Text = "Document" ++

count.ToString();count.ToString(); mdiChild.MdiParent = this;mdiChild.MdiParent = this; editTextBox = new TextBox();editTextBox = new TextBox(); editTextBox.Multiline = true;editTextBox.Multiline = true; editTextBox.Dock = DockStyle.editTextBox.Dock = DockStyle.FillFill;; mdiChild.Controls.Add(editTextBox);mdiChild.Controls.Add(editTextBox); mdiChild.Show();mdiChild.Show(); count++;count++;}}

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 1111

8. Test the New Item 8. Test the New Item

click click

editTextBox.Dock = editTextBox.Dock = DockStyle.Fill;DockStyle.Fill;

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 1212

9. The Cut Item 9. The Cut Item private void cutToolStripButton_Click(

object sender, EventArgs e){ Form activeChildForm =

this.ActiveMdiChild;

if (activeChildForm != null) { TextBox activeTextBox = (TextBox)

activeChildForm.ActiveControl; if (activeTextBox != null) activeTextBox.Cut(); }}

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 1313

10. The Copy Item 10. The Copy Item private void copyToolStripButton_Click(

object sender, EventArgs e){ Form activeChildForm =

this.ActiveMdiChild;

if (activeChildForm != null) { TextBox activeTextBox = (TextBox)

activeChildForm.ActiveControl; if (activeTextBox != null) activeTextBox.Copy(); }}

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 1414

11. The Paste Item 11. The Paste Item private void pasteToolStripButton_Click(

object sender, EventArgs e){ Form activeChildForm =

this.ActiveMdiChild;

if (activeChildForm != null) { TextBox activeTextBox = (TextBox)

activeChildForm.ActiveControl; if (activeTextBox != null) activeTextBox.Paste(); }}

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 1515

12. Test the Cut, Copy and Paste Items 12. Test the Cut, Copy and Paste Items

click click

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 1616

13. The Save Item 13. The Save Item private void saveToolStripButton_Click(object sender, EventArgs e)

{ SaveFileDialog sfd =

new SaveFileDialog();

sfd.Title = "Save a Text File"; sfd.Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*";

DialogResult dr = sfd.ShowDialog(); if (dr == DialogResult.OK) {

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 1717

System.IO.StreamWriter sw = new System.IO.StreamWriter(sfd.FileName);

Form activeChildForm = this.ActiveMdiChild;

if (activeChildForm != null) { TextBox activeTextBox = (TextBox)

activeChildForm.ActiveControl; if (activeTextBox != null) sw.Write(activeTextBox.Text);

sw.Close(); } }}

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 1818

13. The Open Item 13. The Open Item private void openToolStripButton_Click(object sender, EventArgs e)

{ OpenFileDialog ofd =

new OpenFileDialog();

ofd.Title = "Open a Text File"; ofd.Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*";

DialogResult dr = ofd.ShowDialog(); if (dr == DialogResult.OK) {

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 1919

System.IO.StreamReader sr = new System.IO.StreamReader(ofd.FileName);

Form activeChildForm = this.ActiveMdiChild;

if (activeChildForm != null) { TextBox activeTextBox = (TextBox)

activeChildForm.ActiveControl; if (activeTextBox != null) activeTextBox.Text =

sr.ReadToEnd(); sr.Close(); } }}

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 2020

14. The Print Item 14. The Print Item private void printToolStripButton_Click(object sender, EventArgs e)

{ PrintDialog pd = new PrintDialog(); pd.ShowDialog();}

// Display Print dialog only// Cannot print

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 2121

Add Other Controls to ToolStrip Add Other Controls to ToolStrip

We can add more controls to the ToolStrip We can add more controls to the ToolStrip such as such as – ButtonButton– LabelLabel– Split ButtonSplit Button– DropDownButtonDropDownButton– SeparatorSeparator– ComboBoxComboBox– TextBoxTextBox– ProgressBarProgressBar

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 2222

Available Controls for ToolStripAvailable Controls for ToolStrip

Dr Dat Tran - Week 4 Lecture NotesDr Dat Tran - Week 4 Lecture Notes 2323

Add Split Button and Its Sub-ItemsAdd Split Button and Its Sub-Items

Change image for the split buttonChange image for the split button