Windows Develop Bookmark and Share   
 index > Windows Forms General > richtextbox control
 

richtextbox control

heres what I want to do
add different lines of color witch I can do but I want the last line to be added to the beginning of the textbox with the color formatting, like a basic chat box how can I get this to work properly?
Regards,
Smacker
Smacker  Monday, August 31, 2009 1:54 PM
You've gotto select the line then change the color. Here's a thread discussing the issue. and a Microsoft thread.
おろ?
P.Brian.Mackey  Monday, August 31, 2009 3:23 PM
change the color of a line
this time first line
string str = richTextBox1.Lines[0];
richTextBox1.SelectionStart = richTextBox1.Find(str);
richTextBox1.SelectionColor = Color.Blue;
AUmidh  Monday, August 31, 2009 3:29 PM
Try the following,
private void AddLineToRichTextBox(string strToAdd)<br/>
{<br/>

strToAdd = strToAdd+ "\\par";
            string str = richTextBox1.Rtf;
            string[] strSep={richTextBox1.Lines[0]};
            string[] strTotal = str.Split(strSep,2,StringSplitOptions.None);
            string strT ="";
            if(strTotal.Length>0)
            {<br/>
 strT = strTotal[0] + strToAdd + " " + richTextBox1.Lines[0] + strTotal[1];<br/>
           }
            richTextBox1.Rtf = strT;<br/>
           string strLine = richTextBox1.Lines[0];<br/>
            richTextBox1.SelectionStart = richTextBox1.Find(strLine);
           richTextBox1.SelectionColor = Color.Blue;<br/>

}
May be this will help you or provide you an idea to the solution of your problem.
AUmidh  Tuesday, September 01, 2009 10:01 AM
string strLine=richTextBox1.Lines[0];
char strSep[strSep.Length];
for(int i=0;i<strLine.Length;i++)
{
strSep[i]=strLine[i];
}

Hope this help you otherwise tell me the problem. and don't forget to close the question.
AUmidh  Wednesday, September 02, 2009 2:11 PM

After a lot of test, the error should be “File format is not valid.?

It is not easy to change and write the rtf format text. It should follow Rich Text Format (RTF) Specification. (Rich Text Format (RTF) Specification, version 1.6: http://msdn.microsoft.com/en-us/library/aa140277(office.10).aspx )

However, changing the text is easier. For example:

int i = 0;

string str = "aaa";

private void button1_Click(object sender, EventArgs e)

{

richTextBox1.Text = str + (i++).ToString() + Environment.NewLine + richTextBox1.Text;

richTextBox1.SelectionStart = 0;

richTextBox1.SelectionLength = richTextBox1.Lines[0].Length;

richTextBox1.SelectionColor = Color.Blue;

}

This can add the text at the beginning.

Change the color is complex. Please refer to the following article. The author expands richtextbox control. It will tell you how to change the rtf to set the color and font.

http://www.codeproject.com/KB/edit/csexrichtextbox.aspx

Best regards,

Ling Wang


Please remember to click “Mark as Answer?on the post that helps you, and to click “Unmark as Answer?if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Tuesday, September 08, 2009 4:39 AM
You want to assign the lines of one richtextbox into another ?
AUmidh  Monday, August 31, 2009 2:15 PM
its basicly a server client application so messages from other clients will be displayed in one color and messages from the server will be displayed in a different color,
but the last message received needs to be the first line in the richtextbox
Smacker  Monday, August 31, 2009 2:19 PM
Start with a simple chat application like: http://www.codeproject.com/KB/IP/TCPIPChat.aspx

You need towrite a protocol (a set of rules) that can identify the sender of the message as client or server. You also need to establish a topology and a transport protocol as this will affect the protocol design. Once you've done this, color coding the sender will be easy.
おろ?
P.Brian.Mackey  Monday, August 31, 2009 2:39 PM
I already have the client/server portion done just need to know is
1. how to add the last message to the top of the richtextbox and
2. how to change the color of a line from the defualt text forecolor

Smacker  Monday, August 31, 2009 3:06 PM
You've gotto select the line then change the color. Here's a thread discussing the issue. and a Microsoft thread.
おろ?
P.Brian.Mackey  Monday, August 31, 2009 3:23 PM
change the color of a line
this time first line
string str = richTextBox1.Lines[0];
richTextBox1.SelectionStart = richTextBox1.Find(str);
richTextBox1.SelectionColor = Color.Blue;
AUmidh  Monday, August 31, 2009 3:29 PM
Try the following,
private void AddLineToRichTextBox(string strToAdd)<br/>
{<br/>

strToAdd = strToAdd+ "\\par";
            string str = richTextBox1.Rtf;
            string[] strSep={richTextBox1.Lines[0]};
            string[] strTotal = str.Split(strSep,2,StringSplitOptions.None);
            string strT ="";
            if(strTotal.Length>0)
            {<br/>
 strT = strTotal[0] + strToAdd + " " + richTextBox1.Lines[0] + strTotal[1];<br/>
           }
            richTextBox1.Rtf = strT;<br/>
           string strLine = richTextBox1.Lines[0];<br/>
            richTextBox1.SelectionStart = richTextBox1.Find(strLine);
           richTextBox1.SelectionColor = Color.Blue;<br/>

}
May be this will help you or provide you an idea to the solution of your problem.
AUmidh  Tuesday, September 01, 2009 10:01 AM
ok this may sound silly but I really need to use visual c# 2003 for this app, and by the way this code works great,
my problem is
string
[] strTotal = str.Split(strSep,2,StringSplitOptions.None);
in 2003 you can not use a string as your splitter it must be a char[]
so I did this
char
[] strSep={Convert.ToChar(richTextBox1.Lines[0])};
but get a error that char can't be more then one character...makes sence
so i did
char
[] strSep=richTextBox1.Lines[0].ToCharArray();
but the code errors out here "incorrect format"
richTextBox1.Rtf = strT;
any ideas how I can get this to work in vs 2003?
Smacker  Wednesday, September 02, 2009 1:48 PM
string strLine=richTextBox1.Lines[0];
char strSep[strSep.Length];
for(int i=0;i<strLine.Length;i++)
{
strSep[i]=strLine[i];
}

Hope this help you otherwise tell me the problem. and don't forget to close the question.
AUmidh  Wednesday, September 02, 2009 2:11 PM
Im still geting a invalid format exception.
here is the updated code im using.
	private void NormalAddLineToRichTextBox(string strToAdd)
		{
			if (rtb.Lines.Length == 0)
			{
				rtb.SelectionColor = Color.Black;
				rtb.SelectedText = strToAdd;
			}
			else
			{
				strToAdd = strToAdd + "\\par";
				string str = rtb.Rtf;
				string strline = rtb.Lines[0].ToString();
				char[] strSep = new char[strline.Length];
				for(int i = 0; i < strline.Length; i++)
				{
					strSep[i] = strline[i];
				}
				string[] strTotal = str.Split(strSep, 3);
				string strT = "";
				if (strTotal.Length > 0)
				{
					strT = strTotal[0] + strToAdd + " " + rtb.Lines[0] + strTotal[1];
				}
				rtb.Rtf = strT;
				string strLine = rtb.Lines[0];
				rtb.SelectionStart = rtb.Find(strLine);
				rtb.SelectionColor = Color.Black;
			}
		}
the error is thrown at rtb.Rtf = strT;
Regards,
Smacker
Smacker  Thursday, September 03, 2009 12:41 PM

Hi,

Have you figured out?

I test your code on my side. I didn’t get an error at rtb.Rtf = strT; . Could you please show us the error message?

Best reagards,

Ling Wang


Please remember to click “Mark as Answer?on the post that helps you, and to click “Unmark as Answer?if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Monday, September 07, 2009 9:51 AM
@Smacker

I personally code this thing and checked it, there is no error in it. it would be better to send me the error message so that i can figure out if any problem exist.
Regards,
AUmidh  Monday, September 07, 2009 12:57 PM

After a lot of test, the error should be “File format is not valid.?

It is not easy to change and write the rtf format text. It should follow Rich Text Format (RTF) Specification. (Rich Text Format (RTF) Specification, version 1.6: http://msdn.microsoft.com/en-us/library/aa140277(office.10).aspx )

However, changing the text is easier. For example:

int i = 0;

string str = "aaa";

private void button1_Click(object sender, EventArgs e)

{

richTextBox1.Text = str + (i++).ToString() + Environment.NewLine + richTextBox1.Text;

richTextBox1.SelectionStart = 0;

richTextBox1.SelectionLength = richTextBox1.Lines[0].Length;

richTextBox1.SelectionColor = Color.Blue;

}

This can add the text at the beginning.

Change the color is complex. Please refer to the following article. The author expands richtextbox control. It will tell you how to change the rtf to set the color and font.

http://www.codeproject.com/KB/edit/csexrichtextbox.aspx

Best regards,

Ling Wang


Please remember to click “Mark as Answer?on the post that helps you, and to click “Unmark as Answer?if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Tuesday, September 08, 2009 4:39 AM
sorry I been away yes the error was "file format is not valid"

I will check out that link and post my findings

Regards,
Smacker
Smacker  Tuesday, September 08, 2009 10:58 PM

You can use google to search for other answers

Custom Search

More Threads

• how to add prompt
• 'System.Windows.Forms.TreeNodeCollection' error
• Stop user from selecting another item in ListView
• Problem with opening forms in VC++2005 Express
• Run Framework 1.1 and 2.0 application at the same time on the same machine.
• opening a form
• forcing a network connection
• Quick Image.Save question
• what is the Perfect MDI Application
• Rectangular Arraylist or List