3 Replies - 2453 Views - Last Post: 01 December 2009 - 04:01 PM Rate Topic: -----

#1 RoySpoolder   User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 26-November 09

Text editor

Posted 01 December 2009 - 02:58 PM

I have a home-made text editor, but he will not save or open.
Here my code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

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

		}

		private void saveToolStripMenuItem_Click(object sender, EventArgs e)
		{

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

		}

		private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
		{

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


		}

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

		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();
			}}

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

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


		  }

		  private void exitToolStripMenuItem_Click(object sender, EventArgs e)
		  {
			  Application.Exit();
		  }

		  private void undoToolStripMenuItem_Click(object sender, EventArgs e)
		  {
			  txtBox.Undo();
		  }

		  private void cutToolStripMenuItem_Click(object sender, EventArgs e)
		  {
			  txtBox.Cut();
		  }

		  private void copyToolStripMenuItem_Click(object sender, EventArgs e)
		  {
			  txtBox.Copy();
		  }

		  private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
		  {
			  txtBox.Paste();
		  }

		  private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
		  {
			  txtBox.SelectAll();
		  }

		  private void fontDialog_Apply(object sender, EventArgs e)
		  {

			  fontDialog.ShowColor = true;
			  fontDialog.ShowEffects = true;
			  if (fontDialog.ShowDialog(this) == DialogResult.OK)
			  {
				  txtBox.ForeColor = fontDialog.Color;
				  txtBox.Font = fontDialog.Font;
			  }

		  }

		

		  private void txtBox_TextChanged(object sender, EventArgs e)
		  {

			  Int32 lines = txtBox.Lines.Length;
			  Int32 textLength = txtBox.Text.Length;
			  statusLabel.Text = "Lines: " + lines + " Characters: " + textLength;

		  }

		  private void txtBox_TextAlignChanged(object sender, EventArgs e)
		  {

		  }

		  private void wordWrapToolStripMenuItem_Click(object sender, EventArgs e)
		  {
			  txtBox.WordWrap = wordWrapToolStripMenuItem.Checked;
			  wordWrapToolStripMenuItem.Checked = txtBox.WordWrap;

		  }





	 }


		
	}




Further he works good :D

Regards,
Roy Spoolder.

Is This A Good Question/Topic? 1

Replies To: Text editor

#2 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Text editor

Posted 01 December 2009 - 03:00 PM

are you getting any error messages?
Was This Post Helpful? 0
  • +
  • -

#3 RoySpoolder   User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 26-November 09

Re: Text editor

Posted 01 December 2009 - 03:07 PM

View Posteclipsed4utoo, on 1 Dec, 2009 - 10:00 PM, said:

are you getting any error messages?


When I press build in VS 2008, I don't get code's. But when I run the program and try to open or save some file I get this error : empty pathname is not valid.

See the end of this message for details on invoking
of JIT debugging (Just In Time) instead of this dialog.

************** Tekst van uitzondering **************
System.ArgumentException: Lege padnaam is niet geldig.
   bij System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
   bij System.IO.StreamReader..ctor(String path, Encoding encoding)
   bij System.IO.File.ReadAllText(String path, Encoding encoding)
   bij System.IO.File.ReadAllText(String path)
   bij WindowsFormsApplication2.Form1.openToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\Roy\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs:regel 37
   bij System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
   bij System.Windows.Forms.ToolStripMenuItem.onclick(EventArgs e)
   bij System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
   bij System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
   bij System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
   bij System.Windows.Forms.ToolStripMenuItem.ProcessCmdKey(Message& m, Keys keyData)
   bij System.Windows.Forms.ToolStripManager.ProcessShortcut(Message& m, Keys shortcut)
   bij System.Windows.Forms.ToolStripManager.ProcessCmdKey(Message& m, Keys keyData)
   bij System.Windows.Forms.ContainerControl.ProcessCmdKey(Message& msg, Keys keyData)
   bij System.Windows.Forms.Form.ProcessCmdKey(Message& msg, Keys keyData)
   bij System.Windows.Forms.Control.ProcessCmdKey(Message& msg, Keys keyData)
   bij System.Windows.Forms.TextBoxBase.ProcessCmdKey(Message& msg, Keys keyData)
   bij System.Windows.Forms.Control.PreProcessMessage(Message& msg)
   bij System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
   bij System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)


************** Geladen assembly's **************
mscorlib
	Assembly-versie: 2.0.0.0
	Win32-versie: 2.0.50727.3603 (GDR.050727-3600)
	CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
WindowsFormsApplication2
	Assembly-versie: 1.0.0.0
	Win32-versie: 1.0.0.0
	CodeBase: file:///C:/Program%20Files/Spoolder%20BV/Klootpad/WindowsFormsApplication2.exe
----------------------------------------
System.Windows.Forms
	Assembly-versie: 2.0.0.0
	Win32-versie: 2.0.50727.3053 (netfxsp.050727-3000)
	CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
	Assembly-versie: 2.0.0.0
	Win32-versie: 2.0.50727.3053 (netfxsp.050727-3000)
	CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
	Assembly-versie: 2.0.0.0
	Win32-versie: 2.0.50727.3053 (netfxsp.050727-3000)
	CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
	Assembly-versie: 2.0.0.0
	Win32-versie: 2.0.50727.3053 (netfxsp.050727-3000)
	CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
	Assembly-versie: 2.0.0.0
	Win32-versie: 2.0.50727.3074 (QFE.050727-3000)
	CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Windows.Forms.resources
	Assembly-versie: 2.0.0.0
	Win32-versie: 2.0.50727.3053 (netfxsp.050727-3000)
	CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms.resources/2.0.0.0_nl_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------
mscorlib.resources
	Assembly-versie: 2.0.0.0
	Win32-versie: 2.0.50727.3603 (GDR.050727-3600)
	CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------

************** JIT-foutopsporing **************
If you want to enable JIT debugging must be in the configuration file for this
application or computer (machine.config) value
jitDebugging in the section system.windows.forms set.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
	<system.windows.forms jitDebugging="true" />
</ configuration>

When JIT debugging is enabled, any unhandled exceptions
to the JIT debugger on the computer that sent registered
and are not handled by this dialog.




Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6258
  • View blog
  • Posts: 24,026
  • Joined: 23-August 08

Re: Text editor

Posted 01 December 2009 - 04:01 PM

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

...

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


Don't you want to get the file name from the open dialog into the filename variable after the user has clicked OK? I mean, otherwise the filename variable is an empty string, and you now know what happens when you try to use ReadAllText on a filename that's an empty string (and you don't bother to catch exceptions).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1