23
Dr Dat Tran - Week 4 Lectur Dr Dat Tran - Week 4 Lectur e Notes e Notes 1 MenuStrip MenuStrip 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 MenuStrip Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences &

Embed Size (px)

Citation preview

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

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

MenuStripMenuStrip

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

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

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

MenuStripMenuStripThe MenuStrip control is new to VS.NET 2005.The MenuStrip control is new to VS.NET 2005.Use the MenuStrip control to easily create menus Use the MenuStrip control to easily create menus like those found in Microsoft Office.like those found in Microsoft Office.The MenuStrip control supports the multiple-The MenuStrip control supports the multiple-document interface (MDI) and menu merging, tool document interface (MDI) and menu merging, tool tips, and overflow. tips, and overflow. You can enhance the usability and readability of You can enhance the usability and readability of your menus by adding access keys, shortcut keys, your menus by adding access keys, shortcut keys, check marks, images, and separator bars.check marks, images, and separator bars.The MenuStrip control replaces and adds The MenuStrip control replaces and adds functionality to the MainMenu controlfunctionality to the MainMenu control

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

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 MenuStrip control We use an MDI form and a MenuStrip control to build a simple text editor.to build a simple text editor.The MenuStrip control contains standard The MenuStrip control contains standard menu items such as File, Edit, Tools, and menu items such as File, Edit, Tools, and Help, and sub-menu items such as New, Help, and sub-menu items such as New, Open, Save, Cut, Copy, Paste, Undo, Redo, Open, Save, Cut, Copy, Paste, Undo, Redo, and Printand PrintImplementation code is also added to Implementation code is also added to implement some standard itemsimplement some standard items

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

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

1. Add MenuStrip to Form1. Add MenuStrip to Form

drag & dropdrag & drop

rename itrename it

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

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

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

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

2. Some Properties2. Some Properties

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

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

3. Change Render Mode3. Change Render Mode

click click

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

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

4. Create Event Handlers4. Create Event Handlers

double-clickdouble-click

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

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

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

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

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

6. Add Implementation Code 6. Add Implementation Code

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

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

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

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

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++;}}

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

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

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

click click

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

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

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

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(); }}

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

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

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(); }}

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

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

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(); }}

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

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

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

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

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

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) {

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

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

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(); } }}

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

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

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) {

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

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

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(); } }}

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

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

14. The Undo Item 14. The Undo Item private void undoToolStripButton_Click(

object sender, EventArgs e){ Form activeChildForm =

this.ActiveMdiChild;

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

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

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

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

Add Other Items to MenuStrip Add Other Items to MenuStrip

We can add more menu items and sub-menu We can add more menu items and sub-menu items to the MenuStrip, for exampleitems to the MenuStrip, for example

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

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

Add ImagesAdd Images(C:\Program Files\Microsoft Visual Studio 8\Common7\VS2005ImageLibrary)(C:\Program Files\Microsoft Visual Studio 8\Common7\VS2005ImageLibrary)

Add imageAdd image