新手一个问题
[code=C#][/code]
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.IO;
namespace openfile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog1.FileName != "")
{
StreamWriter a = new StreamWriter(openFileDialog1.FileName);
a.Write(richTextBox1.Text);
a.Close();
// openFileDialog1 = richTextBox1.Text;
}
else
{
// saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
StreamWriter a = new StreamWriter(saveFileDialog1.FileName, true);
a.WriteLine(richTextBox1.Text);
a.Close();
// richTextBox1.Text = string.Empty;
}
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
// public static CreateAccessibilityInstance();
// richTextBox1.Text = string.Empty;
// FileInfo crea = new FileInfo("D:\\1.txt");
// crea.Create();
richTextBox1.Text = "";
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.Text = string.Empty;
StreamReader b = new StreamReader(openFileDialog1.FileName);
richTextBox1.Text = b.ReadToEnd();
b.Close();
}
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Text == string.Empty)
{
MessageBox.Show("不能输入为空");
}
else
{
saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
}
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamWriter a = new StreamWriter(saveFileDialog1.FileName, true);
a.WriteLine(richTextBox1.Text);
a.Close();
// richTextBox1.Text = string.Empty;
}
}
}
}
[code=C#][/code]
主要是“保存”功能我很疑惑 使用if (openFileDialog1.FileName != "")这个条件语句,“保存”功能正常使用
如果使用 if (openFileDialog1.ShowDialog() == DialogResult.OK)或者 if (saveFileDialog1.ShowDialog() == DialogResult.OK),功能就变成了“另存为”,无法