• (2 Pages)
  • +
  • 1
  • 2

Creating your own Notepad App

#16 wingot  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 13-October 08

Posted 15 October 2008 - 12:51 AM

View Postgbertoli3, on 20 Sep, 2008 - 02:11 PM, said:

That is really weird because it works for me fine. The only thing I can think of doing is redownload the project.


The problem looks like this piece of code in your tutorial:

			//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;



You're first setting the FileName property to "" then assigning it to the filename string (so filename will always equal ""). You aren't then setting the filename after showing the dialogue. Of course, this means that when it tries to open "filename", it tries to open "", hence the exception stating "Empty path name is not legal".

Changing it to this will work:

			//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)
			{
				// Set the filename to the dialogues value
				filename = open.Filename;
				//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;
			}



All I've done of course is add "filename = open.Filename;" into the if before actually trying to open the file.

This post has been edited by wingot: 15 October 2008 - 01:43 AM

Was This Post Helpful? 0
  • +
  • -

#17 wingot  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 13-October 08

Posted 15 October 2008 - 03:12 AM

Also, I've refactored your tutorial code to be more in keeping with good practice (not duplicating the same code in multiple methods for example). Adds a couple of extra functions, but makes the code both less redundant and more readable.

I also modified the saveToolStripMenuItem_Click event so that if a filename was already selected (from a previous "Save As" or "Open" operation) then this would be used (which is more in keeping with the nature of the Save option in any other program). It seemed a bit redundant and incorrect to have it doing the exact same work as the save as.

Finally, I modified the word wrap functionality slightly so that if word wrap is on the horizontal scrollbar will not display and vice-versa.

I added an about dialogue just for the sake of it too.

Of course, given that this is meant to be a tutorial this sort of thing is unneccessary, but even so, feel free to take a look at the attached code if you want.

NOTE: The attached code was created in VS2008, so the solution file may not work in VS2005.

EDIT: Added fix so that "New" option also clears the filename.

This post has been edited by wingot: 15 October 2008 - 03:16 AM

Was This Post Helpful? 0
  • +
  • -

#18 whitefish  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-October 08

Posted 16 October 2008 - 02:37 PM

for the problem of "Empty path name is not legal"
Change the following code from:-
this.txtBox.Text = System.IO.File.ReadAllText(filename);



to this:-
this.txtBox.Text = System.IO.File.ReadAllText(this.open.FileName);



The problem here was that the string filename was predefined with empty string and didnt have been changed after the user selected the file in the openFileDialog object. So, to solve it the best way was to take the file name from the object file (open) it self.

I didn't checked the save file yet so sorry.


Edit: sorry I didn't checked the last posts of the member before posting mine, so please forgive me

This post has been edited by whitefish: 16 October 2008 - 02:38 PM

Was This Post Helpful? 0
  • +
  • -

#19 DOOMDUDE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-October 08

Posted 25 October 2008 - 02:18 PM

View Postc.v.burgess, on 20 Sep, 2008 - 12:25 PM, said:

i get an error "file name cannot be empty" when opening or saving in yoUR app and mine. What's the problem?


i got the same error and i managed to fix it...

i think the problem is that the filename is checked too soon and that you have to move it so it is checked when the user presses ok... in other words the original code:

//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;
}

has to be changed to:

//Declare open as a new OpenFileDailog
OpenFileDialog open = new OpenFileDialog();
//Set the Filename of the OpenFileDailog to nothing
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)
{
//Declare filename as a String equal to the OpenFileDialog's FileName
String filename = open.FileName;
//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;
}



the same applies to the save dialog because i got the same error there...
Was This Post Helpful? 0
  • +
  • -

#20 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

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

Posted 25 October 2008 - 02:19 PM

Yes, sorry about that. I guess I didn't even realize it.
Was This Post Helpful? 0
  • +
  • -

#21 Zooms  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 78
  • Joined: 04-December 08

Posted 21 December 2008 - 06:16 AM

I got about 6 errors:

The name 'prntDoc' does not exist in the current context.


The code where the error is:

private void printToolStripMenuItem_Click(object sender, EventArgs e)
		{
			//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();
			}
		}


And in the Printpreview.

This post has been edited by Zooms: 21 December 2008 - 06:17 AM

Was This Post Helpful? 0
  • +
  • -

#22 wingot  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 13-October 08

Posted 21 December 2008 - 06:38 AM

View PostZooms, on 21 Dec, 2008 - 05:16 AM, said:

I got about 6 errors:

The name 'prntDoc' does not exist in the current context.

*snip*

And in the Printpreview.


You want something like this in the constructor of the form:
prntDoc = new System.Drawing.Printing.PrintDocument();


This post has been edited by wingot: 21 December 2008 - 06:39 AM

Was This Post Helpful? 0
  • +
  • -

#23 Fuingurth  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 118
  • Joined: 13-August 09

Posted 13 September 2009 - 12:06 PM

so none of the stuff anyone has posted has worked for this. Everything either has 1 error which leads to another, or will compile but then have an exception.
Does anyone know what WILL work?
Was This Post Helpful? 0
  • +
  • -

#24 sam.adams61  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 283
  • Joined: 14-July 08

Posted 20 September 2009 - 05:26 PM

View PostFuingurth, on 13 Sep, 2009 - 06:06 PM, said:

Does anyone know what WILL work?

Yep, I know!! Get the author to scrub it & start all over again, this time compiling the code * running it himself to make sure it works. While I greatly appreciate the time & effort put into writing tutorials, it would prove a lot less irksome to those using the tutorials if the authors proved their own code first.
Having said that, the suggestions made by DoomDude worked fine for me.

This post has been edited by sam.adams61: 20 September 2009 - 05:32 PM

Was This Post Helpful? 0
  • +
  • -

#25 Fuingurth  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 118
  • Joined: 13-August 09

Posted 21 September 2009 - 01:25 PM

View Postsam.adams61, on 20 Sep, 2009 - 04:26 PM, said:

View PostFuingurth, on 13 Sep, 2009 - 06:06 PM, said:

Does anyone know what WILL work?

Having said that, the suggestions made by DoomDude worked fine for me.


Yeah i went back and retried doom's code and this time it worked. So thanks to both of you
Was This Post Helpful? 0
  • +
  • -

#26 deepanshuchopra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-September 09

Posted 25 September 2009 - 04:43 AM

check this out
i have created a far more better notepad using richtext box
which is working fine for me
using open ,close , print, and pagesetup all options

MOD EDIT: Removed ZIP attachment, contained no code, just an executable.
Was This Post Helpful? 0
  • +
  • -

#27 Guest_Vishal*


Reputation:

Posted 10 February 2011 - 10:48 AM

You Really done a great effort dude keep going
Was This Post Helpful? 0

#28 CY5  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 349
  • Joined: 28-September 12

Posted 09 June 2013 - 08:59 AM

View Postc.v.burgess, on 20 September 2008 - 07:25 PM, said:

i get an error "file name cannot be empty" when opening or saving in yoUR app and mine. What's the problem?

try this since i was facing the same problem ,
System.IO.File.WriteAllText(this.Text, textBox1.Text);
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2