Simple Editor based on TextBox : TextBox « GUI Windows Form « C# / C Sharp





Simple Editor based on TextBox

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

public class SimpleEditorForm : Form {
    private string filename = "Untitled";

    public SimpleEditorForm(string filename) {
        InitializeComponent();

        if (filename != null) {
            this.filename = filename;
            OpenFile();
        }
    }

    protected void OpenFile() {
        try {
            textBoxEdit.Clear();
            textBoxEdit.Text = File.ReadAllText(filename);
        } catch (IOException ex) {
            MessageBox.Show(ex.Message, "Simple Editor",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }

    private void OnFileNew(object sender, EventArgs e) {
        filename = "Untitled";
        textBoxEdit.Clear();
    }

    private void OnFileOpen(object sender, EventArgs e) {
        if (dlgOpenFile.ShowDialog() == DialogResult.OK) {
            filename = dlgOpenFile.FileName;
            OpenFile();
        }
    }

    private void OnFileSave(object sender, EventArgs e) {

    }

    private void OnFileSaveAs(object sender, EventArgs e) {

    }
    private void InitializeComponent() {
        this.textBoxEdit = new System.Windows.Forms.TextBox();
        this.mainMenu = new System.Windows.Forms.MenuStrip();
        this.miFile = new System.Windows.Forms.ToolStripMenuItem();
        this.miFileNew = new System.Windows.Forms.ToolStripMenuItem();
        this.miFileOpen = new System.Windows.Forms.ToolStripMenuItem();
        this.miFileSave = new System.Windows.Forms.ToolStripMenuItem();
        this.miFileSaveAs = new System.Windows.Forms.ToolStripMenuItem();
        this.dlgOpenFile = new System.Windows.Forms.OpenFileDialog();
        this.mainMenu.SuspendLayout();
        this.SuspendLayout();
        // 
        // textBoxEdit
        // 
        this.textBoxEdit.AcceptsReturn = true;
        this.textBoxEdit.AcceptsTab = true;
        this.textBoxEdit.Dock = System.Windows.Forms.DockStyle.Fill;
        this.textBoxEdit.Location = new System.Drawing.Point(0, 24);
        this.textBoxEdit.Multiline = true;
        this.textBoxEdit.Name = "textBoxEdit";
        this.textBoxEdit.ScrollBars = System.Windows.Forms.ScrollBars.Both;
        this.textBoxEdit.Size = new System.Drawing.Size(562, 219);
        this.textBoxEdit.TabIndex = 0;
        // 
        // mainMenu
        // 
        this.mainMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.miFile});
        this.mainMenu.Location = new System.Drawing.Point(0, 0);
        this.mainMenu.Name = "mainMenu";
        this.mainMenu.Size = new System.Drawing.Size(562, 24);
        this.mainMenu.TabIndex = 1;
        this.mainMenu.Text = "menuStrip1";
        // 
        // miFile
        // 
        this.miFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.miFileNew,
            this.miFileOpen,
            this.miFileSave,
            this.miFileSaveAs});
        this.miFile.Name = "miFile";
        this.miFile.Text = "&File";
        // 
        // miFileNew
        // 
        this.miFileNew.Name = "miFileNew";
        this.miFileNew.Text = "&New";
        this.miFileNew.Click += new System.EventHandler(this.OnFileNew);
        // 
        // miFileOpen
        // 
        this.miFileOpen.Name = "miFileOpen";
        this.miFileOpen.Text = "&Open";
        this.miFileOpen.Click += new System.EventHandler(this.OnFileOpen);
        // 
        // miFileSave
        // 
        this.miFileSave.Name = "miFileSave";
        this.miFileSave.Text = "&Save";
        this.miFileSave.Click += new System.EventHandler(this.OnFileSave);
        // 
        // miFileSaveAs
        // 
        this.miFileSaveAs.Name = "miFileSaveAs";
        this.miFileSaveAs.Text = "Save &As";
        this.miFileSaveAs.Click += new System.EventHandler(this.OnFileSaveAs);
        // 
        // dlgOpenFile
        // 
        this.dlgOpenFile.Filter = "Text Documents (*.txt)|*.txt|Wrox Documents (*.wroxtext)|*.wroxtext|All Files|*.*" +
            "";
        this.dlgOpenFile.FilterIndex = 2;
        // 
        // SimpleEditorForm
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(562, 243);
        this.Controls.Add(this.textBoxEdit);
        this.Controls.Add(this.mainMenu);
        this.MainMenuStrip = this.mainMenu;
        this.Name = "SimpleEditorForm";
        this.Text = "Simple Editor";
        this.mainMenu.ResumeLayout(false);
        this.ResumeLayout(false);
        this.PerformLayout();

    }



    private System.Windows.Forms.TextBox textBoxEdit;
    private System.Windows.Forms.MenuStrip mainMenu;
    private System.Windows.Forms.ToolStripMenuItem miFile;
    private System.Windows.Forms.ToolStripMenuItem miFileNew;
    private System.Windows.Forms.ToolStripMenuItem miFileOpen;
    private System.Windows.Forms.ToolStripMenuItem miFileSave;
    private System.Windows.Forms.ToolStripMenuItem miFileSaveAs;
    private System.Windows.Forms.OpenFileDialog dlgOpenFile;

    [STAThread]
    static void Main(string[] args) {
        string filename = null;
        if (args.Length != 0)
            filename = args[0];

        Application.EnableVisualStyles();
        Application.Run(new SimpleEditorForm(filename));
    }

}

 










Related examples in the same category

1.Add ScrollBars to TextBox
2.new TextBox(), Localtion, Name, TabIndex, Text
3.TextBox locationTextBox location
4.Keyboard event and TextBox
5.Get value from TextBox
6.Text Changed eventText Changed event
7.A simple text editorA simple text editor
8.Convert TextBox input to double valueConvert TextBox input to double value
9.All cap text textboxAll cap text textbox
10.Data CheckerData Checker
11.User EventsUser Events
12.TextBox and button on formTextBox and button on form
13.TextBox and ListBoxTextBox and ListBox
14.Label, TextBox and ButtonLabel, TextBox and Button
15.TextBox DemoTextBox Demo