Text editor in C#
Below is a basic example of a text editor using TextBox for text input and output and SaveFileDialog and OpenFileDialog for file operations. Copy and paste this code into a C# file (e.g., TextEditor.cs) and run it.
using System; using System.IO; using System.Windows.Forms; class TextEditor : Form { private TextBox textBox; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; public TextEditor() { Text = "Text Editor"; Size = new System.Drawing.Size(800, 600); FormBorderStyle = FormBorderStyle.FixedSingle; MaximizeBox = false; textBox = new TextBox(); textBox.Multiline = true; textBox.Dock = DockStyle.Fill; Controls.Add(textBox); MenuStrip menuStrip = new MenuStrip(); Controls.Add(menuStrip); ToolStripMenuItem fileMenu = new ToolStripMenuItem("File"); menuStrip.Items.Add(fileMenu); ToolStripMenuItem openItem = new ToolStripMenuItem("Open"); ToolStripMenuItem saveItem = new ToolStripMenuItem("Save"); ToolStripMenuItem exitItem = new ToolStripMenuItem("Exit"); fileMenu.DropDownItems.Add(openItem); fileMenu.DropDownItems.Add(saveItem); fileMenu.DropDownItems.Add(exitItem); openFileDialog = new OpenFileDialog(); saveFileDialog = new SaveFileDialog(); openItem.Click += (sender, e) => OpenFile(); saveItem.Click += (sender, e) => SaveFile(); exitItem.Click += (sender, e) => Close(); } private void OpenFile() { if (openFileDialog.ShowDialog() == DialogResult.OK) { try { string content = File.ReadAllText(openFileDialog.FileName); textBox.Text = content; } catch (IOException) { MessageBox.Show("Error reading the file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void SaveFile() { if (saveFileDialog.ShowDialog() == DialogResult.OK) { try { File.WriteAllText(saveFileDialog.FileName, textBox.Text); } catch (IOException) { MessageBox.Show("Error writing to the file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new TextEditor()); } }