记事本编写的两个问题请教

李少1991 2013-03-27 10:40:02
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.Diagnostics;
using System.IO;
using System.Drawing.Printing;

namespace WindowsFormsApplication1
{
public partial class mytext : Form
{
public mytext()
{
InitializeComponent();
}



private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{ //文件菜单 新建功能

if (textBox1.Modified)
{//判断文件内容是否改动
DialogResult result =
MessageBox.Show("文本内容已经改变,是否保存?", "记事本",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);

switch (result)
{
case DialogResult.Yes:
保存ToolStripMenuItem1_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 = "无标题 - 记事本";
}



}
//文件菜单 保存///
private void 保存ToolStripMenuItem1_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "文本文件(*.txt)|*.txt|All files(*.*)|*.*";//设置文本文件或所有文件
///打开对话框
if (sfd.ShowDialog() == DialogResult.OK)
{
///打开文件,并获取文件流
StreamWriter sw =
new StreamWriter(sfd.FileName);//创建写文件流
sw.Write(textBox1.Text);//把编辑内容写入文件流中
sw.Close();
FileInfo fileInfo = new FileInfo(sfd.FileName);
this.Text = fileInfo.Name + " - 记事本";

}

}
//文件菜单 打开
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (textBox1.Modified)
{//判断文件内容是否改动
DialogResult result =
MessageBox.Show("文本内容已经改变,是否保存?", "记事本",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);

switch (result)
{
case DialogResult.Yes:
保存ToolStripMenuItem1_Click(sender, e);
textBox1.Clear();
this.Text = "新建 - 记事本";
break;
case DialogResult.No:
textBox1.Clear();
this.Text = "新建 - 记事本";
break;
case DialogResult.Cancel:
break;

}
}
else
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "文本文件(*.txt)|*.txt|All files(*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
this.Text = ofd.FileName + "记事本";
StreamReader sr = new StreamReader(ofd.FileName, System.Text.Encoding.Default);//把指定文件里的内容写到文件流中
textBox1.Text = sr.ReadToEnd();//把文件流中的内容写到编辑板中
sr.Close();

}

}
}
//文件菜单 另存为
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sf=new SaveFileDialog ();
sf.Filter = "文本文件(*.txt)|*.txt|All files(*.*)|*.*";//设置文本文件或所有文件
if(sf.ShowDialog() ==DialogResult .OK )
{
StreamWriter sw = new StreamWriter(sf.FileName );
sw.Write(textBox1.Text);
sw.Close();


}
}

private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
//编辑下的显示

private void 编辑ToolStripMenuItem_Click_1(object sender, EventArgs e)
{
if (textBox1.SelectionLength > 0)
{
//判断是否选中字符
cut.Enabled = true;
copy.Enabled = true;
del.Enabled = true;
}
else
{
cut.Enabled = false;
copy.Enabled = false;
del.Enabled = false;
}
if (textBox1.CanUndo) //对撤销控件的操作
{
cancle.Enabled = true;
}
else
cancle.Enabled = false;
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text)) //判断剪切板
paste.Enabled = true;
else
paste.Enabled = false;

}
//编辑菜单 撤销
private void cancle_Click(object sender, EventArgs e)
{
if (cancle.Enabled ==true)
{
textBox1.Undo();
textBox1 .ClearUndo ();

}
}
//编辑菜单 剪切
private void cut_Click(object sender, EventArgs e)
{
if (cut.Enabled == true)
textBox1.Cut();
}
//编辑菜单 复制
private void copy_Click(object sender, EventArgs e)
{
if (copy.Enabled == true)
textBox1.Copy();
}
//编辑菜单 粘贴
private void paste_Click(object sender, EventArgs e)
{
if (paste.Enabled == true)
textBox1.Paste();
}
//编辑菜单 删除
private void del_Click(object sender, EventArgs e)
{
if (del.Enabled == true)
textBox1.SelectedText = "";
}
//编辑菜单 全选
private void 全选ToolStripMenuItem1_Click(object sender, EventArgs e)
{
textBox1.SelectAll();
全选ToolStripMenuItem1.Enabled = false;
}
//编辑菜单 时间/日期
private void 日期ToolStripMenuItem_Click(object sender, EventArgs e)
{
DateTime dt = DateTime .Now ;
toolStripStatusLabel1.Text = dt.ToString();

}

//格式菜单 自动换行
private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
{
自动换行ToolStripMenuItem.Checked = !自动换行ToolStripMenuItem.Checked;//判断自动换行菜单是否被选中
if (自动换行ToolStripMenuItem.Checked == true)
textBox1.WordWrap = true;
else
textBox1.WordWrap = false;

}
//格式菜单 字体
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
//打开字体对话框
if (fd.ShowDialog() == DialogResult.OK)
textBox1.Font = fd.Font;
}
//关闭窗口时
private void mytext_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (textBox1.Modified)
{
DialogResult result = MessageBox.Show("文件 " + this.Text.Substring(0, this.Text.Length - 6) + " 被修改过,是否需要保存?", "记事本", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (result == DialogResult.Cancel)
e.Cancel = true;
if (result == DialogResult.Yes)
保存ToolStripMenuItem1_Click(sender, e);
}


}

private void 状态ToolStripMenuItem_Click(object sender, EventArgs e)
{
int total = textBox1.GetLineFromCharIndex(textBox1.Text.Length) + 1;//计算文本总行数
int index = textBox1.GetFirstCharIndexOfCurrentLine();//得到当前行第一个字符的索引
int line = textBox1.GetLineFromCharIndex(index) + 1;//得到当前的行数
int col = textBox1.SelectionStart - index + 1;//前行第一个字符的索引 = 光标所在的列数(从0开始)
toolStripStatusLabel3.Text = "第" + line + "行" + " " + "第" + col + "列" + " " + "共有:" + total + "行";
//行列数居右显示
statusStrip1.LayoutStyle = ToolStripLayoutStyle.HorizontalStackWithOverflow;
toolStripStatusLabel3.Alignment = ToolStripItemAlignment.Right;

}













}
}

两个问题1点红色关闭窗口时,好像没有运行那段代码
2 我代码里并没有实现右击显示上下文菜单,可程序有这个功能,就是能右击复制粘贴。怀疑是windows自带的文本软件影响。
是菜鸟,自学的 求大神帮助。
...全文
64 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
在Closing事件哪里写个MessageBox就知道究竟有没进入到里面了
天涯洪七公 2013-03-27
  • 打赏
  • 举报
回复
代码真心长啊,帮顶一下

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧