Capture Event in RichText Box

Roswell

Newcomer
Joined
Apr 1, 2005
Messages
5
Hi,

I've to capture an enter event everytime when a user hit enter in richtext box for the new line. I want each line into array so that I could get each word separatly.
How do I do that in c# ? Need some quick help with code.

Regards,
 
Roswell:
This would be an easy way to get a string array of the lines in a RichTextBox without the overhead of handling the Key events

Code:
/// <summary>
/// Uses new line breaks to split RichTextBox text into a string array of lines.
/// </summary>
/// <param name="rtb">RichTextBox containing text to break.</param>
/// <returns>Array of RichTextBox lines.</returns>
private string[] SplitRichText(System.Windows.Forms.RichTextBox rtb)
{
          return rtb.Text.Split(new char[]{char.Parse("\n")});
}
 
You could create a handler for the KeyUp event similar to
C#:
private void richTextBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyData== Keys.Return)
	{
	//enter was pressed
	}
}
The rich text box already has an array of Lines as a property, you may be able to use that rather than create an array of your own.
 
Thank you both of you. Tihs line thingy has already been done. Let me brief you what I want to do, please have a look to the following code



string[] tempArray = new string [rch.Lines.Length];
tempArray = rch.Lines;
MessageBox.Show ("Characters are "+rch.TextLength.ToString ()); //number of char

MessageBox.Show ("Lines are " +rch.Lines.Length.ToString ()); //how manylines,

string gg;
int count=0;
string[] kk= new string[rch.Lines.Length];

for(int b = 0;b<tempArray[0].Length ;b++)
{
gg=tempArray[0].Substring(b,1);
kk[count]=kk[count]+ tempArray[0].Substring (b,1);
switch (gg)
{
case ("\n"):
count ++;
break;
case (" "):
count ++;
break;
case (" "): //detecting for tow white spaces
count ++;
break;
}

} // end for


MessageBox.Show("there are " + count.ToString ()+ " words in line 1");


MessageBox.Show ("There are "+tempArray[0].Length.ToString ()+" characters in first line" );

What this code is doing is, while consider the for loop only, to count number of words in an array containing the first line of richtext box. But this switch case doenst work for "\n" i.e. new line and if a line has more than one white spaces then it wont work. I think the reason is this
gg=tempArray[0].Substring(b,1);
It is detecting the line 1 , char by char...............
How can I fix my code for if a user enters more than one whitespaces and to detect the "\n" thingy.

Warm Regards,
 
Back
Top