Click here to Skip to main content
15,922,584 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I'm coding a big log file viewer. So, I have 2 questions:
1. How I can get the actual size of richtextbox? ( with invisible "scrolling" area)
2. How I can delete only a part of richtextbox ( top or bottom text in percent of full text)?
Posted

I'm not sure what it is that you are asking for in 1.. All Controls have a Size property, if that is what you want. I suspect, however, that you mean the size of the area available for text.

The only way that I can think of is to use P/Invoke with the EM_GETRECT Message[^]. I've not tried it, so no guarantees.

As far as 2. is concerned, it depends on whether you want a percentage of the number of lines, the number of words or the number of characters.

Lines
The richtextbox has a Lines property and MyRTB.Lines.Length will give you the value.

Words
Here is a WordCount Method.

C#
private int GetWordCount()
{
    // Add space to end of text to ensure the last word is counted.
    MatchCollection wordColl = Regex.Matches(richTextBox1.Text, @"[\W]+");
    return wordColl.Count;
}


Characters
myRTB.TextLength may be enough for what you need.

In all cases a quick bit of math will get your answer.

For deleting the text there are many methods but one of the easiest is to use the myRTB.Text.Remove(int)[^] method, or one of its' overrides.
 
Share this answer
 
Comments
Vlcyrex 6-May-10 9:57am    
Thank you!!!
I am not sure if this is what you asked for, but the RichTextBox has a Line member where you can get a string array containig the Lines of the Text. If you get the Length member of this array you have the number of lines in your control. This is independend of the visible portion of the text. If you need the size in points or pixels the you need to look up what font is used I think.

With this information you can calculate the percentage of lines to delete.

I haven't tried this, but I think this schould work
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900