Click here to Skip to main content
Click here to Skip to main content

Notepad Application in C#

, 6 Jun 2004
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Abhishek M
Web Developer
India India
No Biography provided

Comments and Discussions

 
GeneralReadToEnd() Pinmembervince_204816-Apr-09 5:01 
Generalproblem Pinmembercheng_j_zhang16-Dec-08 15:11 
GeneralPrint does not work Pinmemberjpma5-Aug-08 11:16 
Generala more codes Pinmembertinred17-Aug-06 0:11 
GeneralVB.net problem Pinmembersidda15-Jan-06 8:29 
GeneralRe: VB.net problem Pinmemberbindukrishna2-Nov-07 21:35 
GeneralCopy Paste Problem Pinmembergodfathers18-Mar-05 0:00 
Generaltry/finally PinmemberMarek_J7-Jun-04 7:44 
QuestionWho are you? PinsitebuilderUwe Keim7-Jun-04 0:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 7 Jun 2004
Article Copyright 2004 by Abhishek M
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid