首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >streamWrite的代码结构

streamWrite的代码结构
EN

Stack Overflow用户
提问于 2015-06-13 09:11:52
回答 2查看 91关注 0票数 0

对于我来说,是否有更好的方法不需要对我按下的每一个按钮重复代码?下面是我正在处理的一个示例,其中一个按钮执行自己的操作,并相应地将其写入元音,而另一个按钮则执行相同的操作,除非它对没有alpha字符的字符进行相应的写入:

代码语言:javascript
复制
        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);
            }
        }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-13 09:41:46

为什么不这样做呢?

代码语言:javascript
复制
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;
}
票数 1
EN

Stack Overflow用户

发布于 2015-06-13 09:44:57

您可以使用一个通用函数,负责将内容写入文件和更新第二个文本框:

代码语言:javascript
复制
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);
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30816947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档