Search Forum
(57415 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


              
Printable Version

Simple Notepad
By Shireesha Magesh Kumar

In this article i have developed a simple windows application which will be more useful for beginners. So this is a simple application similar to ordinary Text Editor. Here in this application i have added some features and removed some features of ordinary text editor. Here we we can choose any color for the text.

Simple Notepad

Open a New Project and choose the Visual C# and In Template select Windows Application. In Windows Application, Open Solution Explorer and right click the project name. In the context menu select add and select Windows Form. In that add the following code, build and run the application. Sample Code:

     if (textBox1.Text != "")
        {
            DialogResult click = MessageBox.Show("The text in the Untitled has changed.\n\n Do you want to save the changes?", " My    
                   Notepad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            if (click == DialogResult.Yes)
            {
                if (fname == "") 
                {
                    saveFileDialog1.Filter = "Text Files|*.textBox1";
                    DialogResult result = saveFileDialog1.ShowDialog();
                    if (result == DialogResult.Cancel )
                    {
                        return;
                    }
                    fname = saveFileDialog1.FileName;
                   }
                StreamWriter write = new StreamWriter(fname); 
                write.WriteLine(textBox1.Text);
                write.Flush();
                write.Close();
                textBox1.Text = "";
                fname = "";
                }
            if (click == DialogResult.No)
            {
                textBox1.Text = ""; 
                fname = "";
            }
       }
        else
        {
            textBox1.Text = "";
            fname = "";
        }
      }
    
In the above code, we checked whether the " textBox1.Text != """,then in the next statement we have message box whether to save the existing one before opening a new file and then open the dialog box "DialogResult result=saveFileDialog1.ShowDialog ()". If the user want to save then use StreamWriter to save the filename and display it whenever necessary. Clear the textbox and it shows a new untitled page.
         openFileDialog1.Multiselect = false;
        openFileDialog1.Filter = "Text Files|*.txt|All files (*.*)|*.*";
        openFileDialog1.ShowDialog();
        if (File.Exists(openFileDialog1.FileName ))
        {
            fname = openFileDialog1.FileName;
            StreamReader sr = new StreamReader(fname);
            textBox1.Text = sr.ReadToEnd();
            sr.Close();
        }
  
In the above code, we used the open file dialog to open a file and using filter property to filter only "Text Files".If specified file exists then read upto end of file.
if (fname == "")
        {
            saveFileDialog1.Filter = "Text Files|*.txt";
            DialogResult result = saveFileDialog1.ShowDialog();
            if (result == DialogResult.Cancel )
            {
                return;
            }
            fname = saveFileDialog1.FileName;
            StreamWriter s = new StreamWriter(fname);
            s.WriteLine(textBox1.Text);
            s.Flush();
            s.Close();
        }
In the above code, we used the save file dialog and Filtered only Text Files. Using Stream Writer we write the filename to the location specified. Similarly use Page Setup dialog for Page setup and Print Dialog for Print. But for Print,
private void document_PrintPage(object sender,
      System.Drawing.Printing.PrintPageEventArgs e)
    {

        // Insert code to render the page here.
        // This code will be called when the control is drawn.

        // The following code will render a simple
        // message on the printed document.
        string text = "In document_PrintPage method.";
        System.Drawing.Font printFont = new System.Drawing.Font 
            ("Arial", 35, System.Drawing.FontStyle.Regular);

        // Draw the content.
        e.Graphics.DrawString(text, printFont,
            System.Drawing.Brushes.Black, 10, 10);
    }
For undo,
 if (textBox1.CanUndo)
 textbox1.undo();
Similarly,
Cut:   
           textBox1.cut(); This will cut the text.
          
Copy:

          textBox1.copy(); This will copy the text.

Paste:
         textBox1.Paste(); This will paste the text.

Delete:
         textBox1.Clear(); This will delete the text.

Select All:
        textBox1.SelectAll(); This will select the whole text in the textbox.

For WordWrap:

 wordWrapToolStripMenuItem.Checked = !(wordWrapToolStripMenuItem.Checked);
        textBox1.WordWrap = wordWrapToolStripMenuItem.Checked;
    
Font:

  Open Font Dialog and use textBox1.ForeColor = fontDialog1.Color; to get color for text.

Date/Time:
use "DateTime.Now.ToShortDateString();" to get the Date/Time.