Học tập‎ > ‎C#‎ > ‎

Simple Notepad at C#

Không thể tìm thấy URL thông số tiện ích

To best see the ToolStrip in action, let us create a simple notepad application.

  1. Create a new project

  2. Add a ToolStrip to a Form


  3. Insert Standard Items by clicking on the Smart Tag icon Image:SmartTagGlyph.jpg


  4. Change the RenderMode to System


  5. Create event handlers for all of the buttons on the ToolStrip by double-clicking on them.

    private void newToolStripButton_Click(object sender, EventArgs e)
    {
     
    }
     
    private void openToolStripButton_Click(object sender, EventArgs e)
    {
     
    }
     
    private void saveToolStripButton_Click(object sender, EventArgs e)
    {
     
    }
     
    private void printToolStripButton_Click(object sender, EventArgs e)
    {
     
    }
     
    private void cutToolStripButton_Click(object sender, EventArgs e)
    {
     
    }
     
    private void copyToolStripButton_Click(object sender, EventArgs e)
    {
     
    }
     
    private void pasteToolStripButton_Click(object sender, EventArgs e)
    {
     
    }

  6. Change the Form to an MDI Form

    Image:Np04.jpg

  7. Modify the code of the form to the following

    int count;
    Form mdiChild;
    TextBox editTextBox;
     
    public Form1()
    {
      InitializeComponent();
      count = 1;
    }

  8. Edit the newToolStripButton_Click method with the following code:

    private void newToolStripButton_Click(object sender, EventArgs e)
    {
      mdiChild = new Form();
      mdiChild.Text = "Document" + count.ToString();
      mdiChild.MdiParent = this;
      editTextBox = new TextBox();
      editTextBox.Multiline = true;
      editTextBox.Dock = DockStyle.Fill;
      mdiChild.Controls.Add(editTextBox);
      mdiChild.Show();
      count++;
    }

  9. Test the new method

    Image:Np05.jpg

  10. Edit the cutToolStripButton_Click method with the following code:

    private void cutToolStripButton_Click(object sender, EventArgs e)
    {
      Form activeChildForm = this.ActiveMdiChild;
     
      if (activeChildForm != null)
      {
        TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;
        if (activeTextBox != null)
        {
          activeTextBox.Cut();
        }
      }
    }

  11. Edit the copyToolStripButton_Click method with the following code:

    private void copyToolStripButton_Click(object sender, EventArgs e)
    {
      Form activeChildForm = this.ActiveMdiChild;
     
      if (activeChildForm != null)
      {
        TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;
        if (activeTextBox != null)
        {
          activeTextBox.Copy();
        }
      }
    }

  12. Edit the pasteToolStripButton_Click method with the following code:

    private void pasteToolStripButton_Click(object sender, EventArgs e)
    {
      Form activeChildForm = this.ActiveMdiChild;
     
      if (activeChildForm != null)
      {
        TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;
        if (activeTextBox != null)
        {
          activeTextBox.Paste();
        }
      }
    }

  13. Test the Cut, Copy and Paste methods

    Image:Np06.jpg

  14. Edit the saveToolStripButton_Click method with the following code:

    private void saveToolStripButton_Click(object sender, EventArgs e)
    {
      SaveFileDialog sfd = new SaveFileDialog();
     
      sfd.Title = "Save a Text File";
      sfd.Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*";
     
      DialogResult dr = sfd.ShowDialog();
      if (dr == DialogResult.OK)
      {
        System.IO.StreamWriter sw = new System.IO.StreamWriter(sfd.FileName);
     
        Form activeChildForm = this.ActiveMdiChild;
     
        if (activeChildForm != null)
        {
          TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;
     
          if (activeTextBox != null)
          {
            sw.Write(activeTextBox.Text);
          }
     
          sw.Close();
        }
      }
    }

  15. Edit the openToolStripButton_Click method with the following code:

    private void openToolStripButton_Click(object sender, EventArgs e)
    {
      OpenFileDialog ofd = new OpenFileDialog();
     
      ofd.Title = "Open a Text File";
      ofd.Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*";
     
      DialogResult dr = ofd.ShowDialog();
      if (dr == DialogResult.OK)
      {
        System.IO.StreamReader sr = new System.IO.StreamReader(ofd.FileName);
     
        Form activeChildForm = this.ActiveMdiChild;
     
        if (activeChildForm != null)
        {
          TextBox activeTextBox = activeChildForm.ActiveControl as TextBox;
     
          if (activeTextBox != null)
          {
            activeTextBox.Text = sr.ReadToEnd();
          }
     
          sr.Close();
        }
      }
    }

  16. Edit the printToolStripButton_Click method with the following code:

    /// <summary>
    /// This displays the Print dialog only, it does not print the document
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void printToolStripButton_Click(object sender, EventArgs e)
    {
      PrintDialog pd = new PrintDialog();
      pd.ShowDialog();
    }
  17. We can add more controls to the ToolStrip such as:
    • Button
    • Label
    • SplitButton
    • DropDownButton
    • Separator
    • ComboBox
    • TextBox
    • ProgressBar

    Image:Np07.jpg

Comments