2014-02-28 86 views
0

question如何删除richtextbox中的空格?

有关这个问题

,我想删除的间距在我的RichTextBox因为显示器总是包含的间距,如:

Hello 


Hello 


Hello 

我想修改,将显示该文本为代码:

Hello 
Hello 
Hello 

我该怎么做?有谁知道?由于

下面是代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.IO.Ports; 
using System.Threading; 
using System.Windows.Threading; 
using System.Data.SQLite; 
using System.Text.RegularExpressions; 

namespace SerialTrial 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 

    public class ThreadExample 
    { 
     FlowDocument mcFlowDoc = new FlowDocument(); 
     Paragraph para = new Paragraph(); 
     public static void ThreadJob(MainWindow mainWindow) 
     { 
      string dBConnectionString = @"Data Source = C:\Users\Documents\Visual Studio 2012\Projects\SerialTrial\SerialTrial\bin\Debug\employee.sqlite;"; 
      SQLiteConnection sqliteCon = new SQLiteConnection(dBConnectionString); 
      //open connection to database 
      try 
      { 
       sqliteCon.Open(); 
       SQLiteCommand createCommand = new SQLiteCommand("Select empID from EmployeeList", sqliteCon); 
       SQLiteDataReader reader; 
       reader = createCommand.ExecuteReader(); 


       //richtextbox2.Document.Blocks.Clear(); 
       while (reader.Read()) 
       { 

        string Text = (String.Format("{0}", Object.Equals(Variables.buffering, reader.GetValue(0)))); 
        if (Convert.ToBoolean(Text)) 
        { 
         mainWindow.Dispatcher.Invoke(new Action(() => 
        mainWindow.richtextbox2.Document.Blocks.Add(new Paragraph(new Run("Hello"))))); 
         string text = "s"; 
         mainWindow.WriteSerial(text); 
         Console.WriteLine(Text); 
         //richtextbox2.Document.Blocks.Add(new Paragraph(new Run(Text))); 
        } 
       } 
       sqliteCon.Close(); 

      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 

    } 


    public partial class MainWindow : Window 
    { 
     SerialPort serial = new SerialPort(); 
     //string received_data; 
     //Thread readThread = new Thread(Read); 
     FlowDocument mcFlowDoc = new FlowDocument(); 
     Paragraph para = new Paragraph(); 



     public MainWindow() 
     { 
      InitializeComponent(); 
      combobox1.Items.Insert(0, "Select Port"); 
      combobox1.SelectedIndex = 0; 
      string[] ports = null; 
      ports = SerialPort.GetPortNames(); 


      // Display each port name to the console. 
      int c = ports.Count(); 
      for (int i = 1; i <= c; i++) 
      { 
       if (!combobox1.Items.Contains(ports[i - 1])) 
       { 
        combobox1.Items.Add(ports[i - 1]); 
       } 

      } 
     } 
     string dBConnectionString = @"Data Source = C:\Users\Documents\Visual Studio 2012\Projects\SerialTrial\SerialTrial\bin\Debug\employee.sqlite;"; 

     static int count = 0; 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      count++; 
      string[] ports = null; 
      ports = SerialPort.GetPortNames(); 


      // Display each port name to the console. 
      int c = ports.Count(); 
      for (int i = 1; i <= c; i++) 
      { 
       if (!combobox1.Items.Contains(ports[i - 1])) 
       { 
        combobox1.Items.Add(ports[i - 1]); 
       } 

      } 

     } 

     private void combobox1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      string myItem = combobox1.SelectedItem.ToString(); 
      textbox1.Text = myItem; 
     } 

     private void button2_Click(object sender, RoutedEventArgs e) 
     { 
      try 
      { 
       if ((string)button2.Content == "Connect") 
       { 
        string myItem = combobox1.SelectedItem.ToString(); 
        if (myItem == "Select Port") 
        { 
         MessageBox.Show("Select Port"); 
        } 
        else 
        { 
         serial.PortName = myItem; 
         serial.Open(); 
         textbox2.Text = "Serial Port Opened"; 
         button2.Content = "Disconnect"; 
         serial.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived); 

        } 
       } 

       else 
       { 
        serial.Close(); 
        button2.Content = "Connect"; 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 

     #region Receiving 


     public void port_DataReceived(object sender, SerialDataReceivedEventArgs e) 
     { 
      int bytes = serial.BytesToRead; 
      byte[] buffer = new byte[bytes]; 
      serial.Read(buffer, 0, bytes); 
      foreach (var item in buffer) 
      { 
       Console.Write(item.ToString()); 
      } 
      Variables.buffering = BitConverter.ToInt64(buffer, 0); 
      Console.WriteLine(); 
      Console.WriteLine(Variables.buffering); 
      Thread thread = new Thread(new ThreadStart(() => ThreadExample.ThreadJob(this))); 
      thread.Start(); 
      thread.Join(); 
     } 


     /* 
     private delegate void UpdateUiTextDelegate(string text); 
     private void Receive(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) 
     { 
      // Collecting the characters received to our 'buffer' (string). 
      received_data = serial.ReadExisting(); 
      Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(WriteData), received_data); 
     } 

     private void WriteData(string text) 
     { 
      // Assign the value of the recieved_data to the RichTextBox. 
      para.Inlines.Add(text); 
      mcFlowDoc.Blocks.Add(para); 
      richtextbox2.Document = mcFlowDoc; 
      richtextbox2.ScrollToEnd(); 
     } 
     */ 

     #endregion 

     private void button3_Click(object sender, RoutedEventArgs e) 
     { 
      if (serial.IsOpen) 
      { 
       TextRange allTextRange = new TextRange(richtextbox1.Document.ContentStart, richtextbox1.Document.ContentEnd); 
       string allText = allTextRange.Text; 
       serial.WriteLine(allText); 
      } 
     } 



     public void WriteSerial(string text) 
     { 
      serial.Write(text); 
     } 



    } 
} 

我希望把它放在这里面,如果功能:

if (Convert.ToBoolean(Text)) 
         { 
          mainWindow.Dispatcher.Invoke(new Action(() => 
         mainWindow.richtextbox2.Document.Blocks.Add(new Paragraph(new Run("Hello"))))); 
          string text = "s"; 
          mainWindow.WriteSerial(text); 
          Console.WriteLine(Text); 
          //richtextbox2.Document.Blocks.Add(new Paragraph(new Run(Text))); 
         } 

IMAGE

+0

通过Regex – Jonesopolis

回答

1

请尝试以下Regex

RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, @"^\s*$(\n|\r|\r\n)", "", RegexOptions.Multiline); 
+0

无法使用单个换行符替换多个换行符。与之前一样 – iaskyou

+0

@iaskyou检查我编辑的答案是否有效。 – MusicLovingIndianGirl

+0

awh。我在这里有我的代码。顺便说一句,我正在使用wpf。 http://stackoverflow.com/questions/22086131/error-cannot-convert-lambda-expression-to-type/22086615?noredirect=1#comment33498363_22086615 不,它不工作 – iaskyou

0
string myText = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd).Text; 
var resultString = Regex.Replace(myText, @"(|\r?\n)\1+", "$1"); 
+0

richtextbox.Resources不适用于wpf c#我认为 – iaskyou

+0

错误在'RichTextBox'类型中未找到可附加属性'Resources'。 – iaskyou

+0

错误成员“资源”无法识别或无法访问。 – iaskyou