Dr Dat Tran - Week 4 Lecture Notes 1 ToolStrip Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences & Engineering
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
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
Dr Dat Tran - Week 4 Lecture Notes 4 1. Add ToolStrip to Form drag & drop rename it
Dr Dat Tran - Week 4 Lecture Notes 5 2. Insert Standard Items click click
Dr Dat Tran - Week 4 Lecture Notes 6 3. Change Render Mode click
Dr Dat Tran - Week 4 Lecture Notes 7 4. Create Event Handlers double-click each item
Dr Dat Tran - Week 4 Lecture Notes 8 5. Change the Form to MDI Form
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;}
Dr Dat Tran - Week 4 Lecture Notes 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++;}
Dr Dat Tran - Week 4 Lecture Notes Test the New Item click editTextBox.Dock = DockStyle.Fill; DockStyle.Fill;
Dr Dat Tran - Week 4 Lecture Notes 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 Notes 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 Notes 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 Notes Test the Cut, Copy and Paste Items click
Dr Dat Tran - Week 4 Lecture Notes 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 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(); }
Dr Dat Tran - Week 4 Lecture Notes 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 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(); }
Dr Dat Tran - Week 4 Lecture Notes 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 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
Dr Dat Tran - Week 4 Lecture Notes 22 Available Controls for ToolStrip
Dr Dat Tran - Week 4 Lecture Notes 23 Add Split Button and Its Sub-Items Change image for the split button