// When clicked to open
private string files; // Create a member variable to store the open path
private void open OToolStripMenuItem_Click (object sender, EventArgs e)
{
//openFileDialog1.Filter = "Text file | * txt | Video file | * avi; * jpg"; // filter create a filter
//openFileDialog1.ShowDialog ();
DialogResult isok = openFileDialog1.ShowDialog ();
if (isok == DialogResult.OK) // Determine if the open button was clicked
{
string filename = openFileDialog1.FileName; // Get the file path, the file path is stored in FileName
// Use stream for file reading
StreamReader sr = new StreamReader (filename); // Build a streamreader object sr with the file path as a parameter
textBox1.Text = sr.ReadToEnd (); // readtoend method, read the file completely
sr.Close (); // The stream must be closed after running out
files = filename;
}
// when clicking the save as button
private void holds sToolStripMenuItem_Click (object sender, EventArgs e)
{
saveFileDialog1.Filter = "Text file | * .txt"; // File format to save. Filter file filter
DialogResult isok = saveFileDialog1.ShowDialog (); // Whether to click the save button
if (isok == DialogResult .OK)
{
string filename = saveFileDialog1.FileName;
StreamWriter sw = new StreamWriter (filename);
sw.Write (textBox1.Text);
sw.Close (); // Be sure to close the stream
}
MessageBox.Show (saveFileDialog1 .FileName);
}
// When the save button is clicked
private void Save as AToolStripMenuItem_Click (object sender, EventArgs e)
{
if (files == null)
{
DialogResult baocun = saveFileDialog1.ShowDialog ();
if (baocun == DialogResult.OK)
{
string filename = saveFileDialog1.FileName;
StreamWriter sw = new StreamWriter (filename);
sw.Write (textBox1.Text);
sw.Close ();
}
}
else
{
StreamWriter sw = new StreamWriter (files);
sw.Write (textBox1.Text);
sw.Close ();
}
}
// When clicking on the color
private void color ToolStripMenuItem_Click (object sender, EventArgs e)
{
//colorDialog1.ShowDialog();//To display the color dialog box, the dialog box to be displayed has the common property of showdialog. . colorDialog1.ShowDialog () will have a return value and accept it with the dialogresult enum type
DialogResult dr = colorDialog1.ShowDialog ();
if (dr == DialogResult .OK) // Determine whether to click the OK button
{
this.BackColor = colorDialog1.Color; // If you just write this code and click Cancel after selecting the color, it will default to a black in the system
}
}
// click the file browser to get the path
private void file browser ToolStripMenuItem_Click (object sender, EventArgs e)
{
DialogResult dr = folderBrowserDialog1.ShowDialog (); // Return value when accepting the punch file path
if (dr == DialogResult .OK)
{
MessageBox.Show (folderBrowserDialog1.SelectedPath); // selectedpath gets the attributes of the path
}
}
// When click New
private void new NToolStripMenuItem_Click (object sender, EventArgs e)
{
if (textBox1 .Text .Length> 0)
{
saveFileDialog1.ShowDialog ();
DialogResult dr = new DialogResult ();
if (dr == DialogResult.OK)
{
string filename = saveFileDialog1.FileName;
StreamWriter sw = new StreamWriter (filename);
sw.Write (textBox1.Text);
sw.Close ();
}
else
{
textBox1.Text = "";
}
}
}
Windows Form form application, build a notepad reference code that focuses on open, save, and Save As