这是一次课后小作业, 给我们班的混子们看下,看可以不要直接抄,变量名改下啊。
using System.Text;
namespace WinFormsApp2caidan
{
public partial class Form1 : Form
{
string file;
public Form1()
{
InitializeComponent();
}
private void 新建菜单ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 xj = new Form2();
xj.Show();
}
private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = true;//该值确定是否可以选择多个文件
dialog.Title = "请选择文件";
dialog.Filter = "txt格式(*.txt)|*.txt|所有文件|*.*";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
file = dialog.FileName;
StreamReader sread = new StreamReader(file, Encoding.UTF8);
string strline = string.Empty;
richTextBox1.Text = "";
while ((strline = sread.ReadLine()) != null)
{
richTextBox1.Text += strline;
}
sread.Close();
}
}
private void 新建文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 保存文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
using (StreamWriter sWrite = new StreamWriter(file, false))
{
string nr = richTextBox1.Text;
sWrite.WriteLine(nr);
}
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (File.Exists(file))
{
File.Delete(file);
}
richTextBox1.Text = "";
}
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Copy();
}
private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Cut();
}
private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}
}
}
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;
namespace WinFormsApp2caidan
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string fname = textBox1.Text;
if (!File.Exists(fname)){
File.Create(fname);
}
if (File.Exists(fname))
{
MessageBox.Show("新建文件成功", "新建", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
运行截图;