TextBox.Clear() : TextBox « System.Windows.Forms « C# / C Sharp by API






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

1.TextBox.AcceptsTab
2.TextBox.Anchor
3.TextBox.BorderStyle
4.TextBox.CanFocus
5.TextBox.Click
6.TextBox.ContextMenu
7.TextBox.ContainsFocus
8.TextBox.DataBindings
9.TextBox.DoDragDrop
10.TextBox.DragDrop
11.TextBox.DragEnter
12.TextBox.Focus()
13.TextBox.Focused
14.TextBox.GotFocus
15.TextBox.KeyPress
16.TextBox.Lines
17.TextBox.LostFocus
18.TextBox.Multiline
19.TextBox.MouseDown
20.TextBox.PasswordChar
21.TextBox.ScrollBars
22.TextBox.SelectAll()
23.TextBox.SelectedText
24.TextBox.SelectionLength
25.TextBox.TextChanged
26.TextBox.Validated
27.TextBox.Validating