您的位置:首页 > 其它

【Form窗体】文件操作完成记事本功能

2013-03-23 20:59 274 查看
对于 文件操作常用的就是文件读写,文件打开,文件保存等本文将制作一个记事本,完成基本的文件操作。并对常用控件包括对话框和菜单以及状态栏综合运用。

下面谈下设计思想,然后再看具体实现。(部分功能代码参考网络资源)

在布局上样式和计算机自带记事本功能都是一致的,这里难点和关键点就是逻辑方面的判断。剪切复制等可用不可用逻辑问题。以及何时保存,还有就是打印等需要一些API(自带方法)。现在把整个界面布局粘贴如下:(下面红色为打印所需对话框)

View Code

public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
//窗体加载
private void MainForm_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;

this.time.Text = DateTime.Now.ToString();
}
/// 保存文件
void SaveText()
{
try
{
saveFileDialog1.FileName = "";
saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string MyFileName = saveFileDialog1.FileName;
StreamWriter sw = new StreamWriter(MyFileName, true, Encoding.Default);
sw.WriteLine(rtxContent.Text);

this.Text = MyFileName.Substring(MyFileName.LastIndexOf("\\") + 1);
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{ }
}
//另存为
private void HoldFile()
{
saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(saveFileDialog1.FileName, false);
sw.WriteLine(rtxContent.Text);
sw.Close();
MessageBox.Show("文件保存成功!", "温馨提示");
}
// 判断是第一次保存还是第二次
if (saveFileDialog1.Equals(""))
{
FileInfo fileInfo = new FileInfo(saveFileDialog1.FileName);
Text = fileInfo.Name + "-记事本";
saveFileDialog1.FileName = fileInfo.Name;
}
else
{
// 把标题改为打开的文件的名称
Text = saveFileDialog1.FileName + "-Mickey记事本";
}
}
//新建
private void FileMenuNew_Click(object sender, EventArgs e)
{
if (rtxContent.Text != "")
{
if (MessageBox.Show("是否保存当前文件?", "温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)== DialogResult.Yes)
{
// 保存文件
SaveText();
Text = "新建-Mickey记事本";
rtxContent.Text = "";
}
else if (MessageBox.Show("是否保存当前文件?", "温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)== DialogResult.No)
{
// 用户选择不保存时将输入框中的内容清除
rtxContent.Text = "";
}
}
}
//打开文件
private void FileMenuOpen_Click(object sender, EventArgs e)
{
try
{
if (rtxContent.Text != "")
{
SaveText(); //检查是否保存当前文件
}
else
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFileDialog1.FileName);
rtxContent.Text = sr.ReadToEnd();
sr.Close();
FileInfo fileInfo = new FileInfo(openFileDialog1.FileName);
// 把标题改为打开的文件的名称
Text = "*" + fileInfo.Name + "-白宁超记事本";
openFileDialog1.FileName = fileInfo.Name;
}
}
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
finally { }
}
//保存
private void FileMenuSave_Click(object sender, EventArgs e)
{
SaveText();
}
//另存为
private void FileMenuLingcun_Click(object sender, EventArgs e)
{
HoldFile();
}
//关闭
private void FileMenuClose_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("是否将更改保存?", "温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
SaveText();
Application.Exit();
}
else if (result == DialogResult.No)
{
Application.Exit();
}
}
private string selectedInfo = "";//定义选中文本
private void 编辑EToolStripMenuItem_Click(object sender, EventArgs e)
{
if ((rtxContent.SelectedText.Equals("")) && (rtxContent.Equals("")))
{
FileMenuCut.Enabled = false;
FileMenuCopy.Enabled = false;
FileMenuPaste.Enabled = false;
FileMenuDelte.Enabled = false;
}
else
{
FileMenuCut.Enabled = true;
FileMenuCopy.Enabled = true;
FileMenuPaste.Enabled = true;
FileMenuDelte.Enabled = true;
}
}
//复制
private void FileMenuCopy_Click(object sender, EventArgs e)
{
rtxContent.Copy();
}
//剪切
private void FileMenuCut_Click(object sender, EventArgs e)
{
selectedInfo = rtxContent.SelectedText;
rtxContent.Cut();
rtxContent.SelectedText = "";
}
//粘贴
private void FileMenuPaste_Click(object sender, EventArgs e)
{
rtxContent.Paste();
}
//删除
private void FileMenuDelte_Click(object sender, EventArgs e)
{
rtxContent.Text = "";
}
//撤销
private void FileMenuCancel_Click(object sender, EventArgs e)
{
rtxContent.Undo();
}
//选择全部
private void FileMenuAll_Click(object sender, EventArgs e)
{
rtxContent.SelectAll();

}
//显示时间
private void FileMenuTime_Click(object sender, EventArgs e)
{
rtxContent.AppendText(DateTime.Now.ToString());
}
//设置字体
private void FileMenuFont_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
rtxContent.Font = fontDialog1.Font;
}
}
//设置颜色
private void FileMenuColor_Click(object sender, EventArgs e)
{
if ( colorDialog1.ShowDialog() == DialogResult.OK)
{
rtxContent.ForeColor = colorDialog1.Color;
}
}
//页面设置
private void FileMenuPage_Click(object sender, EventArgs e)
{
this.pageSetupDialog1.Document = this.printDocument1;
pageSetupDialog1.ShowDialog();
}
//打印
private void FileMenuDY_Click(object sender, EventArgs e)
{
if (rtxContent.Text.Length < 1)
{
MessageBox.Show("请确保要打印的文件的内容不为空!", "温馨提示");
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 FileMenuBr_Click_1(object sender, EventArgs e)
{
if (FileMenuBr.Checked)
{
rtxContent.WordWrap = true;
}
else
{
rtxContent.WordWrap = false;
}
}
//状态栏是否选择
private void FileMenuState_Click(object sender, EventArgs e)
{
statusStrip1.Visible = FileMenuState.Checked;
}
//多窗体水平显示
private void FileMenuH_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileHorizontal);
}
//多窗体垂直显示
private void FileMenuV_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileVertical);
}
//多窗体层叠显示
private void FileMenuC_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.Cascade);
}
//帮助
private void FileMenuHelp_Click(object sender, EventArgs e)
{
Form2 f2=new Form2();
f2.Show();
}
//根据文本变化而状态变化
private void rtxContent_TextChanged(object sender, EventArgs e)
{
this.total.Text = "当前计数" + this.rtxContent.Text.Length.ToString();
}

}


运行结果:

1,打开文件



2,设置字体大小-》查看帮助-》保存成功(查看帮助是另外打开的)



4,状态栏变化

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐