c# 关闭窗口提示保存
做了个记事本但是窗口关闭时不提示是否保存代码如下bools==false是指未保存boolb==ture是指从磁盘读入privatevoidfrmNotepad_Form...
做了个记事本 但是窗口关闭时不提示是否保存 代码如下
bool s==false是指未保存
bool b==ture是指从磁盘读入
private void frmNotepad_FormClosing(object sender, FormClosingEventArgs e)
{
if (s == false)
{
string result;
result = MessageBox.Show("文件尚未保存,是否保存?",
"保存文件", MessageBoxButtons.YesNoCancel).ToString();
switch (result)
{
case "Yes":
if (b == true)
{
rtxtNotepad.SaveFile(odlgNotepad.FileName);
}
else if (sdlgNotepad.ShowDialog() == DialogResult.OK)
{
rtxtNotepad.SaveFile(sdlgNotepad.FileName);
}
rtxtNotepad.Text = "";
break;
case "No":
break;
}
Application.Exit();
}
else
{
Application.Exit();
}
}
这段代码错了嘛?还是别的代码问题? 展开
bool s==false是指未保存
bool b==ture是指从磁盘读入
private void frmNotepad_FormClosing(object sender, FormClosingEventArgs e)
{
if (s == false)
{
string result;
result = MessageBox.Show("文件尚未保存,是否保存?",
"保存文件", MessageBoxButtons.YesNoCancel).ToString();
switch (result)
{
case "Yes":
if (b == true)
{
rtxtNotepad.SaveFile(odlgNotepad.FileName);
}
else if (sdlgNotepad.ShowDialog() == DialogResult.OK)
{
rtxtNotepad.SaveFile(sdlgNotepad.FileName);
}
rtxtNotepad.Text = "";
break;
case "No":
break;
}
Application.Exit();
}
else
{
Application.Exit();
}
}
这段代码错了嘛?还是别的代码问题? 展开
2个回答
展开全部
呵呵。。当然错了喔。。比如一个按钮提示关闭窗口的:代码如下
DialogResult mess=MessageBox.Show("文件尚未保存,是否保存?",
"保存文件", MessageBoxButtons.YesNoCancel);
if (mess == DialogResult.OK)
{
this.Close();
}
else
{
//调用保存文件的方法
}
消息框的按钮消息是用DialogResult 接收的。。其他的么什么问题。。希望给你最给力的帮助! 觉得有用 。采纳喔。。呵呵
DialogResult mess=MessageBox.Show("文件尚未保存,是否保存?",
"保存文件", MessageBoxButtons.YesNoCancel);
if (mess == DialogResult.OK)
{
this.Close();
}
else
{
//调用保存文件的方法
}
消息框的按钮消息是用DialogResult 接收的。。其他的么什么问题。。希望给你最给力的帮助! 觉得有用 。采纳喔。。呵呵
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace myTxt
{
public partial class Form1 : Form
{
string text;//代表记事本里的内容
string path;//代表文件的路径
StringReader sr;
public Form1()
{
InitializeComponent();
text="";
path="";
sr = null;
}
//新建
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Text != text)//内容改变了
{
DialogResult result = MessageBox.Show("内容已经更改,是否保存?", "友情提示", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Cancel)//取消
{
return;
}
if (result == DialogResult.Yes)//如果选择是
{
//保存实现,调用保存方法
保存ToolStripMenuItem_Click(sender, e);
}
//实现清空[新建]
richTextBox1.Text = "";
text = "";
path = "";
}
else
{
//实现清空[新建]
richTextBox1.Text = "";
text = "";
path = "";
}
}
//打开
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Text != text)//已经更改了内容
{
DialogResult result = MessageBox.Show("内容已经更改,是否保存?", "友情提示", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Cancel)//取消
return;
if (result == DialogResult.Yes)
{
//保存实现,调用保存方法
保存ToolStripMenuItem_Click(sender, e);
}
}
//打开功能的实现
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
path=openFileDialog1.FileName;//取路径
richTextBox1.LoadFile(path, RichTextBoxStreamType.PlainText);
}
text = richTextBox1.Text;
}
//保存
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (path == "")//一个新文件
{
//保存的实现
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
path = saveFileDialog1.FileName;
richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
}
}
else//已经存在的文件
{
//保存的实现
richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
}
text = richTextBox1.Text;
}
//另存为
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
path=saveFileDialog1.FileName;
richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
}
text = richTextBox1.Text;
}
//撤消
private void 撤消ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Undo();
}
//剪切
private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Cut();
}
//复制
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Copy();
}
//粘贴
private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}
//删除
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
string str = richTextBox1.SelectedText;
if(str.Length>0)
richTextBox1.SelectedText="";
}
//查找
private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
{
find f = new find();
f.Owner = this;
f.Show();
}
//查找与替换
private void 查找与替换ToolStripMenuItem_Click(object sender, EventArgs e)
{
replace r = new replace();
r.Owner = this;
r.Show();
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Text != text)//内容改变了
{
DialogResult result = MessageBox.Show("内容已经更改,是否保存?", "友情提示", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Cancel)//取消
{
return;
}
if (result == DialogResult.Yes)//如果选择是
{
//保存实现,调用保存方法
保存ToolStripMenuItem_Click(sender, e);
//实现清空[新建]
richTextBox1.Text = "";
text = "";
path = "";
}
Application.Exit();
}
else
{
Application.Exit();
}
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
if(fontDialog1.ShowDialog()==DialogResult.OK)
{
richTextBox1.Font = fontDialog1.Font;
richTextBox1.ForeColor = fontDialog1.Color;
}
}
private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
pageSetupDialog1.Document = printDocument1;
pageSetupDialog1.ShowDialog();
}
private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument1;
if(printDialog1.ShowDialog()==DialogResult.OK)
{
printDocument1.Print();
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics g = e.Graphics;
Font f = richTextBox1.Font;
int rows = e.MarginBounds.Height / f.Height;//没页总行数
int strX = e.MarginBounds.Left;
int strY = e.MarginBounds.Top;
string str = null;//要打印的文本
int curRow = 0;//当前行
while ((curRow <= rows) && (str = sr.ReadLine()) != null)//如果还有行并且当前行数<每页总行数
{
g.DrawString(str, f, new SolidBrush(richTextBox1.ForeColor), new RectangleF(strX, strY, e.MarginBounds.Width, e.MarginBounds.Height));
strY += f.Height;//每画一行之后,新行的高度要改变
curRow++;
}
if (str != null)//如果
e.HasMorePages = true;
else
e.HasMorePages = false;
//checkPrint = richTextBox1.Print(checkPrint, richTextBox1.TextLength, e);
//if (checkPrint < richTextBox1.TextLength)
// e.HasMorePages = true;
//else
// e.HasMorePages = false;
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
sr = new StringReader(richTextBox1.Text);
printPreviewDialog1.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
if (richTextBox1.Text != text)//内容改变了
{
DialogResult result = MessageBox.Show("内容已经更改,是否保存?", "友情提示", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Cancel)//取消
{
return;
}
if (result == DialogResult.Yes)//如果选择是
{
//保存实现,调用保存方法
保存ToolStripMenuItem_Click(sender, e);
//实现清空[新建]
richTextBox1.Text = "";
text = "";
path = "";
}
Application.Exit();
}
else
{
Application.Exit();
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace myTxt
{
public partial class Form1 : Form
{
string text;//代表记事本里的内容
string path;//代表文件的路径
StringReader sr;
public Form1()
{
InitializeComponent();
text="";
path="";
sr = null;
}
//新建
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Text != text)//内容改变了
{
DialogResult result = MessageBox.Show("内容已经更改,是否保存?", "友情提示", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Cancel)//取消
{
return;
}
if (result == DialogResult.Yes)//如果选择是
{
//保存实现,调用保存方法
保存ToolStripMenuItem_Click(sender, e);
}
//实现清空[新建]
richTextBox1.Text = "";
text = "";
path = "";
}
else
{
//实现清空[新建]
richTextBox1.Text = "";
text = "";
path = "";
}
}
//打开
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Text != text)//已经更改了内容
{
DialogResult result = MessageBox.Show("内容已经更改,是否保存?", "友情提示", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Cancel)//取消
return;
if (result == DialogResult.Yes)
{
//保存实现,调用保存方法
保存ToolStripMenuItem_Click(sender, e);
}
}
//打开功能的实现
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
path=openFileDialog1.FileName;//取路径
richTextBox1.LoadFile(path, RichTextBoxStreamType.PlainText);
}
text = richTextBox1.Text;
}
//保存
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (path == "")//一个新文件
{
//保存的实现
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
path = saveFileDialog1.FileName;
richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
}
}
else//已经存在的文件
{
//保存的实现
richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
}
text = richTextBox1.Text;
}
//另存为
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
path=saveFileDialog1.FileName;
richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
}
text = richTextBox1.Text;
}
//撤消
private void 撤消ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Undo();
}
//剪切
private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Cut();
}
//复制
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Copy();
}
//粘贴
private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}
//删除
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
string str = richTextBox1.SelectedText;
if(str.Length>0)
richTextBox1.SelectedText="";
}
//查找
private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
{
find f = new find();
f.Owner = this;
f.Show();
}
//查找与替换
private void 查找与替换ToolStripMenuItem_Click(object sender, EventArgs e)
{
replace r = new replace();
r.Owner = this;
r.Show();
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Text != text)//内容改变了
{
DialogResult result = MessageBox.Show("内容已经更改,是否保存?", "友情提示", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Cancel)//取消
{
return;
}
if (result == DialogResult.Yes)//如果选择是
{
//保存实现,调用保存方法
保存ToolStripMenuItem_Click(sender, e);
//实现清空[新建]
richTextBox1.Text = "";
text = "";
path = "";
}
Application.Exit();
}
else
{
Application.Exit();
}
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
if(fontDialog1.ShowDialog()==DialogResult.OK)
{
richTextBox1.Font = fontDialog1.Font;
richTextBox1.ForeColor = fontDialog1.Color;
}
}
private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
pageSetupDialog1.Document = printDocument1;
pageSetupDialog1.ShowDialog();
}
private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument1;
if(printDialog1.ShowDialog()==DialogResult.OK)
{
printDocument1.Print();
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics g = e.Graphics;
Font f = richTextBox1.Font;
int rows = e.MarginBounds.Height / f.Height;//没页总行数
int strX = e.MarginBounds.Left;
int strY = e.MarginBounds.Top;
string str = null;//要打印的文本
int curRow = 0;//当前行
while ((curRow <= rows) && (str = sr.ReadLine()) != null)//如果还有行并且当前行数<每页总行数
{
g.DrawString(str, f, new SolidBrush(richTextBox1.ForeColor), new RectangleF(strX, strY, e.MarginBounds.Width, e.MarginBounds.Height));
strY += f.Height;//每画一行之后,新行的高度要改变
curRow++;
}
if (str != null)//如果
e.HasMorePages = true;
else
e.HasMorePages = false;
//checkPrint = richTextBox1.Print(checkPrint, richTextBox1.TextLength, e);
//if (checkPrint < richTextBox1.TextLength)
// e.HasMorePages = true;
//else
// e.HasMorePages = false;
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
sr = new StringReader(richTextBox1.Text);
printPreviewDialog1.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
if (richTextBox1.Text != text)//内容改变了
{
DialogResult result = MessageBox.Show("内容已经更改,是否保存?", "友情提示", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Cancel)//取消
{
return;
}
if (result == DialogResult.Yes)//如果选择是
{
//保存实现,调用保存方法
保存ToolStripMenuItem_Click(sender, e);
//实现清空[新建]
richTextBox1.Text = "";
text = "";
path = "";
}
Application.Exit();
}
else
{
Application.Exit();
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询