Senin, 15 Mei 2017

Membuat Program C# Notepad

Tampilan :



















Codingan Notepad :

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;
using Microsoft.Win32; //library tambahan
using System.IO; //dalam program windows 32


namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen; //posisi form berada di tengah,
            this.FormBorderStyle = FormBorderStyle.FixedSingle; // form tidak bisa dibesar / dikecilkan
            this.MaximizeBox = false; //dan menghilangkan tombol maximize dan minimize.
            this.MaximizeBox = false;
        }

        void bersih()  // fungsi untuk membersihkan richtext input
        {
            rtinput.Text = "";

        }
        void bukafile() //funsi untuk membuka file ".txt"
        {
            bersih();
            OpenFileDialog buka = new OpenFileDialog();
            buka.DefaultExt = ".txt";
            buka.Filter = " Text Documents | *.txt";
            buka.FileName = "";

            if (buka.ShowDialog() != DialogResult.Cancel)
            {
                string fileterpilih = buka.FileName;
                if (fileterpilih != "")
                {
                    rtinput.LoadFile(fileterpilih, RichTextBoxStreamType.PlainText);
                }

            }
        }
        void simpanfile() // fungsi untuk menyimpan file
        {
            SaveFileDialog simpan = new SaveFileDialog();
            simpan.Filter = " text document | *.txt";
            simpan.RestoreDirectory = true;
                if (simpan.ShowDialog() != DialogResult.Cancel)
                {
                    StreamWriter filesimpan = new StreamWriter(File.Create(simpan.FileName));
                    filesimpan.Write(rtinput.Text);
                    filesimpan.Dispose();
                }

        }
    
        
        private void bbukafile_Click(object sender, EventArgs e)
        {
        if (rtinput.Text != "")
        {
            var pesan = MessageBox.Show("File belum tersimpan, yakin ingin membuka file baru ???","konfirmasi",MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (pesan == DialogResult.Yes)
            {
                bukafile();
            }
        }
            else
        {
            bukafile();
        }
        }

        private void bsimpan_Click(object sender, EventArgs e)
        {
            {
                simpanfile();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bersih();
        }

        private void rtinput_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            rtinput.SelectionFont = new Font(rtinput.Font, FontStyle.Italic);
            rtinput.SelectionStart = rtinput.SelectionStart + rtinput.SelectionLength;
            rtinput.SelectionLength = 0;
            rtinput.SelectionFont = rtinput.Font;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            rtinput.SelectionFont = new Font(rtinput.Font, FontStyle.Bold);
            rtinput.SelectionStart = rtinput.SelectionStart + rtinput.SelectionLength;
            rtinput.SelectionLength = 0;
            rtinput.SelectionFont = rtinput.Font;
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (rtinput.CanUndo == true)
            {
                rtinput.Undo();
            }
        }

        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (rtinput.CanRedo == true)
            {
                rtinput.Redo();
            }
        }

        private void copyToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            if (rtinput.SelectedText != "")
            {
                rtinput.Copy();
            }
        }

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

        private void italicToolStripMenuItem_Click(object sender, EventArgs e)
        {
            rtinput.SelectionFont = new Font(rtinput.Font, FontStyle.Italic);
            rtinput.SelectionStart = rtinput.SelectionStart + rtinput.SelectionLength;
            rtinput.SelectionLength = 0;
            rtinput.SelectionFont = rtinput.Font;
        }

      
        }
    }

SEMOGA BERMANFAAT :)

Tidak ada komentar:

Posting Komentar