color change
-
Hai I am trying to change the color of a word, while typing itself, for that i wrote the code as follows. private: System::Void keypressevent(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) { array<String^>^ keyword = gcnew array<String^>{"#include","#define","struct"}; int length = keyword->Length; String ^ strRTF = this->richTextBox1->Rtf; this->richTextBox1->Clear(); int iCTableStart = strRTF->IndexOf("colortbl;"); if (iCTableStart != -1) { int iCTableEnd = strRTF->IndexOf('}', iCTableStart); strRTF = strRTF->Remove(iCTableStart, iCTableEnd - iCTableStart); strRTF = strRTF->Insert(iCTableStart, "colortbl ;\\red255\\green0\\blue0;\\red0\\green128\\blue0;\\red0\\green0\\blue255;}"); } else { int iRTFLoc = strRTF->IndexOf("\\rtf"); int iInsertLoc = strRTF->IndexOf('{', iRTFLoc); if (iInsertLoc == -1) iInsertLoc = strRTF->IndexOf('}', iRTFLoc) - 1; strRTF = strRTF->Insert(iInsertLoc, "{\\colortbl ;\\red128\\green0\\blue0;\\red0\\green128\\blue0;\\red0\\green0\\blue255;}"); } int len = strRTF->Length; String^ tempst; String^ result; String ^key; int keylen; int iRTFLoc; bool found = false; bool starcomment = true; int nlen = strRTF->Length; for(int nchRTF = 0;nchRTF < nlen;nchRTF++) { if(strRTF[nchRTF] == ' ') { for(int nkeyword = 0;nkeyword<length;nkeyword++) { key = keyword[nkeyword]; keylen = key->Length; iRTFLoc = tempst->IndexOf(keyword[nkeyword]); if(tempst == key) { found = true; break; } } if(found) { if(starcomment) { int location = 4+iRTFLoc+keylen+1; tempst = tempst->Insert( iRTFLoc,"\\cf3 "); tempst = tempst->Insert( location,"\\cf0 "); } found = false; } tempst = String::Concat(tempst, strRTF[nchRTF]); result = String::Concat(result,tempst); tempst = String::Empty; } else { tempst = String::Concat(tempst,strRTF[nchRTF] ); } } result = String::Concat(result,tempst); tempst = String::Empty; this->richTextBox1->Rtf = result; } Now my problem is when i am typing the code in the richtextbox it will be displayed like "diov" instead of "void". I i think its cursor position problem. can you tell me how to recover this.
-
Hai I am trying to change the color of a word, while typing itself, for that i wrote the code as follows. private: System::Void keypressevent(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) { array<String^>^ keyword = gcnew array<String^>{"#include","#define","struct"}; int length = keyword->Length; String ^ strRTF = this->richTextBox1->Rtf; this->richTextBox1->Clear(); int iCTableStart = strRTF->IndexOf("colortbl;"); if (iCTableStart != -1) { int iCTableEnd = strRTF->IndexOf('}', iCTableStart); strRTF = strRTF->Remove(iCTableStart, iCTableEnd - iCTableStart); strRTF = strRTF->Insert(iCTableStart, "colortbl ;\\red255\\green0\\blue0;\\red0\\green128\\blue0;\\red0\\green0\\blue255;}"); } else { int iRTFLoc = strRTF->IndexOf("\\rtf"); int iInsertLoc = strRTF->IndexOf('{', iRTFLoc); if (iInsertLoc == -1) iInsertLoc = strRTF->IndexOf('}', iRTFLoc) - 1; strRTF = strRTF->Insert(iInsertLoc, "{\\colortbl ;\\red128\\green0\\blue0;\\red0\\green128\\blue0;\\red0\\green0\\blue255;}"); } int len = strRTF->Length; String^ tempst; String^ result; String ^key; int keylen; int iRTFLoc; bool found = false; bool starcomment = true; int nlen = strRTF->Length; for(int nchRTF = 0;nchRTF < nlen;nchRTF++) { if(strRTF[nchRTF] == ' ') { for(int nkeyword = 0;nkeyword<length;nkeyword++) { key = keyword[nkeyword]; keylen = key->Length; iRTFLoc = tempst->IndexOf(keyword[nkeyword]); if(tempst == key) { found = true; break; } } if(found) { if(starcomment) { int location = 4+iRTFLoc+keylen+1; tempst = tempst->Insert( iRTFLoc,"\\cf3 "); tempst = tempst->Insert( location,"\\cf0 "); } found = false; } tempst = String::Concat(tempst, strRTF[nchRTF]); result = String::Concat(result,tempst); tempst = String::Empty; } else { tempst = String::Concat(tempst,strRTF[nchRTF] ); } } result = String::Concat(result,tempst); tempst = String::Empty; this->richTextBox1->Rtf = result; } Now my problem is when i am typing the code in the richtextbox it will be displayed like "diov" instead of "void". I i think its cursor position problem. can you tell me how to recover this.
What does the cursor have to do with any of this code? I'm curious if you know what any of this code does or if you're just using snippets of code obtained from others without knowing how it works... :confused: If you post code in a readable format (use the PRE tags!) you may get more and/or better responses here. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: