• (2 Pages)
  • +
  • 1
  • 2

Creating your own Notepad App

#1 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Posted 20 August 2008 - 08:55 PM

This is a Two-Part Tutorial on Creating and Extending the Notepadd App.

Create a Notepad App
In this tutorial I will show you how you can create your own Notepad like Application.

Start by creating a new Windows Forms Application.

Now we can add some controls. The first control will be a MenuStrip.
Posted Image

After that right click on the newly created menustrip, Select Insert Standard Items. Delete the Tools menu and the Help menu. Delete the Redo menu item from the Edit menu.

Now drag and drop a TextBox onto the form.
Posted Image

Make sure that the textbox can have more than one line, by selecting the multiline checkbox.
Posted Image

Type in txtBox for the name of our newly created textbox. Set the Dock to Fill

Now we will do some coding.

For the New menu item's Click() event type in txtBox.Clear();
For the Open menu item's Click() event type in
			//Declare open as a new OpenFileDailog
			OpenFileDialog open = new OpenFileDialog();
			//Set the Filename of the OpenFileDailog to nothing
			open.FileName = "";
			//Declare filename as a String equal to the OpenFileDialog's FileName
			String filename = open.FileName;
			//Declare filter as a String equal to our wanted OpenFileDialog Filter
			String filter = "Text Files|*.txt|All Files|*.*";
			//Set the OpenFileDialog's Filter to filter
			open.Filter = filter;
			//Set the title of the OpenFileDialog to Open
			open.Title = "Open";
			//Show the OpenFileDialog
			if (open.ShowDialog(this) == DialogResult.OK)
			{
				//Make the txtBox's Text equal to all of the text in the OpenFileDialog's FileName(filename)
				txtBox.Text = System.IO.File.ReadAllText(filename);
			}
			else
			{
				//Return
				return;
			}



For the Save and Save As menu items' Click() event type in
			//Declare save as a new SaveFileDailog
			SaveFileDialog save = new SaveFileDialog();
			//Declare filename as a String equal to the SaveFileDialog's FileName
			String filename = save.FileName;
			//Declare filter as a String equal to our wanted SaveFileDialog Filter
			String filter = "Text Files|*.txt|All Files|*.*";
			//Set the SaveFileDialog's Filter to filter
			save.Filter = filter;
			//Set the title of the SaveFileDialog to Save
			save.Title = "Save";
			//Show the SaveFileDialog
			if (save.ShowDialog(this) == DialogResult.OK)
			{
				//Write all of the text in txtBox to the specified file
				System.IO.File.WriteAllText(filename, txtBox.Text);
			}
			else
			{
				//Return
				return;
			}



After enter this code
			//Declare prntDoc as a new PrintDocument
			System.Drawing.Printing.PrintDocument prntDoc = new System.Drawing.Printing.PrintDocument();



For the Print menu items' Click() event type in
			//Declare print as a new PrintDialog
			PrintDialog print = new PrintDialog();
			//Declare prntDoc_PrintPage as a new EventHandler for prntDoc's Print Page
			prntDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(prntDoc_PrintPage);
			//Set prntDoc to the PrintDialog's Document
			print.Document = prntDoc;
			//Show the PrintDialog
			if (print.ShowDialog(this) == DialogResult.OK)
			{
				//Print the Page
				prntDoc.Print();
			}




Now enter this code right after the print item's Click() event



		private void prntDoc_PrintPage(Object sender, System.Drawing.Printing.PrintPageEventArgs e)
		{
			//Declare g as Graphics equal to the PrintPageEventArgs Graphics
			Graphics g = e.Graphics;
			//Draw the Text in txtBox to the Document
			g.DrawString(txtBox.Text, txtBox.Font, Brushes.Black, 0, 0);
		}



Now enter this code for the Print Preview menu item's Click() event
			//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();
			}



Now go to the Exit menu item's Click() event and type Close;
Now go to the Undo menu item's Click() event and type txtBox.Undo();
Now go to the Cut menu item's Click() event and type txtBox.Cut();
Now go to the Copy menu item's Click() event and type txtBox.Copy();
Now go to the Paste menu item's Click() event and type txtBox.Paste();
Now go to the Select All menu item's Click() event and type txtBox.SelectAll();

Finished!

Don't forget I have included the source files: Attached File  Notes.zip (63.59K)
Number of downloads: 3348

Extend the Notepad App

In this tutorial I will show you how to extend your notepad app we created in my tutorial. If you have not read my tutorial then click here.

We will add some Menu items to the MenuStrip. The first Item will we a new Menu Drop Down called Format.
In the format Drop Down Menu add a menu item called Word Wrap. Set the Checked Property to true, set the Checkonclick Property to true. Now select the TextBox(txtBox) and set the WordWrap property to true.

Now go back to the Format menu and add one more menu item call it Font.
Drag a FontDialog onto the Form, call it fontDialog
Posted Image

Go to the Font menu item's Click() event and type
			fontDialog.ShowColor = true;
			fontDialog.ShowEffects = true;
			if (fontDialog.ShowDialog(this) == DialogResult.OK)
			{
				txtBox.ForeColor = fontDialog.Color;
				txtBox.Font = fontDialog.Font;
			}



Now we will add some shortcut keys.
For the New menu item's shortcut key type in Ctrl+N
For the Open menu item's shortcut key type in Ctrl+O
For the Save menu item's shortcut key type in Ctrl+S
For the Save As menu item's shortcut key type in Ctrl+Shift+S
For the Print menu item's shortcut key type in Ctrl+P
For the Print Preview menu item's shortcut key type in Ctrl+Shift+P
For the Undo menu item's shortcut key type in Ctrl+Z
For the Cut menu item's shortcut key type in Ctrl+X
For the Copy menu item's shortcut key type in Ctrl+C
For the Paste menu item's shortcut key type in Ctrl+V
For the Select All menu item's shortcut key type in Ctrl+A
For the Font menu item's shortcut key type in Alt+F


Now we will add a StatusStrip()
Posted Image

Make sure that it is docked to the bottom, then Right Click on txtBox and select Bring to Front.

Now add a StatusLabel to the StatusStrip by selecting the small arrow on the StatusStrip, call it statusLabel.

Now go to the txtBox's TextChanged() event and type
			Int32 lines = txtBox.Lines.Length;
			Int32 textLength = txtBox.Text.Length;
			statusLabel.Text = "Lines: " + lines + " Characters: " + textLength;



Now paste that in the Form's Load() event as well.

Now go to the Word Wrap menu item's Click() event and type
			txtBox.WordWrap = wordWrapToolStripMenuItem.Checked;
			wordWrapToolStripMenuItem.Checked = txtBox.WordWrap;



Finished!

Don't forget that I have included the source files Attached File  Notes_Extend_.zip (70.62K)
Number of downloads: 4468

This post has been edited by gbertoli3: 27 August 2008 - 07:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Creating your own Notepad App

#2 c.v.burgess  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-September 08

Posted 20 September 2008 - 12:25 PM

i get an error "file name cannot be empty" when opening or saving in yoUR app and mine. What's the problem?
Was This Post Helpful? 0
  • +
  • -

#3 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Posted 20 September 2008 - 12:51 PM

I don't know can you post a screenshot of the ErrorMsg
Was This Post Helpful? 0
  • +
  • -

#4 c.v.burgess  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-September 08

Posted 20 September 2008 - 01:20 PM

Attached Image
Was This Post Helpful? 0
  • +
  • -

#5 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Posted 20 September 2008 - 01:22 PM

What were you doing to get that message?
Was This Post Helpful? 0
  • +
  • -

#6 c.v.burgess  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-September 08

Posted 20 September 2008 - 01:27 PM

Attached Image

that is opening a file
Was This Post Helpful? 0
  • +
  • -

#7 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Posted 20 September 2008 - 01:30 PM

That's Weird!

Did you make sure that the file exists.

Go to the openFileDialog's Properties and make sure the CheckFileExists Items is set to True
Was This Post Helpful? 0
  • +
  • -

#8 c.v.burgess  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-September 08

Posted 20 September 2008 - 01:37 PM

View Postgbertoli3, on 20 Sep, 2008 - 01:30 PM, said:

That's Weird!

Did you make sure that the file exists.

Go to the openFileDialog's Properties and make sure the CheckFileExists Items is set to True

i did
Was This Post Helpful? 0
  • +
  • -

#9 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Posted 20 September 2008 - 01:48 PM

That's really weird. Did you make any modifications to the project?
Was This Post Helpful? 0
  • +
  • -

#10 c.v.burgess  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-September 08

Posted 20 September 2008 - 02:00 PM

View Postgbertoli3, on 20 Sep, 2008 - 01:48 PM, said:

That's really weird. Did you make any modifications to the project?

none WHATSOEVER
Was This Post Helpful? 0
  • +
  • -

#11 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Posted 20 September 2008 - 02:11 PM

That is really weird because it works for me fine. The only thing I can think of doing is redownload the project.
Was This Post Helpful? 0
  • +
  • -

#12 c.v.burgess  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-September 08

Posted 21 September 2008 - 07:50 AM

this works for me (save file)
private void button1_Click(object sender, System.EventArgs e)
 {
	 Stream myStream;
	 SaveFileDialog saveFileDialog1 = new SaveFileDialog();

	 saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
	 saveFileDialog1.FilterIndex = 2;
	 saveFileDialog1.RestoreDirectory = true;

	 if(saveFileDialog1.ShowDialog() == DialogResult.OK)
	 {
		 if((myStream = saveFileDialog1.OpenFile()) != null)
		 {
			 // Code to write the stream goes here.
			 myStream.Close();
		 }
	 }
 }


but will u tell me what to put for "//Code to write the strream goes here"? thank you

This post has been edited by c.v.burgess: 21 September 2008 - 08:04 AM

Was This Post Helpful? 0
  • +
  • -

#13 c.v.burgess  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-September 08

Posted 21 September 2008 - 08:06 AM

this is open
{
			Stream myStream = null;
			OpenFileDialog openFileDialog1 = new OpenFileDialog();
			 
			openFileDialog1.InitialDirectory = "c:\\";
			openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
			openFileDialog1.FilterIndex = 2;
			openFileDialog1.RestoreDirectory = true;
			 
			if (openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				try
				{
					if ((myStream = openFileDialog1.OpenFile()) != null)
					{
						using (myStream)
						{
							// Insert code to read the stream here.
						}
					}
				}
				catch (Exception ex)
				{
					MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
				}
			}
		}

what do i do to read the stream?
Was This Post Helpful? 0
  • +
  • -

#14 c.v.burgess  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-September 08

Posted 21 September 2008 - 08:32 AM

HEY! how could you print with a 1/2 inch margin?
Was This Post Helpful? 0
  • +
  • -

#15 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Posted 21 September 2008 - 11:23 AM

Post your questions in the Forum, with the code you already have.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2