Jump to content




Recent Status Updates

  • Photo
      30 Sep
    rhossis

    laptop hard disk seated beneath motherboard but with no access panel. 7 hours to replace :(

    Show comments (3)
  • Photo
      19 Sep
    Chall

    I love it when you go to write a help thread, then while writing, you reach an enlightenment, and figure it out yourself.

    Show comments (3)
View All Updates

Developed by Kemal Taskin
Photo
* * * * * 1 votes

A Simple Notepad In C#

streamwriter

  • Please log in to reply
35 replies to this topic

#1 Kierien

Kierien

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 30 January 2009 - 09:10 PM

Harlo,

This guide wont teach you to code some awesome notepad but just a simple one..Im going to try to keep the code as simple as possible.

Aite, your gonna need to open a new C# project by opening Microsoft Visual C#, then goto

File>New>Windows Form Application

Name the project Notepad or whatever you want.

Add the following items. (Menu Strip, openFileDialog, saveFileDialog, Text Box)

Posted Image

Now set the textBox's dock to fill, then rename it to txtMain.

Posted Image

Then click one time on your textBox, you should see a small play arrow, click it and check "Multiline".

Posted Image

You should now have something that looks like this.

Posted Image

Now right click on a blank part of the menu strip, and press "Insert Standard Items"

Posted Image

You should have a bunch of basic functions by now, but its not that easy =P, they all dont have code.

But start off by deleting "Tools" and "Help".

Now in your form editor click on the "File" button, then double click on "New", which should bring you to the code editor, then write the code

txtMain.Clear();

Then goto "Open"'s code, and add this code

//Shows the openFileDialog
openFileDialog1.ShowDialog();
//Reads the text file
System.IO.StreamReader OpenFile = new System.IO.StreamReader(openFileDialog1.FileName);
//Displays the text file in the textBox
txtMain.Text = OpenFile.ReadToEnd();
//Closes the proccess
OpenFile.Close();

Now proceed to "Save"'s code, and add this code

//Determines the text file to save to
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(openFileDialog1.FileName);
//Writes the text to the file
SaveFile.WriteLine(txtMain.Text);
//Closes the proccess
SaveFile.Close();

Then for "Save as"'s code, add

//Open the saveFileDialog
saveFileDialog1.ShowDialog();
//Determines the text file to save to
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(saveFileDialog1.FileName);
//Writes the text to the file
SaveFile.WriteLine(txtMain.Text);
//Closes the proccess
SaveFile.Close();

"Print"'s code is

//Declare prntDoc as a new PrintDocument
System.Drawing.Printing.PrintDocument prntDoc = new System.Drawing.Printing.PrintDocument();

"Print Preview" is

//Declare preview as a new PrintPreviewDialog
PrintPreviewDialog preview = new PrintPreviewDialog();
//Declare prntDoc_PrintPage as a new EventHandler for prntDoc's Print Page
prntDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(prntDoc_PrintPage);
//Set the PrintPreview's Document equal to prntDoc
preview.Document = prntDoc;
//Show the PrintPreview Dialog
if (preview.ShowDialog(this) == DialogResult.OK)
{
//Generate the PrintPreview
prntDoc.Print();
}

"Exit"'s code is

Application.Exit();

Move on to "Undo"'s code, add

txtMain.Undo();

For "Redo"'s code its

txtMain.Undo();

Add this to "Cut"'s code

txtMain.Cut();

For "Copy" add

txtMain.Copy();

"Paste"'s code is

txtMain.Paste();

For "Select All" its

txtMain.SelectAll();

Ur done with most of the code, but our Notepad still lacks one function - Word Wrap!!

Quote from Xav:

I disagree with the last bit (the word wrap). It would be much better to use a single checked box to wrap text.

To achieve this, set the menu item's CheckOnClick boolean property to True, and then use the following code in the Click event:

txtMain.WordWrap = wordWrapMenuStripItem.Checked




Well thats it =)

Posted Image

Hope you enjoyed this guide. If you need anymore screenies tell me lol, i'll add em.

~kierien

Edited by Kierien, 31 January 2009 - 11:26 AM.

  • 8

#2 WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderator
  • 17,088 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 31 January 2009 - 05:00 AM

Very nice. +rep
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 Egz0N

Egz0N

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,155 posts

Posted 31 January 2009 - 05:57 AM

nice tutorial dude .. and +rep :)
  • 0

#4 mendim.

mendim.

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 741 posts

Posted 31 January 2009 - 08:54 AM

nice Job , +rep .
  • 0

#5 Kierien

Kierien

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 31 January 2009 - 08:56 AM

Thanks guys, working on screenies and left out code now =)

Edit: Done.

~kierien

Edited by Kierien, 31 January 2009 - 10:13 AM.

  • 0

#6 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8,356 posts

Posted 31 January 2009 - 09:56 AM

I disagree with the last bit (the word wrap). It would be much better to use a single checked box to wrap text.

To achieve this, set the menu item's CheckOnClick boolean property to True, and then use the following code in the Click event:

textBox1.WordWrap = wordWrapMenuStripItem.Checked

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#7 Kierien

Kierien

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 31 January 2009 - 10:18 AM

Xav, ur right, edited.
  • 0

#8 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8,356 posts

Posted 31 January 2009 - 10:43 AM

Excellent. Your way would still work, but from a standard application point of view, a single menu item that checks on click is much better.

A smallish thing: I would change the name of the textbox to something like txtMain, because it's such an important control in the application.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#9 Kierien

Kierien

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 31 January 2009 - 10:47 AM

Excellent. Your way would still work, but from a standard application point of view, a single menu item that checks on click is much better.

A smallish thing: I would change the name of the textbox to something like txtMain, because it's such an important control in the application.


True, but im trying to keep the code syncronized with the person reading this tut, who which on default would have the textBox name "textBox1" =D

But yeah, giving objects names would be helpful when coding.

~kierien
  • 1

#10 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8,356 posts

Posted 31 January 2009 - 10:51 AM

You could add a simple instruction to edit the Name property of the textbox when you create it.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#11 Kierien

Kierien

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 31 January 2009 - 11:23 AM

You could add a simple instruction to edit the Name property of the textbox when you create it.


Alright then, done.
  • 0

#12 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8,356 posts

Posted 31 January 2009 - 11:23 AM

Great! Now I've got that off my chest.. +rep!
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?





Also tagged with one or more of these keywords: streamwriter