Reputation: 184
I would like to display a chunk of text in a richtexbox, but limit the length to fit the text area displayed on the GUI. The user then then hit the next button and it erases the text area and continues onwards with more text in the richtextbox. Kind of like custom scroll bars for the text area, but a way to be able to tell if there is space left on the text area, incase the text font gets larger or smaller it will be dynamic? Is there an event listener for when the text area is full?
Upvotes: 0
Views: 1062
Reputation: 184
I found an answer. You can use:
Size textSize = TextRenderer.MeasureText(richTextBox1.Text, richTextBox1.Font);
for the size of the text then check OnContentResized
event whether the MeasureText()
is bigger then the height of the rich textbox.
Upvotes: 3
Reputation: 6581
If WinForms. Overflow detection in case someone copies a bunch of text.
private void richTextBox1_TextChanged(object sender, EventArgs e) {
if(richTextBox1.Text.Length ==maxlength){
//textboxfull
}
else if (richTextBox1.Text.Length > maxlength) {
//textbox over flow
}
}
if WPF
Markup:
<RichTextBox Name="rtbEditor" TextChanged="rtbEditor_TextChanged" />
Code:
private void rtbEditor_TextChanged(object sender, TextChangedEventArgs e) {
string myText = new TextRange(rtbEditor.Document.ContentStart, rtbEditor.Document.ContentEnd).Text;
if (myText.Length == maxlength) {
//textboxfull
}
else if (myText.Length > maxlength) {
//textbox over flow
}
}
Upvotes: 2