C# streamWrite的代码结构
有没有更好的方法可以让我流畅地编写代码,而不必为我按下的每个按钮重复代码?下面是我正在研究的一个示例,其中一个按钮执行自己的操作,并相应地写入元音,而另一个按钮执行相同的操作,只是它没有相应地写入字母字符:C# streamWrite的代码结构,c#,winforms,C#,Winforms,有没有更好的方法可以让我流畅地编写代码,而不必为我按下的每个按钮重复代码?下面是我正在研究的一个示例,其中一个按钮执行自己的操作,并相应地写入元音,而另一个按钮执行相同的操作,只是它没有相应地写入字母字符: private void btnVowels_Click(object sender, EventArgs e) { string wholeText = ""; string copyText = richTe
private void btnVowels_Click(object sender, EventArgs e)
{
string wholeText = "";
string copyText = richTextBox1.Text;
if (System.IO.File.Exists(Second_File) == true)
{
System.IO.StreamWriter objWriter;
objWriter = new System.IO.StreamWriter(Second_File);
string vowels = "AaEeIiOoUu";
copyText = new string(copyText.Where(c => !vowels.Contains(c)).ToArray());
wholeText = richTextBox1.Text + copyText;
objWriter.Write(wholeText);
richTextBox2.Text = wholeText;
objWriter.Close();
}
else
{
MessageBox.Show("No file named " + Second_File);
}
}
private void btnAlpha_Click(object sender, EventArgs e)
{
string wholeText = "";
string copyText = richTextBox1.Text;
if (System.IO.File.Exists(Second_File) == true)
{
System.IO.StreamWriter objWriter;
objWriter = new System.IO.StreamWriter(Second_File);
string nonAlpha = @"[^A-Za-z ]+";
string addSpace = "";
copyText = Regex.Replace(copyText, nonAlpha, addSpace);
objWriter.Write(wholeText);
richTextBox2.Text = wholeText;
objWriter.Close();
}
else
{
MessageBox.Show("No file named " + Second_File);
}
}
为什么不这样做呢
private void Write(string file, string text)
{
if (File.Exists(file))
{
using (StreamWriter objWriter = new StreamWriter(file))
{
objWriter.Write(text);
}
}
else
{
MessageBox.Show("No file named " + file);
}
}
private void btnAlpha_Click(object sender, EventArgs e)
{
string wholeText = "";
string copyText = richTextBox1.Text;
string nonAlpha = @"[^A-Za-z ]+";
string addSpace = "";
copyText = Regex.Replace(copyText, nonAlpha, addSpace);
wholeText = richTextBox1.Text + copyText;
Write(Second_File, wholeText); // same for the second button
richTextBox2.Text = wholeText;
}
为什么不这样做呢
private void Write(string file, string text)
{
if (File.Exists(file))
{
using (StreamWriter objWriter = new StreamWriter(file))
{
objWriter.Write(text);
}
}
else
{
MessageBox.Show("No file named " + file);
}
}
private void btnAlpha_Click(object sender, EventArgs e)
{
string wholeText = "";
string copyText = richTextBox1.Text;
string nonAlpha = @"[^A-Za-z ]+";
string addSpace = "";
copyText = Regex.Replace(copyText, nonAlpha, addSpace);
wholeText = richTextBox1.Text + copyText;
Write(Second_File, wholeText); // same for the second button
richTextBox2.Text = wholeText;
}
为什么不这样做呢
private void Write(string file, string text)
{
if (File.Exists(file))
{
using (StreamWriter objWriter = new StreamWriter(file))
{
objWriter.Write(text);
}
}
else
{
MessageBox.Show("No file named " + file);
}
}
private void btnAlpha_Click(object sender, EventArgs e)
{
string wholeText = "";
string copyText = richTextBox1.Text;
string nonAlpha = @"[^A-Za-z ]+";
string addSpace = "";
copyText = Regex.Replace(copyText, nonAlpha, addSpace);
wholeText = richTextBox1.Text + copyText;
Write(Second_File, wholeText); // same for the second button
richTextBox2.Text = wholeText;
}
为什么不这样做呢
private void Write(string file, string text)
{
if (File.Exists(file))
{
using (StreamWriter objWriter = new StreamWriter(file))
{
objWriter.Write(text);
}
}
else
{
MessageBox.Show("No file named " + file);
}
}
private void btnAlpha_Click(object sender, EventArgs e)
{
string wholeText = "";
string copyText = richTextBox1.Text;
string nonAlpha = @"[^A-Za-z ]+";
string addSpace = "";
copyText = Regex.Replace(copyText, nonAlpha, addSpace);
wholeText = richTextBox1.Text + copyText;
Write(Second_File, wholeText); // same for the second button
richTextBox2.Text = wholeText;
}
您可以使用一个通用函数,负责将内容写入文件并更新第二个文本框:
private void btnAlpha_Click(object sender, EventArgs e)
{
string nonAlpha = @"[^A-Za-z ]+";
string addSpace = "";
string copyText = richTextBox1.Text;
copyText = Regex.Replace(copyText, nonAlpha, addSpace);
WriteToFile(Second_File, wholeText);
}
private void btnVowels_Click(object sender, EventArgs e)
{
string vowels = "AaEeIiOoUu";
string copyText = richTextBox1.Text;
copyText = new string(copyText.Where(c => !vowels.Contains(c)).ToArray());
string wholeText = richTextBox1.Text + copyText;
WriteToFile(Second_File, wholeText);
}
private void WriteToFile(string filename, string contents)
{
if (File.Exists(filename))
{
File.WriteAllText(filename, contents);
richTextBox2.Text = wholeText;
}
else
{
MessageBox.Show("No file named " + filename);
}
}
您可以使用一个通用函数,负责将内容写入文件并更新第二个文本框:
private void btnAlpha_Click(object sender, EventArgs e)
{
string nonAlpha = @"[^A-Za-z ]+";
string addSpace = "";
string copyText = richTextBox1.Text;
copyText = Regex.Replace(copyText, nonAlpha, addSpace);
WriteToFile(Second_File, wholeText);
}
private void btnVowels_Click(object sender, EventArgs e)
{
string vowels = "AaEeIiOoUu";
string copyText = richTextBox1.Text;
copyText = new string(copyText.Where(c => !vowels.Contains(c)).ToArray());
string wholeText = richTextBox1.Text + copyText;
WriteToFile(Second_File, wholeText);
}
private void WriteToFile(string filename, string contents)
{
if (File.Exists(filename))
{
File.WriteAllText(filename, contents);
richTextBox2.Text = wholeText;
}
else
{
MessageBox.Show("No file named " + filename);
}
}
您可以使用一个通用函数,负责将内容写入文件并更新第二个文本框:
private void btnAlpha_Click(object sender, EventArgs e)
{
string nonAlpha = @"[^A-Za-z ]+";
string addSpace = "";
string copyText = richTextBox1.Text;
copyText = Regex.Replace(copyText, nonAlpha, addSpace);
WriteToFile(Second_File, wholeText);
}
private void btnVowels_Click(object sender, EventArgs e)
{
string vowels = "AaEeIiOoUu";
string copyText = richTextBox1.Text;
copyText = new string(copyText.Where(c => !vowels.Contains(c)).ToArray());
string wholeText = richTextBox1.Text + copyText;
WriteToFile(Second_File, wholeText);
}
private void WriteToFile(string filename, string contents)
{
if (File.Exists(filename))
{
File.WriteAllText(filename, contents);
richTextBox2.Text = wholeText;
}
else
{
MessageBox.Show("No file named " + filename);
}
}
您可以使用一个通用函数,负责将内容写入文件并更新第二个文本框:
private void btnAlpha_Click(object sender, EventArgs e)
{
string nonAlpha = @"[^A-Za-z ]+";
string addSpace = "";
string copyText = richTextBox1.Text;
copyText = Regex.Replace(copyText, nonAlpha, addSpace);
WriteToFile(Second_File, wholeText);
}
private void btnVowels_Click(object sender, EventArgs e)
{
string vowels = "AaEeIiOoUu";
string copyText = richTextBox1.Text;
copyText = new string(copyText.Where(c => !vowels.Contains(c)).ToArray());
string wholeText = richTextBox1.Text + copyText;
WriteToFile(Second_File, wholeText);
}
private void WriteToFile(string filename, string contents)
{
if (File.Exists(filename))
{
File.WriteAllText(filename, contents);
richTextBox2.Text = wholeText;
}
else
{
MessageBox.Show("No file named " + filename);
}
}
是windows窗体吗?是的,我会将标记作为旁白,不做“如果(某物==true)”,只做“如果(某物)”。是windows窗体吗?是的,我会将标记作为旁白,不做“如果(某物==true)”,只做“如果(某物)”。是windows窗体吗?是的,我会将标记作为旁白,不做“如果(某物==true)”,只要做“如果(某事)”。这是windows窗体吗?是的,我会把标记放在旁边,不要做“如果(某事==true)”,只要做“如果(某事)”。