0

I want to write an application which sorts randomly line of text which I copy from a source and paste into RichTextBox area.

However, there is one condition - text is formatted (some words are in bold, underline etc.). So any suggestions? How should it look like?

I think I should use RichTextBox.Rtf or something but I am really a beginner and I appreciate every hint or example code.

Thanks

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
user1188235
  • 55
  • 1
  • 1
  • 4
  • http://stackoverflow.com/questions/1287567/c-is-using-random-and-orderby-a-good-shuffle-algorithm – sll Mar 16 '12 at 21:09
  • So you want to keep the formatting while the source is unknown: rtf, html, word etc. ? – L.B Mar 16 '12 at 22:41

2 Answers2

0

The task seems not complicated(if I understand it correctly). Get your clipboard into string then parse into array- use Split(). Then determine how many randon events you need and iterate through every word ; generate random number for each iteration(which should match the amount of events), intersect that number with one of the events and apply that case to that particular word. Maybe not the most efficient way to do it, but that's what comes to my mind

Andrew
  • 7,619
  • 13
  • 63
  • 117
  • You cannot use `String.Split` since the RTF-Lines are not just separated with `\r\n`. Since the OP wants to keep RTF-formatting, things are a bit more complicated. – Olivier Jacot-Descombes Mar 16 '12 at 22:07
0

It is a bit tricky. You can retrieve the formatted RTF text lines like this

string[] rtfLines = new string[richTextBox1.Lines.Length];
for (int i = 0; i < rtfLines.Length; i++) {
    int start = richTextBox1.GetFirstCharIndexFromLine(i);
    int length = richTextBox1.Lines[i].Length;
    richTextBox1.Select(start, length);
    rtfLines[i] = richTextBox1.SelectedRtf;
}

Now you can shuffle the lines like this

var random = new Random();
rtfLines = rtfLines.OrderBy(s => random.NextDouble()).ToArray();

Clear the RichtTextBox

richTextBox1.Text = "";

Inserting the lines is best done in reverse order because it is easier to select the beginning of the text

// Insert the line which will be the last line.
richTextBox1.Select(0, 0);
richTextBox1.SelectedRtf = rtfLines[0];

// Prepend the other lines and add a line break.
for (int i = 1; i < rtfLines.Length; i++) {
    richTextBox1.Select(0, 0);

    // Replace the ending "}\r\n" with "\\par }\r\n". "\\par" is a line break.
    richTextBox1.SelectedRtf =
        rtfLines[i].Substring(0, rtfLines[i].Length - 3) + "\\par }\r\n";
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Thanks, that seems to work very well :) but here's one more thing. I added context menu to the richboxtext with only one function "paste". What code will paste my clipboard content to the richboxtext form? I tried with: private void PasteToolStripMenuItem_Click_1(object sender, EventArgs e) { richTextBox1.Text = Clipboard.GetText(); } but it pastes non-formatted text. How can I paste text with the formatting? – user1188235 Mar 17 '12 at 10:13
  • `richTextbox1.Paste();` or you can use the key combination `Ctrl-C`. – Olivier Jacot-Descombes Mar 17 '12 at 16:28