Click here to Skip to main content
15,884,472 members
Articles / Programming Languages / C#
Article

Notepad Application in C#

Rate me:
Please Sign up or sign in to vote.
3.56/5 (24 votes)
6 Jun 20042 min read 143.4K   8.1K   24   12
An article on C# describing a notepad application

Image 1

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

C#
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.

C#
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.

C#
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


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to use AutoHotkey for Debug through DBGp Pin
201199927-Sep-20 2:38
201199927-Sep-20 2:38 
Questionnotepad code in c# Pin
prasad60621-Jul-15 20:30
prasad60621-Jul-15 20:30 
GeneralReadToEnd() Pin
vince_204816-Apr-09 4:01
vince_204816-Apr-09 4:01 
Generalproblem Pin
James_Zhang16-Dec-08 14:11
James_Zhang16-Dec-08 14:11 
GeneralPrint does not work Pin
jpma5-Aug-08 10:16
jpma5-Aug-08 10:16 
Generala more codes Pin
tinred16-Aug-06 23:11
tinred16-Aug-06 23:11 
GeneralVB.net problem Pin
sidda15-Jan-06 7:29
sidda15-Jan-06 7:29 
I want to create the following application.

1. If a click on OPen button it should open a "Open Dialog Box" with .shp,.pfe, .txt file extensions. I did this.

2. If I select .txt as file extension and select a .txt file it should open in Note pad.
Please help me out as this is urgent

sidda
GeneralRe: VB.net problem Pin
bindukrishna2-Nov-07 20:35
bindukrishna2-Nov-07 20:35 
GeneralRe: VB.net problem Pin
prasad60621-Jul-15 20:53
prasad60621-Jul-15 20:53 
GeneralCopy Paste Problem Pin
godfathers17-Mar-05 23:00
godfathers17-Mar-05 23:00 
Generaltry/finally Pin
Marek_J7-Jun-04 6:44
Marek_J7-Jun-04 6:44 
QuestionWho are you? Pin
Uwe Keim6-Jun-04 23:55
sitebuilderUwe Keim6-Jun-04 23:55 

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

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