[.net] Help with ExRichTextBox remove first line.
Hiya all..
Not sure if this is good spot to ask this, but i have a problem thats been nagging me for a bit.
Its about the "addon" control
http://www.codeproject.com/KB/edit/csexrichtextbox.aspx
I have made a chat that supports smilies, but i want the chat to have a maximum lenght (eg 100 lines).
Problem is, i dont know how to remove the first line from a Rtf textbox.
I tried tons of ways, but i cant seem to get it to work in any ways.
If anyone could give me some insight or perhaps a good sollution to this, i would be really grateful.
im basicly still at
if (richTextBox1.Lines.Length == maxRows)
{
}
i tried richTextBox1.Lines[0].Remove();, but that crashes.
i tried tons of other ways, but all isnt working.
Thanks :)
The easiest way is to select the first line of text and erase it, like this:
private static void LimitLines(RichTextBox rtb, int maxLines) { int OldSelectionStart = rtb.SelectionStart; int OldSelectionLength = rtb.SelectionLength; try { while (rtb.Lines.Length > maxLines && maxLines > 0) { rtb.SelectionStart = 0; rtb.SelectionLength = rtb.Lines[0].Length + 1; OldSelectionStart -= rtb.SelectionLength; rtb.SelectedText = ""; } } finally { rtb.SelectionStart = OldSelectionStart; rtb.SelectionLength = OldSelectionLength; }}
Thanks, but with that code, it wont add new lines after maxlines, it doesnt remove the first line.
Problem is, i dont know how to -> remove the first line <- from a Rtf textbox.
Problem is, i dont know how to -> remove the first line <- from a Rtf textbox.
Quote:Original post by NightMarezThis bit of the code:
Problem is, i dont know how to -> remove the first line <- from a Rtf textbox.
rtb.SelectionStart = 0;rtb.SelectionLength = rtb.Lines[0].Length + 1;rtb.SelectedText = "";
...removes the first line for me. It sets a text selection from 0 to the end of the first line + 1 (+1 so it selects the newline character too) then erases it by setting it to "". Usual "works on my machine" disclaimer applies - try pasting your full code?
okay, here is where i add code
and then ofcourse your code
private int maxRows = 5; // for testing private void addMessage(string user, byte access, string message) { DateTime time = DateTime.Now; LimitLines(richTextBox1, maxRows); if (access == 0) // normal user { string msgtime = time.Hour.ToString("00") + ":" + time.Minute.ToString("00"); string username = " [" + user + "] "; richTextBox1.AppendTextAsRtf(msgtime, new Font("Arial", 8, FontStyle.Regular), Khendys.Controls.RtfColor.Time, Khendys.Controls.RtfColor.Back); richTextBox1.AppendTextAsRtf(username, new Font("Arial", 8, FontStyle.Regular), Khendys.Controls.RtfColor.White, Khendys.Controls.RtfColor.Back); } richTextBox1.AppendRtf( CheckSmilies(access, message) ); richTextBox1.Select(richTextBox1.TextLength, 0); richTextBox1.ScrollToCaret(); }
and then ofcourse your code
private void LimitLines(Khendys.Controls.ExRichTextBox rtb, int maxLines) { int OldSelectionStart = rtb.SelectionStart; int OldSelectionLength = rtb.SelectionLength; try { while (rtb.Lines.Length > maxLines && maxLines > 0) { rtb.SelectionStart = 0; rtb.SelectionLength = rtb.Lines[0].Length + 1; OldSelectionStart -= rtb.SelectionLength; rtb.SelectedText = ""; } } finally { rtb.SelectionStart = OldSelectionStart; rtb.SelectionLength = OldSelectionLength; } }
I'm not sure if it's a problem with that control, then. I couldn't get your code to run at all (Khendys.Controls.RtfColor.Time and Khendys.Controls.RtfColor.Back aren't defined, AppendTextAsRtf always throws a FileFormatException).
Take a form, chuck a RichTextBox and a Button onto it, and this works for me:
Take a form, chuck a RichTextBox and a Button onto it, and this works for me:
using System;using System.Windows.Forms;namespace LimitLines { public partial class TestForm : Form { public TestForm() { InitializeComponent(); } private static void LimitLines(RichTextBox rtb, int maxLines) { int OldSelectionStart = rtb.SelectionStart; int OldSelectionLength = rtb.SelectionLength; try { while (rtb.Lines.Length > maxLines && maxLines > 0) { rtb.SelectionStart = 0; rtb.SelectionLength = rtb.Lines[0].Length + 1; OldSelectionStart -= rtb.SelectionLength; rtb.SelectedText = ""; } } finally { rtb.SelectionStart = OldSelectionStart; rtb.SelectionLength = OldSelectionLength; } } private void button1_Click(object sender, EventArgs e) { this.richTextBox1.AppendText("Hello: " + DateTime.Now + Environment.NewLine); LimitLines(this.richTextBox1, 5); this.richTextBox1.SelectionStart = this.richTextBox1.Text.Length; this.richTextBox1.ScrollToCaret(); } }}
Actually, i think i know the problem.
Its a Rtf text control, Rtf text is colored, fonts, ect ect info in the rtf text, and you simply cant remove 1 line.
excample of text
---------------------------
{\rtf1\ansi\ansicpg1252\deff0\deflang1030{\fonttbl{\f0\fnil\fcharset0 \\fnilArial;}{\f1\fnil\fcharset0 Microsoft Sans Serif;}}
{\colortbl ;\red255\green165\blue0;\red59\green59\blue59;\red255\green255\blue255;}
\viewkind4\uc1\pard\cf1\highlight2\lang1033\b\f0\fs16 - Welcome to the xxxxx Chat.\par
\b0 - For help, Type /help, to change room, use the list in lower right cornor, and enjoy your stay.\par
\par
\pard\cf3\highlight0\lang1030\f1\fs17\par
}
---------------------------
thats the Rtf output with colors ect.
the .Lines[] is plaintext
the .Text is plaintext
the .Rtf is rtf formatted text.
If you try to remove some plaintext, you only remove the text and not the fomatting i think (and i tried to manually remove a rtf formatted textstring by just removing the string, and application crashed).
this is just speculation tho, since i still didnt manage to get it to remove a line.
Its a Rtf text control, Rtf text is colored, fonts, ect ect info in the rtf text, and you simply cant remove 1 line.
excample of text
---------------------------
{\rtf1\ansi\ansicpg1252\deff0\deflang1030{\fonttbl{\f0\fnil\fcharset0 \\fnilArial;}{\f1\fnil\fcharset0 Microsoft Sans Serif;}}
{\colortbl ;\red255\green165\blue0;\red59\green59\blue59;\red255\green255\blue255;}
\viewkind4\uc1\pard\cf1\highlight2\lang1033\b\f0\fs16 - Welcome to the xxxxx Chat.\par
\b0 - For help, Type /help, to change room, use the list in lower right cornor, and enjoy your stay.\par
\par
\pard\cf3\highlight0\lang1030\f1\fs17\par
}
---------------------------
thats the Rtf output with colors ect.
the .Lines[] is plaintext
the .Text is plaintext
the .Rtf is rtf formatted text.
If you try to remove some plaintext, you only remove the text and not the fomatting i think (and i tried to manually remove a rtf formatted textstring by just removing the string, and application crashed).
this is just speculation tho, since i still didnt manage to get it to remove a line.
Khendys.Controls.ExRichTextBox rtb1 = new Khendys.Controls.ExRichTextBox(); rtb1.Rtf = rtb.Rtf; rtb1.SelectionStart = 0; rtb1.SelectionLength = rtb.Lines[0].Length + 1; rtb1.SelectedText = ""; rtb.Clear(); rtb.Rtf = rtb1.Rtf;
seems that works :) thank for the idea ^_^ finally i can continue hehe
Khendys.Controls.ExRichTextBox rtb1 = new Khendys.Controls.ExRichTextBox(); rtb1.Rtf = rtb.Rtf; rtb1.SelectionStart = 0; rtb1.SelectionLength = rtb.Lines[0].Length + 1; rtb1.SelectedText = ""; rtb.Clear(); rtb.Rtf = rtb1.Rtf;
seems that works :) thank for the idea ^_^ finally i can continue hehe
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement