Jump to content




Recent Status Updates

  • 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

#25 Nickalbokid96

Nickalbokid96

    CC Newcomer

  • Just Joined
  • PipPip
  • 22 posts

Posted 21 July 2009 - 01:08 PM

Nice tutorial, everything worked fine for me but I don't get the Word Wrap part, I'm new to C# :o

EDIT: Never mind, silly me, I read it wrong, I got it now thanks, +REP :) and by the way add a semicolon after the

txtMain.WordWrap = wordWrapMenuStripItem.Checked

, Just a little minor mistake XD

Edited by Nickalbokid96, 21 July 2009 - 07:19 PM.

  • 0

#26 garrettgjb

garrettgjb

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 22 July 2009 - 07:36 AM

Thanks Kierien, I'm definitely going to do this tutorial really soon.

I have a question for you: where did you learn how to do this (looking for a good book on this kind of stuff)? I know C#, I just lack the GUI programming part of this.

Thanks.
  • 0

#27 Parabola

Parabola

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 221 posts

Posted 27 July 2009 - 01:02 PM

I also seem to be having a problem with prntDoc. Here's what I've got:
[COLOR=Blue]private void[/COLOR] printPreviewToolStripMenuItem_Click([COLOR=Blue]object[/COLOR] sender, [COLOR=MediumTurquoise]EventArgs[/COLOR] e)
     {

          System.Drawing.Printing.[COLOR=MediumTurquoise]PrintDocument[/COLOR] prntDoc = [COLOR=Blue]new[/COLOR] System.Drawing.Printing.[COLOR=MediumTurquoise]PrintDocument[/COLOR]();
          [COLOR=SeaGreen]//Declare preview as a new PrintPreviewDialog[/COLOR]
          [COLOR=MediumTurquoise]PrintPreviewDialog[/COLOR] preview = [COLOR=Blue]new[/COLOR] [COLOR=MediumTurquoise]PrintPreviewDialog[/COLOR]();
          [COLOR=SeaGreen]//Declare prntDoc_PrintPage as a new EventHandler for prntDoc's Print Page[/COLOR]
          prntDoc.PrintPage += [COLOR=Blue]new[/COLOR] System.Drawing.Printing.[COLOR=MediumTurquoise]PrintPageEventHandler[/COLOR](prntDoc_PrintPage);
          [COLOR=SeaGreen]//Set the PrintPreview's Document equal to prntDoc[/COLOR]
          preview.Document = prntDoc;
          [COLOR=SeaGreen]//Show the PrintPreview Dialog[/COLOR]
          [COLOR=Blue]if[/COLOR] (preview.ShowDialog([COLOR=Blue]this[/COLOR]) == [COLOR=MediumTurquoise]DialogResult[/COLOR].OK)
          {
               [COLOR=SeaGreen]//Generate the PrintPreview[/COLOR]
               prntDoc.Print();
          }
     }
My error is:
Error 1 The name 'prntDoc_PrintPage' does not exist in the current context c:\CSharp Tutorials\Notepad\Form1.cs 71

(yes, I realize I didn't change the name of the form)

If someone could please point me in the right direction. Also, as I mentioned in my other reply today, this is my first time working with c# or VS. I have some knowledge in other languages though, and understand all the basic functions. I'm just trying to learn as much as I can as I got a job (finally) doing these kinds of things. So if you want to throw in any extra explanation, it WILL be read, I promise.
Edit:
I say first time, but I have made a form before (within past few days) that basically asks for a date frame and pulls up a report after sending a query to MySQL and another one with Microsoft SQL. However, I would like to understand and learn more about coding, not just dropping parts in and pointing them places. So really, what I'm asking is, how is this defined? how does it work? Not just what is the correct code, but actually how to fix it.
Also- note for anyone who hasn't used both- Microsoft SQL is way better.... syntax is different a little bit (having clause, aggregate functions), but it's way faster at actually running the query.
  • 0

#28 476rick

476rick

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 16 March 2012 - 07:12 AM

Nice :P. but one question, if i typ txtMain in c# they underline it in red, he doesnt know that command, what to do?
plz help me:p thnx
  • 0

#29 Jaffary

Jaffary

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 13 April 2012 - 07:24 PM

Very nice tutorial specially someone like me Who is begginer in C#
Thanx
  • 0

#30 abbyface23

abbyface23

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 26 April 2012 - 05:34 PM

great! very useful haha...thanks
  • 0

#31 TryElsIf

TryElsIf

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Others
  • Learning:Java, C#, PHP, Python, JavaScript, Others

Posted 24 July 2012 - 12:02 PM

NICE tut!

How would I add a 'Find and Replace' option to the menu?

Looked everywhere to find an answer and got nothing. So I came back here to the source.

Thank you for your help.
  • 0

#32 note

note

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts
  • Programming Language:Java, C#
  • Learning:Java, C#

Posted 03 March 2013 - 04:22 AM

I also seem to be having a problem with prntDoc. Here's what I've got:

[COLOR=Blue]private void[/COLOR] printPreviewToolStripMenuItem_Click([COLOR=Blue]object[/COLOR] sender, [COLOR=MediumTurquoise]EventArgs[/COLOR] e)
     {

          System.Drawing.Printing.[COLOR=MediumTurquoise]PrintDocument[/COLOR] prntDoc = [COLOR=Blue]new[/COLOR] System.Drawing.Printing.[COLOR=MediumTurquoise]PrintDocument[/COLOR]();
          [COLOR=SeaGreen]//Declare preview as a new PrintPreviewDialog[/COLOR]
          [COLOR=MediumTurquoise]PrintPreviewDialog[/COLOR] preview = [COLOR=Blue]new[/COLOR] [COLOR=MediumTurquoise]PrintPreviewDialog[/COLOR]();
          [COLOR=SeaGreen]//Declare prntDoc_PrintPage as a new EventHandler for prntDoc's Print Page[/COLOR]
          prntDoc.PrintPage += [COLOR=Blue]new[/COLOR] System.Drawing.Printing.[COLOR=MediumTurquoise]PrintPageEventHandler[/COLOR](prntDoc_PrintPage);
          [COLOR=SeaGreen]//Set the PrintPreview's Document equal to prntDoc[/COLOR]
          preview.Document = prntDoc;
          [COLOR=SeaGreen]//Show the PrintPreview Dialog[/COLOR]
          [COLOR=Blue]if[/COLOR] (preview.ShowDialog([COLOR=Blue]this[/COLOR]) == [COLOR=MediumTurquoise]DialogResult[/COLOR].OK)
          {
               [COLOR=SeaGreen]//Generate the PrintPreview[/COLOR]
               prntDoc.Print();
          }
     }
My error is:
Error 1 The name 'prntDoc_PrintPage' does not exist in the current context c:\CSharp Tutorials\Notepad\Form1.cs 71

(yes, I realize I didn't change the name of the form)

If someone could please point me in the right direction. Also, as I mentioned in my other reply today, this is my first time working with c# or VS. I have some knowledge in other languages though, and understand all the basic functions. I'm just trying to learn as much as I can as I got a job (finally) doing these kinds of things. So if you want to throw in any extra explanation, it WILL be read, I promise.
Edit:
I say first time, but I have made a form before (within past few days) that basically asks for a date frame and pulls up a report after sending a query to MySQL and another one with Microsoft SQL. However, I would like to understand and learn more about coding, not just dropping parts in and pointing them places. So really, what I'm asking is, how is this defined? how does it work? Not just what is the correct code, but actually how to fix it.
Also- note for anyone who hasn't used both- Microsoft SQL is way better.... syntax is different a little bit (having clause, aggregate functions), but it's way faster at actually running the query.

change prntDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(prntDoc_PrintPage); to

prntDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printPreviewToolStripMenuItem_Click);


  • 0

#33 MuhammedOwais

MuhammedOwais

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 17 May 2013 - 12:39 PM

this lines are giving errors. it cant read

prntDoc, txtMain


  • 0

#34 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 17 May 2013 - 12:48 PM

People, please learn how to use lambda expression when you are writting an event handler. Your life will be much easier.


  • 0

Microsoft Student Partner, Microsoft Certified Professional


#35 MuhammedOwais

MuhammedOwais

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 18 May 2013 - 11:58 PM

when i was checking  the code of notepad.. some functions was not working

e.g.1)txtMain.Clear(), (2) prntDoc.Print();

can u help me..

 


  • 0

#36 bre

bre

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 23 August 2013 - 11:52 AM

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)

Toolbar.jpg

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

TextBox-Dock.jpg

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

Multiline.jpg

You should now have something that looks like this.

TextBox-Dock.jpg

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

InsertStandardItems.jpg

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:





Well thats it =)

FinalForm.jpg

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

~kierien

 

How would you write the code for this using the Peek Method? I'm a newbie and can't seem to get it to work.


  • 0





Also tagged with one or more of these keywords: streamwriter