News
* IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. *** 
  Nov 30 2014 8:13AM     Prashanth
  1 Comments    11475 Views  
Here Prashanth provided a short tutorial with example, how to create Menu with File, Edit and Format options in Windows Forms applicaton C#. Using the Control MenuStrip we can create menus.
Here, example shows like notepad properties ( File, Edit and Format ).
Create a new windows forms application and do the steps given below to create a Menu
Adding a Controls to the Windows Form
We have to drag and drop MenuStrip, RichTextBox and FontDialog to the windows forms
  1. MenuStrip - Used to create Menu
  2. RichTextbox - To show text in form
  3. FontDialog - Used to change the font size, font and etc
Classes
  1. OpenFileDialog - Class is used to open files
  2. SaveFileDialog - Class is used to Save files
The image given below shows the control, which we have to drag and drop to the windows forms application C#







After placing the all the controls, Below your form it displays like this.
Form Design
Design a menu as shown below,

Menu Design
  1. File -> Open, Save, Save As and Exit
  2. Edit -> Cut, Copy, Paste and Select All
  3. Format -> Font
  4. Help
Creating Shortcuts
As you know the shortcut keys for open, save, cut, copy, paste and etc,. Likewise we can create shortcut keys for Menustrip. Right click the menu item and click properties, select Shortcut key from Misc tab (Image given below shows)
Select the shortcut keys you want for your menu items and move to next item. After completing the shortkey properties of all the menu items, the menu should look like this
To create a Menu in C#
Below is the sample code for creating a Menu with File, Edit and Format options in C#.
C#
Create a object for OpenFileDialog globaly.

public partial class Menu : Form
{
        OpenFileDialog file_open = new OpenFileDialog();
        public Menu()
        {
            InitializeComponent();
        }
}

To Open a file (.txt and .rtf)

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
            // To filter files from OpenFileDialog
            file_open.Filter = "Rich Text File (*.rtf)|*.rtf| Plain Text File (*.txt)|*.txt";
            file_open.FilterIndex = 1;
            file_open.Title = "Open text or RTF file";
           
            RichTextBoxStreamType stream_type;
            stream_type = RichTextBoxStreamType.RichText;
            if (DialogResult.OK == file_open.ShowDialog())
            {                
                if (string.IsNullOrEmpty(file_open.FileName))
                    return;
                if (file_open.FilterIndex == 2)
                    stream_type = RichTextBoxStreamType.PlainText;                
                richText.LoadFile(file_open.FileName, stream_type);
            }
}

To Save a existing file

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{            
            SaveFileDialog saveDlg = new SaveFileDialog();
            string filename = "";

            // To filter files from SaveFileDialog
            saveDlg.Filter = "Rich Text File (*.rtf)|*.rtf|Plain Text File (*.txt)|*.txt";                                          
            saveDlg.DefaultExt = "*.rtf";
            saveDlg.FilterIndex = 1;
            saveDlg.Title = "Save the contents";

            filename = file_open.FileName;
            
            RichTextBoxStreamType stream_type;
            // Checks the extension of the file to save
            if (filename.Contains(".txt"))
                stream_type = RichTextBoxStreamType.PlainText;
            else
                stream_type = RichTextBoxStreamType.RichText;
                    
            richText.SaveFile(filename, stream_type);
}

To save a new file [ Save As..]

private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{            
            SaveFileDialog saveDlg = new SaveFileDialog();
            string filename = "";

            // To filter files from SaveFileDialog
            saveDlg.Filter = "Rich Text File (*.rtf)|*.rtf|Plain Text File (*.txt)|*.txt"
            saveDlg.DefaultExt = "*.rtf";
            saveDlg.FilterIndex = 1;
            saveDlg.Title = "Save the contents";
            
            DialogResult retval = saveDlg.ShowDialog();
            if (retval == DialogResult.OK)
                filename = saveDlg.FileName;
            else
                return;
           
            RichTextBoxStreamType stream_type;
            if (saveDlg.FilterIndex == 2)
                stream_type = RichTextBoxStreamType.PlainText;
            else
                stream_type = RichTextBoxStreamType.RichText;
              
            richText.SaveFile(filename, stream_type);
            MessageBox.Show("File Saved");
}

To Exit

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
            Application.Exit();
}

For Edit Options like Cut, Copy, Paste and Select All

private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
            if (richText.SelectionLength > 0)
                richText.Cut();  
}

 private void copyToolStripMenuItem_Click(object sender, EventArgs e)
 {
            if (richText.SelectionLength > 0)
                richText.Copy();     
 }

 private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
 {
            if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
            {
                richText.Paste();
            }
 }

private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
            if (richText.TextLength > 0)
                richText.SelectAll();
}

For Font Properties

private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
            // Show the font dialog.
            DialogResult result = fontDialog1.ShowDialog();
            // Check if OK was pressed.
            if (result == DialogResult.OK)
            {
                // Get Font.
                Font font = fontDialog1.Font;
                // Set font to richtextbox properties.              
                this.richText.Font = font;
            }
}

For Help

private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
            MessageBox.Show("Options:\n\nFile Open : Press 'CTRL + O'\nFile Save : Press 'CTRL + S'\nCut : Press 'CTRL + X'\nCopy : Press 'CTRL + C'\nPaste : Press 'CTRL + V'\n");
}  

Result
I hope this page will helps to create menu with File, Edit and Format options in Windows Forms Application in C#. Thanks.
BackToTop
Comments


AnonymousUser
Aug 21 2018 5:42AM
nice

 
Search
Recent Posts
Create Amazon ElasticCache Using Memcached in CSharp
Oct 29 2018 12:09PM Posted By Amose
Get Google Map Lat Lng (Geo Point) By Pincode in C#
Oct 28 2018 12:09PM Posted By Pranav
Google URL Shortener in C#
Oct 20 2018 12:09PM Posted By Sanjay
Bind Gridview from CSV file in Asp.Net C#
Oct 5 2018 12:09PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Sep 26 2018 12:09PM Posted By John
Gridview custom CSS in ASP.Net
Sep 14 2018 12:09PM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Sep 10 2018 12:09PM Posted By Micheal
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: