2016-09-15 42 views
0
namespace PostingQueueMonitoring_V2 
{ 
    public partial class Helper : Form 
    { 
     public Helper() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      if (String.IsNullOrEmpty(input_richTextBox1.Text.Trim())) 
      { 
       MessageBox.Show("Please input text..."); 
       return; 
      } 

      string[] myText = new string[] {input_richTextBox1.Text}; 
      foreach (string element in myText) 
      { 
       output_richTextBox2.Text = element; 
       System.Console.WriteLine(output_richTextBox2.Text); 
      } 
      System.Console.WriteLine(); 
      //output_richTextBox2.Rtf = input_richTextBox1.Rtf; 
     } 

     private void Helper_Resize(object sender, EventArgs e) 
     { 
      this.MinimumSize = new Size(736, 466); 
      this.MaximumSize = new Size(736, 466); 
     } 
    } 
} 

如何更新將格式化從input_richTextBox1文字和output_richTextBox2所示的代碼。下面的例子。C#如何添加字符串中的線值的RichTextBox的

input_richTextBox1: 蘋果\ n 版納\ n 椰子\ n 榴蓮\ n 橙色

預期結果爲output_richTextBox2 ( '蘋果', '版納', '椰子', 'Durian', 'Orange')

+0

你試過了什麼? –

+0

我試過了,output_richTextBox2.Text =「(」+ element +「)」;但那不是我所期望的。嘗試搜索相同的請求,但沒有運氣。 – deadlock

+0

永遠不要修改文本屬性,否則你會搞亂所有的格式!看看string.Split和RTB.Append – TaW

回答

0

請試試這個。

 private void button1_Click(object sender, EventArgs e) 
    { 
     if (String.IsNullOrEmpty(richTextBox1.Text.Trim())) 
     { 
      MessageBox.Show("Please input text..."); 
      return; 
     } 

     var textValues = richTextBox1.Text.Split('\n').Select(txt => $"'{txt}'"); 
     var concatValues = string.Join(",", textValues); 
     richTextBox2.Text = concatValues; 
     System.Console.WriteLine(); 
    }