How To Create A Notepad In C# Windows Form
Jebbidan
list of my created websites: https://jebbidanyt.000webhostapp.com/ my main website: https://jebbidan.editorx.io/hadsis
the code:
private string FileName = string.Empty; //put this line of code at a form level
==================================================
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
open.Title = "Open File";
open.FileName = "";
if (open.ShowDialog() == DialogResult.OK)
{
// save the opened FileName in our variable
this.FileName = open.FileName;
this.Text = string.Format("{0}", Path.GetFileNameWithoutExtension(open.FileName));
StreamReader reader = new StreamReader(open.FileName);
richTextBox1.Text = reader.ReadToEnd();
reader.Close();
}
==================================================
SaveFileDialog saving = new SaveFileDialog();
saving.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
saving.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
saving.Title = "Save As";
saving.FileName = "Untitled";
if (saving.ShowDialog() == DialogResult.OK)
{
// save the new FileName in our variable
this.FileName = saving.FileName;
StreamWriter writing = new StreamWriter(saving.FileName);
writing.Write(richTextBox1.Text);
writing.Close();
}
==================================================
if (string.IsNullOrEmpty(this.FileName)) { // call SaveAs saveAsToolStripMenuItem_Click(sender, e); } else { // we already have the filename. we overwrite that file StreamWriter writer = new StreamWriter(this.FileName); writer.Write(richTextBox1.Text); writer.Close(); } //note: don't copy the "==================================================". also, //make sure you follow closely to the video's instruction when you want to copy all of the code //in this description ... https://www.youtube.com/watch?v=7PFwFI5-hts
31062915 Bytes