当我点击我有新文件的文件下拉列表时,对于新文件,我提示用户。如果他们在文本框中有数据,他们是否想要在开始新文件之前保存他们所做的事情。我得到了保存提示对话框以弹出所有但我不会保存文件。
private void mnuFileNew_Click(object sender, EventArgs e)
{
if (txtText.Text.Trim().Length <= 0)
{
lblStatus.ForeColor = Color.BlueViolet;
lblStatus.Text = "New file!";
}
else
{
if (MessageBox.Show("Start new file without saving?", "Exit",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
{
try
{
SaveFileDialog sfd = new SaveFileDialog();
// set properties
sfd.Title = "Where would you like to save this?";
sfd.InitialDirectory = @"c:\Users\public";
sfd.Filter = "Text File (*.txt)|*.txt|All files (*.*)|*.*";
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
lblStatus.Text = sfd.FileName;
lblStatus.ForeColor = Color.Navy;
}
// show a message
lblStatus.ForeColor = Color.BlueViolet;
lblStatus.Text = "The Text has been uploaded!";
}
catch (Exception ex)
{
lblStatus.ForeColor = Color.Red;
lblStatus.Text = ex.Message;
}
}
else
{
txtText.Text = "";
}
答案 0 :(得分:0)
您需要创建文件
if(sfd.ShowDialog() == DialogResult.OK)
{
using(StreamWriter sw = new StreamWriter(sfd.FileName))
{
sw.WriteLine(YourTextBox.Text);
}
}