Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Dr Dat Tran - Week 4 Lecture Notes 1 MenuStrip Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences &"— Presentation transcript:

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

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

3 Dr Dat Tran - Week 4 Lecture Notes 3 A Simple Text Editor We use an MDI form and a MenuStrip control to build a simple text editor. The MenuStrip control contains standard menu items such as File, Edit, Tools, and Help, and sub-menu items such as New, Open, Save, Cut, Copy, Paste, Undo, Redo, and Print Implementation code is also added to implement some standard items

4 Dr Dat Tran - Week 4 Lecture Notes 4 1. Add MenuStrip to Form drag & drop rename it

5 Dr Dat Tran - Week 4 Lecture Notes 5 2. Insert Standard Items click click

6 Dr Dat Tran - Week 4 Lecture Notes 6 2. Some Properties

7 Dr Dat Tran - Week 4 Lecture Notes 7 3. Change Render Mode click

8 Dr Dat Tran - Week 4 Lecture Notes 8 4. Create Event Handlers double-click

9 Dr Dat Tran - Week 4 Lecture Notes 9 5. Change the Form to MDI Form

10 Dr Dat Tran - Week 4 Lecture Notes 10 6. Add Implementation Code int count; Form mdiChild; TextBox editTextBox; public MainForm() { InitializeComponent(); InitializeComponent(); count = 1; count = 1;}

11 Dr Dat Tran - Week 4 Lecture Notes 11 7. The New Item private void newToolStripButton_Click(object sender, EventArgs e) { mdiChild = new Form(); mdiChild = new Form(); mdiChild.Text = "Document" + mdiChild.Text = "Document" +count.ToString(); mdiChild.MdiParent = this; mdiChild.MdiParent = this; editTextBox = new TextBox(); editTextBox = new TextBox(); editTextBox.Multiline = true; editTextBox.Multiline = true; editTextBox.Dock = DockStyle.Fill; editTextBox.Dock = DockStyle.Fill; mdiChild.Controls.Add(editTextBox); mdiChild.Controls.Add(editTextBox); mdiChild.Show(); mdiChild.Show(); count++; count++;}

12 Dr Dat Tran - Week 4 Lecture Notes 12 8. Test the New Item click editTextBox.Dock = DockStyle.Fill; DockStyle.Fill;

13 Dr Dat Tran - Week 4 Lecture Notes 13 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(); }

14 Dr Dat Tran - Week 4 Lecture Notes 14 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(); }

15 Dr Dat Tran - Week 4 Lecture Notes 15 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(); }

16 Dr Dat Tran - Week 4 Lecture Notes 16 12. Test the Cut, Copy and Paste Items

17 Dr Dat Tran - Week 4 Lecture Notes 17 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) {

18 Dr Dat Tran - Week 4 Lecture Notes 18 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(); }

19 Dr Dat Tran - Week 4 Lecture Notes 19 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) {

20 Dr Dat Tran - Week 4 Lecture Notes 20 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(); }

21 Dr Dat Tran - Week 4 Lecture Notes 21 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(); }

22 Dr Dat Tran - Week 4 Lecture Notes 22 Add Other Items to MenuStrip We can add more menu items and sub-menu items to the MenuStrip, for example

23 Dr Dat Tran - Week 4 Lecture Notes 23 Add Images (C:\Program Files\Microsoft Visual Studio 8\Common7\VS2005ImageLibrary) Add image


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

Similar presentations


Ads by Google