Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C#, modify from MenuDialog to create a simple text editor. You need to add

ID: 3709786 • Letter: U

Question

Using C#, modify from MenuDialog to create a simple text editor. You need to add [STAThread]

Before its Main method to make it work.

In File menu, add option like New, Open, Save, Save As,Close, and Exit.

When the user selects Save As, display a SaveFileDialog to let the user to select a filename and whatever in the textbox should be saved to this file.

When the user selects Save, display a SaveFileDialog if the file has not been opened (created after the user clicked New or Close).

Both objects from OpenFileDialog and SaveFileDialog has a property called FileName. Define an instance variable of String type called filename to hold file name after an OpenFileDialog or SaveFileDialog is created. When the user clicks Save and filename is null, display SaveFileDialog.

When New and Close is selected, simply set textbox as empty string like textBox1.Text=”” and set filename as null.

Add a menu called Edit with three items like Cut, Copy, Paste

textBox1.selectedText returns the selected text from the textbox

Clipboard.SetText(textBox1.SelectedText) adds selectedText to Clipboard

Clipboard.GetText() returns what is in the clipboard.

When the user clicks cut or copy, set the clipboard with the text selected

When paste is clicked, set textBox1.Text as textBox1.Text.Substring(0, textBox1.SelectionStart) + what is in the clipboard + textBox1.Text.Substring(textBox1.SelectionStart + textBox1.SelectionLength);

When cut is clicked, it is almost same as above except “what is in the clipboard” will be empty string

Add another menu like Help with one or two option like who created the editor and version etc.

Explanation / Answer

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
public class MenuDialog : Form {

    // Create control
TextBox text = new TextBox();

public MenuDialog() {

      // Configure form
    Size = new Size(500,200);
    Text = "Menus and Dialogs";

      // Configure text box
    text.Size = new Size(450,120);
    text.Multiline = true;
    text.ScrollBars = ScrollBars.Both;
    text.WordWrap = false;
    text.Location = new Point(20,20);

      // Configure file menu
    MenuItem fileMenu = new MenuItem("File");
    MenuItem open = new MenuItem("Open");
        open.Shortcut = Shortcut.CtrlO;
    MenuItem save = new MenuItem("Save");
        save.Shortcut = Shortcut.CtrlS;
    MenuItem saveAs = new MenuItem("Save as");
        MenuItem New = new MenuItem("New");
        New.Shortcut = Shortcut.CtrlN;
    MenuItem close = new MenuItem("Close");
    MenuItem exit = new MenuItem("Exit");
  
    fileMenu.MenuItems.Add(New);
    fileMenu.MenuItems.Add(open);
    fileMenu.MenuItems.Add(save);
    fileMenu.MenuItems.Add(saveAs);
    fileMenu.MenuItems.Add(close);
    fileMenu.MenuItems.Add(exit);

        // Configure feedback menu
        MenuItem editMenu = new MenuItem("Edit");
    MenuItem cut = new MenuItem("Cut");
    cut.Shortcut = Shortcut.CtrlM;
    MenuItem copy = new MenuItem("Copy");
        copy.Shortcut = Shortcut.CtrlC;
    MenuItem paste = new MenuItem("Paste");
        paste.Shortcut = Shortcut.CtrlV;
    editMenu.MenuItems.Add(cut);
    editMenu.MenuItems.Add(copy);
    editMenu.MenuItems.Add(paste);

      // Configure format menu
    MenuItem formatMenu = new MenuItem("Format");
    MenuItem font = new MenuItem("Font");
    font.Shortcut = Shortcut.CtrlF;
    formatMenu.MenuItems.Add(font);
   
      // Configure main menu
    MainMenu bar = new MainMenu();
    Menu = bar;
    bar.MenuItems.Add(fileMenu);
    bar.MenuItems.Add(editMenu);
    bar.MenuItems.Add(formatMenu);

      // Add control to form
    Controls.Add(text);

      // Register event handlers
    open.Click += new EventHandler(Open_Click);
    save.Click += new EventHandler(Save_Click);
   // saveAs.Click += new EventHandler(saveAs_Click);
// New.Click += new EventHandler(New_Click);
    close.Click += new EventHandler(close_Click);
    //exit.Click += new EventHandler(exit_Click);
   // cut.Click += new EventHandler(Cut_Click);
    //copy.Click += new EventHandler(copy_Click);
    //paste.Click += new EventHandler(paste_Click);
    font.Click += new EventHandler(Font_Click);
}

    // Handle open menu item
protected void Open_Click(Object sender, EventArgs e) {
    OpenFileDialog o = new OpenFileDialog();
    if(o.ShowDialog() == DialogResult.OK) {
      Stream file = o.OpenFile();
      StreamReader reader = new StreamReader(file);
      char[] data = new char[file.Length];
      reader.ReadBlock(data,0,(int)file.Length);
      text.Text = new String(data);
      reader.Close();
    }
}
    //Handle close menu item
    protected void close_Click(object sender, EventArgs e)
    {
       // textBox1.Text = "";
    }

    // Handle save menu item
protected void Save_Click(Object sender, EventArgs e) {
    SaveFileDialog s = new SaveFileDialog();
    if(s.ShowDialog() == DialogResult.OK) {
      StreamWriter writer = new StreamWriter(s.OpenFile());
      writer.Write(text.Text);
      writer.Close();
    }
}

    // Handle message menu
protected void Cut_Click(Object sender, EventArgs e) {
    MessageBox.Show
                ("You clicked the Message menu", "My message");
}

    // Handle font menu
protected void Font_Click(Object sender, EventArgs e) {
    FontDialog f = new FontDialog();
    if(f.ShowDialog() == DialogResult.OK)
      text.Font = f.Font;
}
    [STAThread]
public static void Main() {
    Application.Run(new MenuDialog());
}

    private void InitializeComponent()
    {
            this.SuspendLayout();
            //
            // MenuDialog
            //
            this.ClientSize = new System.Drawing.Size(285, 253);
            this.Name = "MenuDialog";
            this.ResumeLayout(false);

    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote