Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Notepad Application in C#

0.00/5 (No votes)
6 Jun 2004 1  
An article on C# describing a notepad application

Introduction

Beginners often find some problems in understanding the fundamental controls of any new language. This article is my first attempt to make a few controls and application development in the C# environment a little more familiar.

As I am new to writing articles kindly post me comments on any mistakes committed during this presentation and ignore any grammatical/spelling mistakes as I am not to be blamed for that. (a bad try to escape)

I have used the tools OpenFileDialog, SaveFileDialog, PageSetupDialog, PrintDialog, PrintPreviewDialog, FontDialog, etc; for the functions of opening a file, saving a file, page setup, printing the document, print preview of the document and font selection. The client area is a multi line edit box with vertical scroll provided by default.

Using the code

The root namespace which is used very often for describing and including various controls is System.Windows.Forms.

I have declared a variable "dirty" of the Boolean (bool keyword) type. This identifies any change in the client area by any user event. This variable is by default set to false and is made true only when there is a change occurring in the editable multi line text box which is identified in the function

private void txt_TextChanged(object sender, System.EventArgs e)

The code for opening a file using OpenFileDialog is given below. ofd is an object of the OpenFileDialog class. This function makes use of the StreamReader class object to read the text input.

private void mopen_Click(object sender, System.EventArgs e)
{
  ofd.Multiselect = false;
  ofd.Filter = "Text Files|*.txt";
  ofd.ShowDialog();
  if (File.Exists(ofd.FileName))
  {
    fname = ofd.FileName;
    StreamReader sr = new StreamReader(fname);
    txt.Text=sr.ReadToEnd();
    dirty = false;
    sr.Close();
  } 
}

The code for saving a file is given below. sfd is an object of the SaveFileDialog class. This function makes use of the StreamWriter class object to read the contents and write to the file to be saved.

"StreamWriter sw = new StreamWriter(fname);" passes the file name as a variable to the constructor of StreamWriter class to initialize the file to be written using the StreamWriter object.

private void savedata()
{
  if (fname == "")
  { 
    sfd.Filter = "Text Files|*.txt";
    DialogResult res = sfd.ShowDialog();
    if (res == DialogResult.Cancel)
    {
      return; 
    }
    fname = sfd.FileName;
    MessageBox.Show(fname);
  }
  StreamWriter sw = new StreamWriter(fname);
  sw.WriteLine(txt.Text);
  sw.Flush();
  sw.Close();
  dirty = false;
}

Points of Interest

I don't say that this is a very good idea, but this is the demonstration, and this may be enhanced. Modifications/good ideas are welcomed with regards.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here