2015-06-13 30 views
0

有沒有更好的方法讓我無需重複我按下的每個按鈕的代碼就可以進行流式處理?下面是一個例子我的工作,其中一個按鈕做它自己的事情,並相應地寫入到元音和其他的不只是它同樣的事情沒有字母字符據此寫道:streamWrite的代碼結構

 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); 
      } 
     } 
+0

它是windows窗體? – Dreamweaver

+0

是的,我將包括標籤 – BruceyBandit

+1

另外,不要做「if(something == true)」,只是做「if(something)」。 –

回答

1

爲什麼不能做這種方式?

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; 
} 
2

你可以使用一個共同的功能,將採取書面形式的內容,文件和更新第二個文本框的護理:

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); 
    } 
}