Posted by : Unknown Wednesday, September 25, 2013

Sebelumnya, kita sudah membahas bagaimana cara membuat notepad sederhana dengan C#. Pada tahap pertama adalah membuat desain formnya.

Pada postingan kali ini, saya akan membahas tahap kedua yang merupakan tahap terpenting yaitu codingan..

Deklarasi :
string namaFile = "";

Dimulai dengan script untuk membuat file baru (Menu-New) :
textBox1.Clear();
namaFile = "";

Menu-File-Open :
openFileDialog1.Filter = "namaFile(*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.ShowDialog();
namaFile = openFileDialog1.FileName;
textBox1.Text = File.ReadAllText(namaFile);

Menu-File-Save :
if (namaFile == "")
{
 saveFileDialog1.Filter = "namaFile (*.txt)|*.txt|All files (*.*)|*.*";
 saveFileDialog1.FilterIndex = 2;
 saveFileDialog1.RestoreDirectory = true;

 DialogResult pilihan = saveFileDialog1.ShowDialog();

 if (pilihan == DialogResult.OK)
 {
  //menunjuk file yg akan di save
  System.IO.StreamWriter simpan = new  
  System.IO.StreamWriter(saveFileDialog1.FileName);
  simpan.WriteLine(textBox1.Text);

  //menyimpan nama file
  namaFile = saveFileDialog1.FileName;
                    
  //close proses
  simpan.Close();
 }
}
else
    {
     System.IO.StreamWriter SaveFile = new   
     System.IO.StreamWriter(namaFile);
     SaveFile.WriteLine(textBox1.Text);
     SaveFile.Close();
    }

Menu-File-Save As :
saveFileDialog1.Filter = "namafile (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
DialogResult pilihan = saveFileDialog1.ShowDialog();

if (pilihan == DialogResult.OK)          
{
 System.IO.StreamWriter SaveFile = new  System.IO.StreamWriter(saveFileDialog1.FileName);
 SaveFile.WriteLine(textBox1.Text);
 namaFile = saveFileDialog1.FileName;
 SaveFile.Close();
}

Menu-File-Exit :
if (textBox1.Text == "")
{
  Application.Exit();
}
if (namaFile == "")
 {
  DialogResult result = MessageBox.Show("File belum disimpan. Do 
  you wanna save this?", "Warning", MessageBoxButtons.YesNo);
                
  if (result == DialogResult.Yes)
   {
    saveFileDialog1.Filter = "namafile (*.txt)|*.txt|All files   
    (*.*)|*.*";
    saveFileDialog1.FilterIndex = 2;
    saveFileDialog1.RestoreDirectory = true;
    DialogResult pilihan = saveFileDialog1.ShowDialog();

    if (pilihan == DialogResult.OK)
     {
      System.IO.StreamWriter SaveFile = new
      System.IO.StreamWriter(saveFileDialog1.FileName);
      SaveFile.WriteLine(textBox1.Text);
      namaFile = saveFileDialog1.FileName;
      SaveFile.Close();
     }
   }
   else
    {
     Application.Exit();
    }
  }
  else
   {
    System.IO.StreamWriter SaveFile = new
    System.IO.StreamWriter(namaFile);
    SaveFile.WriteLine(textBox1.Text);
    SaveFile.Close();
    Application.Exit();
   }

Menu-Edit-Cut :
textBox1.Cut();

Menu-Edit-Copy :
textBox1.Copy();

Menu-Edit-Paste :
textBox1.Paste();

Menu-Edit-Select All :
textBox1.SelectAll();

Nah, demikian codingan yang sangat sederhanan ini, semoga membantu :)

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © 2013 Laboratorium Pemrograman dan Basis Data -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -