0% found this document useful (0 votes)
40 views

Notepad Coding

This document contains C# code for a basic notepad application. It includes code for opening, saving, and editing text files. Methods are defined to handle menu item clicks for common editing functions like cut, copy, paste, undo, and changing font/color. Strings are used to track cut/copied text. An undo stack tracks previous actions.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Notepad Coding

This document contains C# code for a basic notepad application. It includes code for opening, saving, and editing text files. Methods are defined to handle menu item clicks for common editing functions like cut, copy, paste, undo, and changing font/color. Strings are used to track cut/copied text. An undo stack tracks previous actions.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NotepadApplication
{
public partial class Form1 : Form
{

String cutText = "";


String copyText = "";

public Form1()
{
InitializeComponent();
}

Stack<Action> undoAction = new Stack<Action>();

private void editToolStripMenuItem_Click(object sender, EventArgs e)


{

private void savesToolStripMenuItem_Click(object sender, EventArgs e)


{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Text file|*.txt";
sfd.InitialDirectory = "C:/";
sfd.ShowDialog();

if(sfd.FileName != "")
{
System.IO.FileStream fs = (System.IO.FileStream) sfd.OpenFile();
byte[] filetext = Encoding.ASCII.GetBytes(TextArea.Text);
fs.Write(filetext,0,filetext.Length);
fs.Close();
}

private void richTextBox1_TextChanged(object sender, EventArgs e)


{

private void fileToolStripMenuItem1_Click(object sender, EventArgs e)


{

private void quitToolStripMenuItem_Click(object sender, EventArgs e)


{
Application.Exit();
}

private void toolStripTextBox1_Click(object sender, EventArgs e)


{

private void fileToolStripMenuItem2_Click(object sender, EventArgs e)


{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = "C:/Desktop";
ofd.Filter = "Text File|*txt";
ofd.ShowDialog();

if (ofd.FileName != "")
{
String[] fileLines = System.IO.File.ReadAllLines(ofd.FileName);
TextArea.Text = "";

for (int a = 0; a < fileLines.Length; a++)


{
TextArea.AppendText(fileLines[a]);
TextArea.AppendText("\n");
}

}
}

private void fieToolStripMenuItem_Click(object sender, EventArgs e)


{
Form1 f = new Form1();
f.ShowDialog();
}

private void cutToolStripMenuItem_Click(object sender, EventArgs e)


{
cutText = TextArea.Text;
copyText = "";
TextArea.SelectedText = "";
}

private void copyToolStripMenuItem_Click(object sender, EventArgs e)


{
copyText = TextArea.SelectedText;
cutText = "";
}

private void undoToolStripMenuItem_Click(object sender, EventArgs e)


{
if (undoAction.Count > 0)
{
Action cmd = undoAction.Pop();

}
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
if (cutText.Length > 0)
{

TextArea.Text = TextArea.Text.Insert(TextArea.SelectionStart,
cutText);
}
else
{
TextArea.Text = TextArea.Text.Insert(TextArea.SelectionStart,
copyText);
}

private void changeFontToolStripMenuItem_Click(object sender, EventArgs e)


{
if (fontDialog1.ShowDialog() == DialogResult.OK &&
TextArea.SelectedText.Length > 0)
{
TextArea.SelectionFont = fontDialog1.Font;
}

private void changeColorToolStripMenuItem_Click(object sender, EventArgs e)


{
if (colorDialog1.ShowDialog() == DialogResult.OK &&
TextArea.SelectedText.Length > 0)
{
TextArea.SelectionColor = colorDialog1.Color;
}

}
}
}

You might also like