Download presentation
Presentation is loading. Please wait.
Published byFelicia Rose Modified over 9 years ago
1
Dr Dat Tran - Week 4 Lecture Notes 1 ToolStrip Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering
2
Dr Dat Tran - Week 4 Lecture Notes 2 ToolStrip ToolStrip class is a ToolStrip class is abstract base class for MenuStrip, StatusStrip, and ContextMenuStrip ToolStrip control provides many members that manage painting, mouse and keyboard input, and drag-and-drop 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 containers and contained items, in the same way you handle events for other controls. You can drag items from one ToolStrip to another or within a ToolStrip
3
Dr Dat Tran - Week 4 Lecture Notes 3 A Simple Text Editor We use an MDI form and a ToolStrip control to build a simple text editor. The ToolStrip control contains standard items such as New, Open, Save, Cut, Copy, Paste, and Print Implementation code is also added to implement the standard items
4
Dr Dat Tran - Week 4 Lecture Notes 4 1. Add ToolStrip 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 3. Change Render Mode click
7
Dr Dat Tran - Week 4 Lecture Notes 7 4. Create Event Handlers double-click each item
8
Dr Dat Tran - Week 4 Lecture Notes 8 5. Change the Form to MDI Form
9
Dr Dat Tran - Week 4 Lecture Notes 9 6. Add Implementation Code int count; Form mdiChild; TextBox editTextBox; public MyNotePad() { InitializeComponent(); InitializeComponent(); count = 1; count = 1;}
10
Dr Dat Tran - Week 4 Lecture Notes 10 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++;}
11
Dr Dat Tran - Week 4 Lecture Notes 11 8. Test the New Item click editTextBox.Dock = DockStyle.Fill; DockStyle.Fill;
12
Dr Dat Tran - Week 4 Lecture Notes 12 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(); }
13
Dr Dat Tran - Week 4 Lecture Notes 13 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(); }
14
Dr Dat Tran - Week 4 Lecture Notes 14 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(); }
15
Dr Dat Tran - Week 4 Lecture Notes 15 12. Test the Cut, Copy and Paste Items click
16
Dr Dat Tran - Week 4 Lecture Notes 16 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) {
17
Dr Dat Tran - Week 4 Lecture Notes 17 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(); }
18
Dr Dat Tran - Week 4 Lecture Notes 18 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) {
19
Dr Dat Tran - Week 4 Lecture Notes 19 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(); }
20
Dr Dat Tran - Week 4 Lecture Notes 20 14. The Print Item private void printToolStripButton_Click(object sender, EventArgs e) { PrintDialog pd = new PrintDialog(); pd.ShowDialog(); } // Display Print dialog only // Cannot print
21
Dr Dat Tran - Week 4 Lecture Notes 21 Add Other Controls to ToolStrip We can add more controls to the ToolStrip such as –Button –Label –Split Button –DropDownButton –Separator –ComboBox –TextBox –ProgressBar
22
Dr Dat Tran - Week 4 Lecture Notes 22 Available Controls for ToolStrip
23
Dr Dat Tran - Week 4 Lecture Notes 23 Add Split Button and Its Sub-Items Change image for the split button
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.