Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace Files
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.             openFileDialog1.InitialDirectory =
  19.             saveFileDialog1.InitialDirectory =
  20.             Directory.GetCurrentDirectory();
  21.         }
  22.  
  23.         private void SaveToFile(string path)
  24.         {
  25.             StreamWriter sw = new StreamWriter(path, false,
  26.             Encoding.Default);
  27.             sw.WriteLine(textBox1.Text);
  28.             sw.Close();
  29.         }
  30.  
  31.         private void saveAs1_Click(object sender, EventArgs e)
  32.         {
  33.             if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  34.             {
  35.                 string s = saveFileDialog1.FileName;
  36.                 SaveToFile(s);
  37.                 Text = "Text Editor - " + Path.GetFileName(s);
  38.             }
  39.         }
  40.  
  41.         private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  42.         {}
  43.  
  44.         private void exit1_Click(object sender, EventArgs e)
  45.         {
  46.             Close();
  47.         }
  48.  
  49.         private void save1_Click(object sender, EventArgs e)
  50.         {
  51.             string path = saveFileDialog1.FileName;
  52.             if (path == "")
  53.                 saveAs1_Click(saveAs1, null);
  54.             else
  55.                 SaveToFile(path);
  56.         }
  57.  
  58.         private void new1_Click(object sender, EventArgs e)
  59.         {
  60.             textBox1.Clear();
  61.             Text = "Text Editor";
  62.             saveFileDialog1.FileName = "";
  63.         }
  64.  
  65.         private void open1_Click(object sender, EventArgs e)
  66.         {
  67.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  68.             {
  69.                 string s = openFileDialog1.FileName;
  70.                 StreamReader sr = new StreamReader(s, Encoding.Default);
  71.                 List<string> fileLines = new List<string>();
  72.                 while (true)
  73.                 {
  74.                     String ss = sr.ReadLine();
  75.                     if (ss == null)
  76.                         break;
  77.                     fileLines.Add(ss);
  78.                 }
  79.                 //textBox1.Text = sr. sr.ReadToEnd();
  80.                 sr.Close();
  81.                 textBox1.Lines = fileLines.ToArray();
  82.                 saveFileDialog1.FileName = s;
  83.                 Text = "Text Editor" + Path.GetFileName(s);
  84.                 openFileDialog1.FileName = "";
  85.             }
  86.         }
  87.         private void saveAs1_Click_1(object sender, EventArgs e)
  88.         { }
  89.  
  90.         private void menu1_Click(object sender, EventArgs e)
  91.         {
  92.            
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement