Click here to Skip to main content
15,879,045 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have a problem with follow code:
C#
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    richTextBox1.SelectionColor = Color.Red;
}
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\t')
    {
        e.Handled = true;
        richTextBox1.Text += "hell0;
    }
}

I only want when I press tab key, richtextbox add "hell0".and text in richtextbox not change color. but it change black. I don't know why.
please help me! thank so much.
Posted
Updated 6-Mar-11 21:44pm
v2

Make sure that the property TabStop is set to False
 
Share this answer
 
Comments
OriginalGriff 7-Mar-11 4:26am    
I think you mean "AcceptsTab" and "true"...
Ryan Zahra 7-Mar-11 4:29am    
I tested with "TabStop" and "False" and it worked fine...
OriginalGriff 7-Mar-11 4:36am    
TabStop just says that Windows should move to it from another control (i.e. if it appears in teh tab order). AcceptsTab decides if the tab key should be passed to the KeyPress event: the default is false...
Ryan Zahra 7-Mar-11 4:38am    
You're right. Thanks for clearing it up :)
OriginalGriff 7-Mar-11 4:39am    
No problem! :laugh:
The KeyPress event is raising before the TextChanged event.
First, make sure that the TabStop property is false.
Second, the += operator doesn't work for changing text color. So, you need to change the code into this:
C#
private void richTextBox1_TextChanged(object sender, EventArgs e)
{

}
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\t')
    {
        richTextBox1.SelectionColor = Color.Red; // set selection color to red before appending text
        e.Handled = true;
        richTextBox1.AppendText("hell0"); // change richTextBox1.Text += "hell0" into richTextBox1.AppendText("hell0");
    }
}

Hope this helps.
 
Share this answer
 
v2
If I've understood your question, you need to change the color of "hello" to black.
You can select "hello" in the text and then change color just for that piece of selected to black.
 
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