How do i copy from textBox1 to textBox2 the text inside including the spaces between the...

  • Thread starter Thread starter Chocolade1972
  • Start date Start date
C

Chocolade1972

Guest
For example in textBox1 i have lets say 4 words: hello danny yellow blue

I want to copy the words including the spaces between them !

Between danny and yellow there are 5 spaces and between yellow and blue 3 spaces.

So i want that in textBox2 the words will show up excatly with the same number of spaces between the words as it is in textBox1.

I could copy the whole text as it but my program take each word from textBox1 do some scramble on the word and copy it back to textBox2.


This is the code in my new class :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ScrambleRandomWordsTest
{
class ScrambleTextBoxText
{
private static readonly Random RandomGen = new Random();
private List<string> _words;
private List<string> _scrambledWords;

public ScrambleTextBoxText(List<string> textBoxList)
{
_words = textBoxList;
GetText();
}

private void GetText()
{
for (int i = 0; i < _words.Count; i++)
{
string word = _words;
if (word.Length < 4) continue;

string first = word.Substring(0, 1);
string last = word.Substring(word.Length - 1, 1);
string middle = word.Substring(1, word.Length - 2);
_words = first + MakeRandomwords(middle) + last;//MakeRandomwords(middle) + last;
}
_scrambledWords = _words;
}

private static StringBuilder MakeRandomwords(string theWord)
{
var jumbleSb = new StringBuilder();
jumbleSb.Append(theWord);
int lengthSb = jumbleSb.Length;

char first_char = theWord[0];
bool flag = false;
for (int i = 1; i < theWord.Length; i++)
{
if (theWord != first_char)
{
flag = true;
break;
}
}
if (flag == false)
return jumbleSb; // all characaters in the word are the same... so it cant be jumbled..



while (MyCompare(theWord, jumbleSb.ToString()) == 0) // keep jumbling untill we get something different (because it is random it could be also the same
// at least how you wrote it here...) ... in general i think you can write something that make a random permutation that
// that will not create the same word by chance... but for now this can be good enough... to keep jumbling...
{
for (int i = 0; i < lengthSb; ++i)
{
int index1 = (RandomGen.Next() % lengthSb);
int index2 = (RandomGen.Next() % lengthSb);
Char temp = jumbleSb[index1];
jumbleSb[index1] = jumbleSb[index2];
jumbleSb[index2] = temp;
}
}

return jumbleSb;
}

private static int MyCompare(string a, string b)
{
if (a.Length != b.Length)
return -1;
for (int i = 0; i < a.Length; i++)
{
if (a != b)
return -1;
}
return 0;
}

public List<string> scrambledTextBoxWords()
{
List<string> words = _scrambledWords;
return words;
}
}
}



And the code in Form1 button click which make the copy:


private void BtnScrambleText_Click(object sender, EventArgs e)
{
textBox1.Enabled = false;
BtnScrambleText.Enabled = false;
textBoxWords = ExtractWords(textBox1.Text);
ScrambleTextBoxText scrmbltb = new ScrambleTextBoxText(textBoxWords);
for (int i = 0; i < scrmbltb.scrambledTextBoxWords().Count; i++)
{
textBox2.AppendText(scrmbltb.scrambledTextBoxWords());
}
}


First i put some text in textBox1 click the button make an instance of the new class scramble the words then from the new class i send back a List with all the scrambled words to Form1.


Then make a loop and add the words to the textBox2 using AppendText.

The way it is now in textBox2 i will see : hellodannyellowblue

I could make :


textBox2.Text = String.Join(" ", scrmbtb.ScrambledTextBoxWords);


But then it will add one space between each words.

I want to add the same exact spaces there was in textBox1 between the words.

How can i do it ?

Continue reading...
 
Back
Top