Share via


Write strings into a richTextBox

Question

Thursday, February 23, 2006 6:32 AM

Im looking for a way to write strings from an array into a richTextBox. Im fairly new to c# and im trying to make a windows version of an app i did in php.

lets say for instance i had an array of 10 strings

array[] letters {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};

I want to add the letters inside the text box, one by one (so not all at once), in a loop (while).

in PHP i would just echo from the array in between the textarea tags

for instance(PHP):

$lettersArray = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")
$i = 0;

while ($i < 10)
     {
          echo"$lettersArray[$i]";
          $i++;
     }

Its its really simple in php because you just echo between the tags without interacting with the textbox, so i have no idea how to get the text into the box in C# :| Please help!

Thanks,
Atomican

All replies (3)

Thursday, February 23, 2006 8:39 AM ✅Answered

string[] letters = new string[10];

letters[0] = 'a';
letters[1] = 'b';
letters[2] = 'c';
letters[3] = 'd';
letters[4] = 'e';
letters[ 5 ] = 'f';
letters[ 6 ] = 'g';
letters[7] = 'h';
letters[ 8 ] = 'i';
letters[9] = 'j';

Lets say your richtextbox is called rtbFromArray

for(int i = 0; i < letters.Length; i++)
{
         rtbFromArray.Text += letters[ i ];
}

OR:

ArrayList letters = new ArrayList(); /* this one has no max. like the above example has 10 elements an ArrayList has a dynamic length.

letters.Add("a");
letters.Add("b");
letters.Add("c");
letters.Add("d");
letters.Add("e");
letters.Add("f");

foreach(string s in letters)
{
      rtbFromArray.Text += s;
}

good luck!

// bloody emoticons...
 


Thursday, February 23, 2006 8:58 AM ✅Answered

Mark Dirksma is correct, you can just iterate over a array and append this to the Text property value of you Contol.

Here is a little optimalization for your process, that uses a buffer first and append the full text in one operation so you Control doesn't need to process every character and refresh all the time. Also string concat's aren't the faster things on earth, so for large operations you can use a buffer instead.

A example:


public void AddString()
{
    Char[] letters = new Char[]
    {
        'a', 'b', 'c', 'd', 'e'
    }
   
    StringBuffer buffer = new StringBuffer( charaters.Lenght );
   
    foreach( Char letter in letters )
    {
        buffer.Append( letter );
    }
   
    String fullText = buffer.ToString();
    rtbFormArray.AppendText( fullText );
}
 

Thursday, February 23, 2006 8:42 AM

Thanks very much, ill try that :)

also, sorry about posting in the wrong section. i just clicked 'ask a question' and it took me here lol