2017-01-25 4 views
1

Im Download-Ereignis meines Codes Ich habe diese zwei Zeilen:Wie kann ich einen Teil des RichTextBox-Texts ändern?

RichTextBoxExtensions.AppendText(richTextBox1, "Downloading: ", Color.Red); 
RichTextBoxExtensions.AppendText(richTextBox1, url, Color.Green); 

Der Teil des Codes

int count = 0; 
     private void DownloadFile() 
     { 
      if (_downloadUrls.Any()) 
      { 
       WebClient client = new WebClient(); 
       client.DownloadProgressChanged += client_DownloadProgressChanged; 
       client.DownloadFileCompleted += client_DownloadFileCompleted; 

       var url = _downloadUrls.Dequeue(); 

       client.DownloadFileAsync(new Uri(url), @"C:\Temp\New folder (13)\" + count + ".txt"); 
       RichTextBoxExtensions.AppendText(richTextBox1, "Downloading: ", Color.Red); 
       RichTextBoxExtensions.AppendText(richTextBox1, url, Color.Green); 
       richTextBox1.AppendText(Environment.NewLine); 
       count++; 
       countCompleted--; 
       label1.Text = countCompleted.ToString(); 
       return; 
      } 

      // End of the download 
      btnStart.Text = "Download Complete"; 
     } 

     private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) 
     { 
      if (e.Error != null) 
      { 
       // handle error scenario 
       throw e.Error; 
      } 
      else 
      { 

      } 
      if (e.Cancelled) 
      { 
       // handle cancelled scenario 
      } 
      DownloadFile(); 
     } 

Was ich will ist jedes Mal herunterladen eine Datei tun abgeschlossen, dass im fertigen Ereignis ist zu Ändern Sie den Teil "Downloading" in "Downloaded:" und die ganze Zeile.

Die Klasse der RichTextBoxExtensions

public class RichTextBoxExtensions 
     { 
      public static void AppendText(RichTextBox box, string text, Color color) 
      { 
       box.SelectionStart = box.TextLength; 
       box.SelectionLength = 0; 

       box.SelectionColor = color; 
       box.AppendText(text); 
       box.SelectionColor = box.ForeColor; 
      } 
     } 

Antwort

0

Hier ist, was Sie gefragt:

public static class RichTextBoxExtensions 
{ 
    public static void AppendText(this RichTextBox box, string text, Color color) 
    { 
     box.SelectionStart = box.TextLength; 
     box.SelectionLength = 0; 

     box.SelectionColor = color; 
     box.AppendText(text); 
     box.SelectionColor = box.ForeColor; 
    } 
    public static void UpdateText(this RichTextBox box, string find, string replace, Color? color) 
    { 
     box.SelectionStart = box.Find(find, RichTextBoxFinds.Reverse); 
     box.SelectionLength = find.Length; 
     box.SelectionColor = color ?? box.SelectionColor; 
     box.SelectedText = replace; 
    } 
} 

Ich habe auch Ihre Erweiterungsmethoden korrigiert, so dass Sie sie direkt anrufen:

richTextBox1.AppendText("Downloading: ", Color.Red); 
richTextBox1.AppendText("qwe", Color.Green); 

// ... 

richTextBox1.UpdateText("Downloading: ", "Downloaded: ", Color.Green); 
Verwandte Themen