TextBox.Clear()
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