十万火急!!用C#编写记事本,这是期末要交的作业,但是我不会呀,哪为编成高手快帮帮我吧!
嘿嘿~~不好意思哈,这门课程我基本上课都没听,所以说是一窍不通,只会打开VisualStudio2005后点了windows应用程序出现的哪有form1.csde页面,具...
嘿嘿~~不好意思哈,这门课程我基本上课都没听,所以说是一窍不通,只会打开Visual Studio 2005后点了windows应用程序出现的哪有form1.csde 页面,具体接下来怎么操作我都不知道,请朋友们除了给我详细代码再说说怎么操作点出贴代码的页面,越详细越好哈。拜托了~~喔,还有一点老师叫我们交的时候说什么在那什么版权那注明证实是自己的作业,这又该怎么弄呀,也具体说说啊~~大家多多帮忙啊,搞定了的话除了给的100分还会另加给分的。就把代码放在回答页面也行发到我邮箱也行,1363291030@qq.com
展开
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Mickey记事本
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 用于存储当前操作的文件的名称
private string textFileName = "";
private string filePath = "";
private void 新建_Click(object sender, EventArgs e)
{
textFileName = "";
// 新建一个文本时,若输入框中的内容不为空,应先提示“是否保存”
if (inputInfo.Text != string.Empty)
{
// 若用户选择“是”,应弹出保存文件的对话框
if (MessageBox.Show("是否保存当前文件?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)
== DialogResult.Yes)
{
// 保存文件
Save();
Text = "新建-Mickey记事本";
inputInfo.Text = "";
}
else if (MessageBox.Show("是否保存当前文件?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)
== DialogResult.No)
{
// 用户选择不保存时将输入框中的内容清除
inputInfo.Text = "";
}
}
}
private void 打开_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "文本文件(*.txt)|*.txt";
if (openFile.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFile.FileName);
inputInfo.Text = sr.ReadToEnd();
sr.Close();
FileInfo fileInfo = new FileInfo(openFile.FileName);
// 把标题改为打开的文件的名称
Text = "*" + fileInfo.Name + "-Mickey记事本";
textFileName = fileInfo.Name;
}
}
private void 保存_Click(object sender, EventArgs e)
{
Save();
}
// “保存”
private void Save()
{
if (!textFileName.Equals(""))
{
SaveFileDialog saveFile = new SaveFileDialog();
// 默认保存格式
saveFile.Filter = "文本文件(*.txt)|*.txt";
StreamWriter sw = new StreamWriter(filePath, false);
sw.Write(inputInfo.Text);
sw.Close();
MessageBox.Show("文件保存成功!", "Mickey温馨提示");
filePath = saveFile.FileName;
// 把标题改为打开的文件的名称
Text = textFileName + "-Mickey记事本";
}
else
{
// 成员变量为“”,说明文件第一次保存,执行“另存为”操作
HoldFile();
}
}
private void HoldFile()
{
// 若用户选择另保存文件
SaveFileDialog saveFile = new SaveFileDialog();
// 默认保存格式
saveFile.Filter = "文本文件(*.txt)|*.txt";
if (saveFile.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(saveFile.FileName, false);
sw.WriteLine(inputInfo.Text);
sw.Close();
MessageBox.Show("文件保存成功!", "Mickey温馨提示");
filePath = saveFile.FileName;
}
// 判断是第一次保存还是第二次
if (textFileName.Equals(""))
{
FileInfo fileInfo = new FileInfo(saveFile.FileName);
Text = fileInfo.Name + "-Mickey记事本";
textFileName = fileInfo.Name;
}
else
{
// 把标题改为打开的文件的名称
Text = textFileName + "-Mickey记事本";
}
}
private void 另存为_Click(object sender, EventArgs e)
{
HoldFile();
}
private void 页面设置_Click(object sender, EventArgs e)
{
this.pageSetupDialog1.Document = this.printDocument1;
pageSetupDialog1.ShowDialog();
}
private void 打印_Click(object sender, EventArgs e)
{
if (inputInfo.Text.Length < 1)
{
MessageBox.Show("请确保要打印的文件的内容不为空!", "Mickey温馨提示");
return;
}
else
{
// 设置Document的属性
this.printDialog1.Document = this.printDocument1;
this.printDialog1.PrinterSettings = this.pageSetupDialog1.PrinterSettings;
if (this.printDialog1.ShowDialog() == DialogResult.OK)
{
try
{
this.printDocument1.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void fileNew_Click(object sender, EventArgs e) { if (textBox1.Modified == true) { DialogResult re = MessageBox.Show("文件" + Text + "的内容已改变,需要保存吗?", "保存文件", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); switch (re) { case DialogResult.Yes: this.fileSave_Click(sender, e); textBox1.Clear(); this.Text = "新建"; break; case DialogResult.No: textBox1.Clear(); this.Text = "新建"; break; case DialogResult.Cancel: break; } } else { textBox1.Clear(); this.Text = "新建"; textBox1.Modified = false; } } private void fileSave_Click(object sender, EventArgs e) { saveFileDialog1.Title = "保存"; saveFileDialog1.Filter = "文本文件|*.txt|Word文档|*.doc|网页文件|*.html|所有文件|*.*"; saveFileDialog1.FileName = ""; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName)) { sw.Write(textBox1); sw.Close(); textBox1.Modified = false; this.Text =Path.GetFileNameWithoutExtension(saveFileDialog1.FileName) +"记事本"; } } }
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Mickey记事本
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 用于存储当前操作的文件的名称
private string textFileName = "";
private string filePath = "";
private void 新建_Click(object sender, EventArgs e)
{
textFileName = "";
// 新建一个文本时,若输入框中的内容不为空,应先提示“是否保存”
if (inputInfo.Text != string.Empty)
{
// 若用户选择“是”,应弹出保存文件的对话框
if (MessageBox.Show("是否保存当前文件?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)
== DialogResult.Yes)
{
// 保存文件
Save();
Text = "新建-Mickey记事本";
inputInfo.Text = "";
}
else if (MessageBox.Show("是否保存当前文件?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)
== DialogResult.No)
{
// 用户选择不保存时将输入框中的内容清除
inputInfo.Text = "";
}
}
}
private void 打开_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "文本文件(*.txt)|*.txt";
if (openFile.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFile.FileName);
inputInfo.Text = sr.ReadToEnd();
sr.Close();
FileInfo fileInfo = new FileInfo(openFile.FileName);
// 把标题改为打开的文件的名称
Text = "*" + fileInfo.Name + "-Mickey记事本";
textFileName = fileInfo.Name;
}
}
private void 保存_Click(object sender, EventArgs e)
{
Save();
}
// “保存”
private void Save()
{
if (!textFileName.Equals(""))
{
SaveFileDialog saveFile = new SaveFileDialog();
// 默认保存格式
saveFile.Filter = "文本文件(*.txt)|*.txt";
StreamWriter sw = new StreamWriter(filePath, false);
sw.Write(inputInfo.Text);
sw.Close();
MessageBox.Show("文件保存成功!", "Mickey温馨提示");
filePath = saveFile.FileName;
// 把标题改为打开的文件的名称
Text = textFileName + "-Mickey记事本";
}
else
{
// 成员变量为“”,说明文件第一次保存,执行“另存为”操作
HoldFile();
}
}
private void HoldFile()
{
// 若用户选择另保存文件
SaveFileDialog saveFile = new SaveFileDialog();
// 默认保存格式
saveFile.Filter = "文本文件(*.txt)|*.txt";
if (saveFile.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(saveFile.FileName, false);
sw.WriteLine(inputInfo.Text);
sw.Close();
MessageBox.Show("文件保存成功!", "Mickey温馨提示");
filePath = saveFile.FileName;
}
// 判断是第一次保存还是第二次
if (textFileName.Equals(""))
{
FileInfo fileInfo = new FileInfo(saveFile.FileName);
Text = fileInfo.Name + "-Mickey记事本";
textFileName = fileInfo.Name;
}
else
{
// 把标题改为打开的文件的名称
Text = textFileName + "-Mickey记事本";
}
}
private void 另存为_Click(object sender, EventArgs e)
{
HoldFile();
}
private void 页面设置_Click(object sender, EventArgs e)
{
this.pageSetupDialog1.Document = this.printDocument1;
pageSetupDialog1.ShowDialog();
}
private void 打印_Click(object sender, EventArgs e)
{
if (inputInfo.Text.Length < 1)
{
MessageBox.Show("请确保要打印的文件的内容不为空!", "Mickey温馨提示");
return;
}
else
{
// 设置Document的属性
this.printDialog1.Document = this.printDocument1;
this.printDialog1.PrinterSettings = this.pageSetupDialog1.PrinterSettings;
if (this.printDialog1.ShowDialog() == DialogResult.OK)
{
try
{
this.printDocument1.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void fileNew_Click(object sender, EventArgs e) { if (textBox1.Modified == true) { DialogResult re = MessageBox.Show("文件" + Text + "的内容已改变,需要保存吗?", "保存文件", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); switch (re) { case DialogResult.Yes: this.fileSave_Click(sender, e); textBox1.Clear(); this.Text = "新建"; break; case DialogResult.No: textBox1.Clear(); this.Text = "新建"; break; case DialogResult.Cancel: break; } } else { textBox1.Clear(); this.Text = "新建"; textBox1.Modified = false; } } private void fileSave_Click(object sender, EventArgs e) { saveFileDialog1.Title = "保存"; saveFileDialog1.Filter = "文本文件|*.txt|Word文档|*.doc|网页文件|*.html|所有文件|*.*"; saveFileDialog1.FileName = ""; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName)) { sw.Write(textBox1); sw.Close(); textBox1.Modified = false; this.Text =Path.GetFileNameWithoutExtension(saveFileDialog1.FileName) +"记事本"; } } }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询