I am trying to create a text editor but I got a small isue. I want the save,copy,paste buttons to be disable untill I start to make changes on that text file. Then be able to save the file. Also can I open another tab or window in the same form so that I can edit 2 different text files in same window?Thanks fot the help!!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace TXTEDTR
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
saveToolStripMenuItem.Enabled = false;
cutToolStripButton.Enabled = false;
copyToolStripButton.Enabled = false;
pasteToolStripButton.Enabled = false;
}
bool saved = false;
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
saved = false;
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void fileToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
saved = true;
if (CurrentFile == "") saveAsToolStripMenuItem_Click(sender, e);
else richTextBox1.SaveFile(CurrentFile, RichTextBoxStreamType.PlainText);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (saved == false)
{
if (MessageBox.Show("Changes were not saved! Do you still want to exit?", "Warning", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
{
e.Cancel = true;
}
}
}
string CurrentFile = "";
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Text != "")
{
if (((DialogResult.OK == MessageBox.Show("The content will be lost.", "Still want to continue?", MessageBoxButtons.OKCancel))))
{
richTextBox1.Text = "";
CurrentFile = "";
}
}
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (DialogResult.OK == openFileDialog1.ShowDialog())
{
CurrentFile = openFileDialog1.FileName;
if (Path.GetExtension(CurrentFile) == ".txt" || Path.GetExtension(CurrentFile) == ".cs") richTextBox1.LoadFile(CurrentFile, RichTextBoxStreamType.PlainText);
else richTextBox1.LoadFile(CurrentFile);
this.Text = Path.GetFileName(CurrentFile) + "- Text Editor";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (CurrentFile == "")
{
saveFileDialog1.FileName = "Untitled";
}
if (DialogResult.OK == saveFileDialog1.ShowDialog())
{
if (Path.GetExtension(saveFileDialog1.FileName) == ".txt" || Path.GetExtension(CurrentFile) == ".cs")
{
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
}
else richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
CurrentFile = saveFileDialog1.FileName;
this.Text = Path.GetFileName(CurrentFile) + "- Text Editor";
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void a?ToolStripButton_Click(object sender, EventArgs e)
{
try
{
if (DialogResult.OK == openFileDialog1.ShowDialog())
{
CurrentFile = openFileDialog1.FileName;
richTextBox1.Text = File.ReadAllText(CurrentFile);
if (Path.GetExtension(CurrentFile) == ".txt" || Path.GetExtension(CurrentFile) == ".cs") richTextBox1.LoadFile(CurrentFile, RichTextBoxStreamType.PlainText);
else richTextBox1.LoadFile(CurrentFile);
this.Text = Path.GetFileName(CurrentFile) + "- Text Editor";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void newToolStripButton_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
}
private void saveToolStripButton_Click(object sender, EventArgs e)
{
saved = true;
if (CurrentFile == "") saveAsToolStripMenuItem_Click(sender, e);
else richTextBox1.SaveFile(CurrentFile, RichTextBoxStreamType.PlainText);
}
private void cutToolStripButton_Click(object sender, EventArgs e)
{
richTextBox1.Cut();
}
private void copyToolStripButton_Click(object sender, EventArgs e)
{
richTextBox1.Copy();
}
private void pasteToolStripButton_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}
}
}
question from:
https://stackoverflow.com/questions/65927190/how-to-enable-save-button-and-other-buttons-after-editing-the-rich-text-i-c-sha 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…