You are on page 1of 3

using System;

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

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string currFileName = "";
string mySafeCurrFileName = "";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "Untitled - NotePad";
}
private void neToolStripMenuItem_Click(object sender, EventArgs e)
{

private void txtMainEdit_TextChanged(object sender, EventArgs e)


{
if (txtMainEdit.Modified)
{
if (currFileName != "")
this.Text = "*" + mySafeCurrFileName + " - NotePad";
else
this.Text = "*Untitled - NotePad";
}
}

private void cmdSave_Click(object sender, EventArgs e)


{
if(currFileName == "")
{
SaveFileDialog f = new SaveFileDialog();
f.ShowDialog();
if(f.FileName != "")
{
currFileName = f.FileName;
SaveToFile(currFileName);
}
} else
{
SaveToFile(currFileName);
}
this.Text = Path.GetFileNameWithoutExtension(currFileName) + " -
NotePad";
}

private void SaveToFile(string currFileName)


{
StreamWriter sw = new StreamWriter(currFileName);
sw.Write(txtMainEdit.Text);
sw.Close();
}

private void cmdOpen_Click(object sender, EventArgs e)


{
if (txtMainEdit.Modified)
{
if (MessageBox.Show("Do you want to save this NotePad",
"MyNotePad", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
System.Windows.Forms.DialogResult.Yes)
{
cmdSave_Click(null, null);
}
}
OpenFileDialog f = new OpenFileDialog();
f.Filter = "Text Document|*.txt";
f.ShowDialog();
if(f.FileName != "")
{
mySafeCurrFileName = f.SafeFileName;
currFileName = f.FileName;
OpenFromFile(currFileName);
this.Text = mySafeCurrFileName + " - NotePad";
}
}

private void OpenFromFile(string currFileName)


{
StreamReader sr = new StreamReader(currFileName);
txtMainEdit.Text = "";
while(!sr.EndOfStream)
{
txtMainEdit.Text += sr.ReadLine() + "\r\n";
}
sr.Close();
}

private void cmdFont_Click(object sender, EventArgs e)


{
FontDialog f = new FontDialog();
f.Font = txtMainEdit.Font;
f.ShowDialog();
txtMainEdit.Font = f.Font;
}

private void toolStrip1_ItemClicked(object sender,


ToolStripItemClickedEventArgs e)
{

}
private void menuStrip1_ItemClicked(object sender,
ToolStripItemClickedEventArgs e)
{

private void cmdExit_Click(object sender, EventArgs e)


{
Application.Exit();
}

private void cmdSaveAs_Click(object sender, EventArgs e)


{
SaveFileDialog f = new SaveFileDialog();
f.ShowDialog();
if (f.FileName != "")
{
currFileName = f.FileName;
SaveToFile(currFileName);
}

private void cmdNew_Click(object sender, EventArgs e)


{
if (txtMainEdit.Modified)
{
if (MessageBox.Show("Do you want to save this NotePad",
"MyNotePad", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
System.Windows.Forms.DialogResult.Yes)
{
cmdSave_Click(null, null);
}
}
currFileName = "";
txtMainEdit.Text = "";
this.Text = "Untitled - NotePad";
}

private void worldToolStripMenuItem_Click(object sender, EventArgs e)


{

}
}

You might also like