Example with MenuStrip:-



Add a new Form i.e.,FormMenu to the Windows Application WABasics

Create one textbox and one MenuStrip ,one ColorDialog,one FontDialog and one SaveFileDialog,one OpenFileDialog& set the following properties to them

[menuStrip1,colorDialog1,fontDialog1,fontDIalog1,saveFileDialog1,openFileDialog1 will only appear in Component tray]

textBox1:
     Name     :         txtSample


menuStrip1:-

Name          :         menuStrip1
         
ToolStripMenuItem s Name s

1.File          :         mnuFile

          1)Open       :         smnuopen
          2)Save        :         smnuSave
          3)Exit         :         smnuExit

2.Format     :         mnuFormat

          1)Font         :         smnuFont
          2)Color       :         smnuColor

                   1]ForeColor          :         smnuForeColor
                   2]BackColor         :         smnuBackColor  


Design :-



Generate the Event Handlers for smnuOpen,smnuSave,smnuExit,smnuFont ,smnuForeColor,smnuBackColor Click Events
& write the following code in Source[FormMenu.cs] 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WABasics
{
    public partial class FormMenu : Form
    {
        public FormMenu()
        {
            InitializeComponent();
        }

        private void smnuOpen_Click(object sender, EventArgs e)
        {
          openFileDialog1.Filter="Text Files|*.txt|Word Files|*.doc|Excel Files|*.xls|All Files|*.*";
          openFileDialog1.ShowDialog();
          //txtSample.Text = openFileDialog1.FileName;
          string s = openFileDialog1.FileName;
          StreamReader obj1 = new StreamReader(s);
          txtSample.Text = obj1.ReadToEnd();
        }

        private void smnuSave_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
           // txtSample.Text = saveFileDialog1.FileName;
            string s = saveFileDialog1.FileName;
            StreamWriter obj1 = new StreamWriter(s);
            obj1.WriteLine(txtSample.Text);
            obj1.Flush();

        }

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

        private void smnuFont_Click(object sender, EventArgs e)
        {
            fontDialog1.ShowDialog();
            txtSample.Font = fontDialog1.Font;
        }

        private void smnuForeColor_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            txtSample.ForeColor = colorDialog1.Color;
        }

        private void smnuBackColor_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            txtSample.BackColor = colorDialog1.Color;
        }

        private void mnuFile_Click(object sender, EventArgs e)
        {

        }

       
    }
}



Result :-
Run the application & Enter any data in textbox
Use Open,Save Exit,Font,ForeColor,BackColor options & Check


No comments:

Post a Comment