/* --------------------------------------------------------------------- Drop Down menus, Windows Dialogs, & Popup menus R. Kessler Created 1/22/07 --------------------------------------------------------------------- Updated 1/22/07 NOTES: 1. Make sure to set the richtext boxes context menu property to the name of your popup menu control. If you don//t, the popup menu won t show. 2. I included a folder with some icons for you to use on your menus. Right-click on a menu item and choose set image. Then navigate to the folder in your project. Be sure to select local resource on the dialog box. 3. When you first add a menustrip, tell it to add the standard items. Then customize them like I did. 4. Make sure to rename all the menu items to something that makes sense to you! */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; //add this using System.IO; namespace MenusAndDialogs { public partial class frmMain : Form { public frmMain() { InitializeComponent(); } string fileName = "C:\\Temp\\Text Document.txt"; //default private void mnuFileNew_Click(object sender, EventArgs e) { textPad.Text = ""; } //=====================File OPEN===================== private void mnuFileOpen_Click(object sender, EventArgs e) { OpenFileDialog1.Filter ="Text files(*.txt)|*.txt|All files(*.*)|*.*"; OpenFileDialog1.FilterIndex = 1; OpenFileDialog1.InitialDirectory = "c:\\My documents"; if ( OpenFileDialog1.ShowDialog() == DialogResult.OK) { fileName = OpenFileDialog1.FileName; using (StreamReader myReader = new StreamReader(fileName)) try { textPad.Text = myReader.ReadToEnd(); } catch (Exception ex) { MessageBox.Show("I cannot open that file. " + ex.Message, "System message", MessageBoxButtons.OK,MessageBoxIcon.Error); } finally { myReader.Close(); } } } //=====================File SAVE===================== private void mnuFileSave_Click(object sender, EventArgs e) { string fileName = ""; SaveFileDialog1.DefaultExt = ".txt"; SaveFileDialog1.FileName = fileName; SaveFileDialog1.Filter = "Text files(*.txt)|*.txt|All files(*.*)|*.*"; SaveFileDialog1.FilterIndex = 1; SaveFileDialog1.InitialDirectory = "c:\\My documents"; SaveFileDialog1.OverwritePrompt = true; if (SaveFileDialog1.ShowDialog() ==DialogResult.OK) { fileName = SaveFileDialog1.FileName; using (StreamWriter myWriter = new StreamWriter(fileName)) try { myWriter.Write(textPad.Text); } catch (Exception ex) { MessageBox.Show("I could not save your file. " + ex.Message, "System Message", MessageBoxButtons.OK,MessageBoxIcon.Error); } finally { myWriter.Close(); } } } private void mnuFileSaveAs_Click(object sender, EventArgs e) { SaveFileDialog1.Title = "Save File As..."; mnuFileSave.PerformClick(); } //=================Edit Menu=================== private void mnueditUndo_Click(object sender, EventArgs e) { // ---use me new code textPad.Undo(); } private void mnuEditCut_Click(object sender, EventArgs e) { //---if you have multiple text boxes textPad.Cut(); } private void mnuEditCopy_Click(object sender, EventArgs e) { textPad.Copy(); } private void mnuEditPaste_Click(object sender, EventArgs e) { textPad.Paste(); } private void mnuEditSelectAll_Click(object sender, EventArgs e) { textPad.SelectAll(); } //======================== pop up menu events===================== private void mnuPopupUndo_Click(object sender, EventArgs e) { mnueditUndo.PerformClick(); } private void mnuPopupCopy_Click(object sender, EventArgs e) { mnuEditCopy.PerformClick(); } private void mnuPopupcut_Click(object sender, EventArgs e) { mnuEditCut.PerformClick(); } private void mnuPopupPaste_Click(object sender, EventArgs e) { mnuEditPaste.PerformClick(); } private void mnuPopupSelectAll_Click(object sender, EventArgs e) { mnuEditSelectAll.PerformClick(); } //================Format Menu=================== private void mnuFormatFont_Click(object sender, EventArgs e) { FontDialog1.ShowColor = true; if (FontDialog1.ShowDialog() == DialogResult.OK) { textPad.SelectionFont = FontDialog1.Font; textPad.SelectionColor = FontDialog1.Color; //---this will change all the text to the same color textPad.ForeColor = FontDialog1.Color; } } private void mnuFormatBackground_Click(object sender, EventArgs e) { //---show the color dialog if (ColorDialog1.ShowDialog() == DialogResult.OK) textPad.BackColor = ColorDialog1.Color; } } }