C# C中filemanager的问题
我需要写些什么才能让它工作 如果文本窗口中的当前文本未保存或自打开后未更改,并且您尝试关闭、打开或创建新文件,则应询问用户是否希望在打开新文件/新文档之前保存。答案选项应为是、否或取消。如果案文没有改变,就不应提出任何问题 如果文本已更改,则应在窗口标题中用星号*表示,例如File.txt* 此外,您应该能够在当前文件名下使用save进行保存。 和另存为以创建新文件 最后:关闭当前视图的可能性也可以是打开的文件 这是我的密码:C# C中filemanager的问题,c#,file,dialog,filenames,word-processor,C#,File,Dialog,Filenames,Word Processor,我需要写些什么才能让它工作 如果文本窗口中的当前文本未保存或自打开后未更改,并且您尝试关闭、打开或创建新文件,则应询问用户是否希望在打开新文件/新文档之前保存。答案选项应为是、否或取消。如果案文没有改变,就不应提出任何问题 如果文本已更改,则应在窗口标题中用星号*表示,例如File.txt* 此外,您应该能够在当前文件名下使用save进行保存。 和另存为以创建新文件 最后:关闭当前视图的可能性也可以是打开的文件 这是我的密码: namespace filemanager { public
namespace filemanager
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void closeFile_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length != 0)
{
DialogResult answr;
answr = MessageBox.Show("Do you want to save your file before closing?", "Exit", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (answr == DialogResult.No)
{
textBox1.Clear();
Application.ExitThread();
}
if (answr == DialogResult.Yes)
{
saveFile_Click(this, e);
}
}
}
private void openFile_Click(object sender, EventArgs e)
{
openFileFunc.Title = "Open file";
openFileFunc.InitialDirectory = "C:";
openFileFunc.FileName = "";
openFileFunc.Filter = "Text Document|*.txt|Word Documents|*.doc";
string Chosen_File = "";
if (openFileFunc.ShowDialog() != DialogResult.Cancel)
{
Chosen_File = openFileFunc.FileName;
textBox1.LoadFile(Chosen_File, RichTextBoxStreamType.PlainText);
}
}
private void saveFile_Click(object sender, EventArgs e)
{
saveFileFunc.Title = "Save file";
saveFileFunc.InitialDirectory = "C:";
saveFileFunc.FileName = "";
saveFileFunc.Filter = "Text Document|*.txt|Word Documents|*.doc";
string Save_File = "";
if (saveFileFunc.ShowDialog() != DialogResult.Cancel)
{
Save_File = saveFileFunc.FileName;
textBox1.SaveFile(Save_File, RichTextBoxStreamType.PlainText);
}
}
private void saveAs_Click(object sender, EventArgs e)
{
saveFileFunc.Title = "Save file";
saveFileFunc.InitialDirectory = "C:";
saveFileFunc.FileName = "";
saveFileFunc.Filter = "Text Document|*.txt|Word Documents|*.doc";
string Save_File = "";
if (saveFileFunc.ShowDialog() != DialogResult.Cancel)
{
Save_File = saveFileFunc.FileName;
textBox1.SaveFile(Save_File, RichTextBoxStreamType.PlainText);
}
}
}
}
这段代码将为您指明正确的方向。我把最后定稿留给你
// flag for holding the Dirty state of the textbox
private bool IsDirty = false;
// the setter handles the showing or no showing of a * in the title
private bool Dirty
{
set
{
IsDirty = value;
this.Text = "TextProcessor " + (IsDirty ? "*" : "");
}
}
// hookup textChanged on the (rich)textBox to trigger the Dirty flag
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
Dirty = true;
}
// hookup the formclosing event to Cancel the closing of the form.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (IsDirty)
{
switch (MessageBox.Show("save?", "saving", MessageBoxButtons.YesNoCancel))
{
case DialogResult.Cancel:
case DialogResult.Yes:
e.Cancel = true;
break;
default:
break;
}
}
}
你会找到足够的关键词来搜索更多细节。那么你的问题是什么?哪种代码不起作用?哈哈,哎呀,我把自己的措辞弄错了。我甚至不知道怎么写。我已经试过很多次了,但我所做的只是把我的代码弄乱了。谢谢!:这太有用了!