谁有用c#编写的记事本代码
2个回答
展开全部
/宏灶/系统剪切板的操作
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.textBox1.SelectedText != "")
{
Clipboard.SetDataObject(this.textBox1.SelectedText);
}
else
MessageBox.Show("没有选中文字!");
}
private void 粘帖ToolStripMenuItem_Click(object sender, EventArgs e)
{
IDataObject iData = Clipboard.GetDataObject();
//检测数据是否是可以使用的格式,即文本格式
if (iData.GetDataPresent(DataFormats.Text))
{
if (this.textBox1.SelectedText != "")
{
textBox1.SelectedText = (String)iData.GetData(DataFormats.Text);
}
else
{
textBox1.Text += (String)iData.GetData(DataFormats.Text);
}
}
else
{
MessageBox.Show("没有从剪切板中接雹绝樱收到数据!");
}
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.textBox1.SelectedText = "";
}
private void 剪切XToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.textBox1.SelectedText != "")
{
Clipboard.SetDataObject(this.textBox1.SelectedText);
this.textBox1.SelectedText = "";
}
else
MessageBox.Show("没有选中文字!");
}
//关闭之前看文件是否保存
private void MyNotePad_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.textBox1.Text != "")
{
if (!File.Exists(this.Text))
{
DialogResult d = MessageBox.Show("文件 "源丛 + this.Text + @" 的文字已经改变。
想保存文件吗?", "MyNotePad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
switch (d)
{
case DialogResult.Yes:
using (SaveFileDialog saveDig = new SaveFileDialog())
{
saveDig.Filter = @"文本文档(*.txt)|*.txt";
saveDig.FileName = "*.txt";
if (saveDig.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(saveDig.FileName, false, System.Text.Encoding.Default);
sw.Write(this.textBox1.Text);
sw.Close();
this.Text = saveDig.FileName;
}
}
break;
case DialogResult.No:
break;
case DialogResult.Cancel:
break;
default: System.Diagnostics.Debug.Assert(false);
break;
}
}
}
}
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.textBox1.SelectedText != "")
{
Clipboard.SetDataObject(this.textBox1.SelectedText);
}
else
MessageBox.Show("没有选中文字!");
}
private void 粘帖ToolStripMenuItem_Click(object sender, EventArgs e)
{
IDataObject iData = Clipboard.GetDataObject();
//检测数据是否是可以使用的格式,即文本格式
if (iData.GetDataPresent(DataFormats.Text))
{
if (this.textBox1.SelectedText != "")
{
textBox1.SelectedText = (String)iData.GetData(DataFormats.Text);
}
else
{
textBox1.Text += (String)iData.GetData(DataFormats.Text);
}
}
else
{
MessageBox.Show("没有从剪切板中接雹绝樱收到数据!");
}
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.textBox1.SelectedText = "";
}
private void 剪切XToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.textBox1.SelectedText != "")
{
Clipboard.SetDataObject(this.textBox1.SelectedText);
this.textBox1.SelectedText = "";
}
else
MessageBox.Show("没有选中文字!");
}
//关闭之前看文件是否保存
private void MyNotePad_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.textBox1.Text != "")
{
if (!File.Exists(this.Text))
{
DialogResult d = MessageBox.Show("文件 "源丛 + this.Text + @" 的文字已经改变。
想保存文件吗?", "MyNotePad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
switch (d)
{
case DialogResult.Yes:
using (SaveFileDialog saveDig = new SaveFileDialog())
{
saveDig.Filter = @"文本文档(*.txt)|*.txt";
saveDig.FileName = "*.txt";
if (saveDig.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(saveDig.FileName, false, System.Text.Encoding.Default);
sw.Write(this.textBox1.Text);
sw.Close();
this.Text = saveDig.FileName;
}
}
break;
case DialogResult.No:
break;
case DialogResult.Cancel:
break;
default: System.Diagnostics.Debug.Assert(false);
break;
}
}
}
}
2010-11-30
展开全部
简单代码,关闭后自动保存记事瞎隐本陵神冲
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(this.openFileDialog1.FileName,FileMode.Open,FileAccess.Read,FileShare.None);
StreamReader sr = new StreamReader(fs,Encoding.Default);
try
{
this.textBox1.Text = sr.ReadToEnd();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
fs.Close();
sr.Close();
}
}
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(this.saveFileDialog1.FileName,FileMode.CreateNew,FileAccess.Write);
StreamWriter sw = new StreamWriter(fs,Encoding.Default);
try
{
sw.Write(this.textBox1.Text);
MessageBox.Show("保存成功");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sw.Close();
}
}
}
private void 退出尺歼ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.fontDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.Font = this.fontDialog1.Font;
this.textBox1.ForeColor = this.fontDialog1.Color;
}
}
private void 前景色ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.colorDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.ForeColor = this.colorDialog1.Color;
}
}
private void 背景色ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.colorDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.BackColor = this.colorDialog1.Color;
}
}
//查找下一个
private void toolStripButton1_Click(object sender, EventArgs e)
{
int findText = 0;
try
{
findText = this.textBox1.SelectionStart + this.textBox1.SelectionLength;
if ((findText = textBox1.Text.IndexOf(toolStripTextBox1.Text, findText)) == -1)
{
MessageBox.Show("下面没有\"" + this.toolStripTextBox1.Text + "\"", "注意", MessageBoxButtons.OK, MessageBoxIcon.Information);
findText = textBox1.Text.Length;
}
else
{
textBox1.Select(findText,toolStripTextBox1.Text.Length);
textBox1.Focus();
findText += toolStripTextBox1.Text.Length;
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
字体ToolStripMenuItem_Click(sender,e);
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
前景色ToolStripMenuItem_Click(sender,e);
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
背景色ToolStripMenuItem_Click(sender,e);
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
this.textBox1.Text += System.Environment.NewLine + string.Format("智方在线 时间:{0}",DateTime.Now);
}
private void toolStripButton6_Click(object sender, EventArgs e)
{
this.Close();
}
private void frmNotePad_Activated(object sender, EventArgs e)
{
try
{
StreamReader sr = new StreamReader("Text.txt",Encoding.Default,false);
textBox1.Text = sr.ReadToEnd();
sr.Close();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}
}
private void frmNotePad_Deactivate(object sender, EventArgs e)
{
StreamReader sr = new StreamReader("text.txt",Encoding.Default);
string st = sr.ReadToEnd();
try
{
if (st.Length == textBox1.Text.Length)
{
sr.Close();
StreamWriter sw = new StreamWriter("text.txt", false, Encoding.Default);
sw.Write(textBox1.Text);
sw.Flush();
sw.Close();
}
else
{
sr.Close();
StreamWriter sw = new StreamWriter("text.txt",false,Encoding.Default);
sw.Write(textBox1.Text);
sw.Flush();
sw.Close();
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sr.Close();
}
}
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(this.openFileDialog1.FileName,FileMode.Open,FileAccess.Read,FileShare.None);
StreamReader sr = new StreamReader(fs,Encoding.Default);
try
{
this.textBox1.Text = sr.ReadToEnd();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
fs.Close();
sr.Close();
}
}
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(this.saveFileDialog1.FileName,FileMode.CreateNew,FileAccess.Write);
StreamWriter sw = new StreamWriter(fs,Encoding.Default);
try
{
sw.Write(this.textBox1.Text);
MessageBox.Show("保存成功");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sw.Close();
}
}
}
private void 退出尺歼ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.fontDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.Font = this.fontDialog1.Font;
this.textBox1.ForeColor = this.fontDialog1.Color;
}
}
private void 前景色ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.colorDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.ForeColor = this.colorDialog1.Color;
}
}
private void 背景色ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.colorDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.BackColor = this.colorDialog1.Color;
}
}
//查找下一个
private void toolStripButton1_Click(object sender, EventArgs e)
{
int findText = 0;
try
{
findText = this.textBox1.SelectionStart + this.textBox1.SelectionLength;
if ((findText = textBox1.Text.IndexOf(toolStripTextBox1.Text, findText)) == -1)
{
MessageBox.Show("下面没有\"" + this.toolStripTextBox1.Text + "\"", "注意", MessageBoxButtons.OK, MessageBoxIcon.Information);
findText = textBox1.Text.Length;
}
else
{
textBox1.Select(findText,toolStripTextBox1.Text.Length);
textBox1.Focus();
findText += toolStripTextBox1.Text.Length;
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
字体ToolStripMenuItem_Click(sender,e);
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
前景色ToolStripMenuItem_Click(sender,e);
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
背景色ToolStripMenuItem_Click(sender,e);
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
this.textBox1.Text += System.Environment.NewLine + string.Format("智方在线 时间:{0}",DateTime.Now);
}
private void toolStripButton6_Click(object sender, EventArgs e)
{
this.Close();
}
private void frmNotePad_Activated(object sender, EventArgs e)
{
try
{
StreamReader sr = new StreamReader("Text.txt",Encoding.Default,false);
textBox1.Text = sr.ReadToEnd();
sr.Close();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}
}
private void frmNotePad_Deactivate(object sender, EventArgs e)
{
StreamReader sr = new StreamReader("text.txt",Encoding.Default);
string st = sr.ReadToEnd();
try
{
if (st.Length == textBox1.Text.Length)
{
sr.Close();
StreamWriter sw = new StreamWriter("text.txt", false, Encoding.Default);
sw.Write(textBox1.Text);
sw.Flush();
sw.Close();
}
else
{
sr.Close();
StreamWriter sw = new StreamWriter("text.txt",false,Encoding.Default);
sw.Write(textBox1.Text);
sw.Flush();
sw.Close();
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sr.Close();
}
}
}
参考资料: http://www.cnfxinfo.com/m/setup.exe
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询