reqaurding rtf boxes
-
i noticed with a rtf box when i select a text that has diffrent size text to it and diffrent colors,and i change the fontstyle of the selected text...it changes the colors, and font of the entire selected string. and doesnt keep intact the individual styles of each char in the selected text. The teacher at my school asked us to write a method that would take into account each individual char's size, font, color ect. I have been trying to iterate through the rtf.SelectedText collection and isolate each char but how do i get the font and color of a single char in a rtf box ? Does this question make any sense? if not let me know and ill try to rewrite it . anywho any ideas would help... Jesse M The Code Project Is Your Friend...
-
i noticed with a rtf box when i select a text that has diffrent size text to it and diffrent colors,and i change the fontstyle of the selected text...it changes the colors, and font of the entire selected string. and doesnt keep intact the individual styles of each char in the selected text. The teacher at my school asked us to write a method that would take into account each individual char's size, font, color ect. I have been trying to iterate through the rtf.SelectedText collection and isolate each char but how do i get the font and color of a single char in a rtf box ? Does this question make any sense? if not let me know and ill try to rewrite it . anywho any ideas would help... Jesse M The Code Project Is Your Friend...
You need to go through your selection char by char (you can do a select(i,1) in a loop from start of select to end), and then for each char in selection, you can get the color by the oRTFBox.SelectionColor property. I just had to do something very similar to this. If you get stuck email me (from link) and I can email a code example.
There are only 10 types of people in this world....those that understand binary, and those that do not.
-
You need to go through your selection char by char (you can do a select(i,1) in a loop from start of select to end), and then for each char in selection, you can get the color by the oRTFBox.SelectionColor property. I just had to do something very similar to this. If you get stuck email me (from link) and I can email a code example.
There are only 10 types of people in this world....those that understand binary, and those that do not.
i whipped something together...maybe you can help me work with it. it doesnt seem to work correctly because the Rtf.Select() requires a start index....how can i find the start index of a specific char in the selected text ? heres the code i just kinda through together.
char[] chars = GetMainFormData.MainTextArea.SelectedText.ToCharArray(); Color[] cls= new Color[chars.Length]; int count =0; int tmp = GetMainFormData.MainTextArea.SelectedText.Length; for(int i =0;i Any ideas ? The Code Project Is Your Friend...
-
i whipped something together...maybe you can help me work with it. it doesnt seem to work correctly because the Rtf.Select() requires a start index....how can i find the start index of a specific char in the selected text ? heres the code i just kinda through together.
char[] chars = GetMainFormData.MainTextArea.SelectedText.ToCharArray(); Color[] cls= new Color[chars.Length]; int count =0; int tmp = GetMainFormData.MainTextArea.SelectedText.Length; for(int i =0;i Any ideas ? The Code Project Is Your Friend...
Here is some code I used for my latest project where I had to do something similar. ----------------
int iIndex = 0; while ( iIndex < (oBox.TextLength) ) { // Go one char at a time oBox.Select(iIndex,1); // ** oBox is the RichTextBox if (null != oBox.SelectionFont) { iSelectedCharTextSize = getTextSizeFromFont(oBox.SelectionFont); } if (true != oBox.SelectionColor.IsEmpty) { strSelectedCharColor = getHexColorString(oBox.SelectionColor); }
-------------- You see, though, I went through the whole box from the beginning...not just using what was selected. So you will probably have to find the beginning index based on what is selected. I wouldn't think it would be that hard. good luck. chris
There are only 10 types of people in this world....those that understand binary, and those that do not.
-
Here is some code I used for my latest project where I had to do something similar. ----------------
int iIndex = 0; while ( iIndex < (oBox.TextLength) ) { // Go one char at a time oBox.Select(iIndex,1); // ** oBox is the RichTextBox if (null != oBox.SelectionFont) { iSelectedCharTextSize = getTextSizeFromFont(oBox.SelectionFont); } if (true != oBox.SelectionColor.IsEmpty) { strSelectedCharColor = getHexColorString(oBox.SelectionColor); }
-------------- You see, though, I went through the whole box from the beginning...not just using what was selected. So you will probably have to find the beginning index based on what is selected. I wouldn't think it would be that hard. good luck. chris
There are only 10 types of people in this world....those that understand binary, and those that do not.
-
thanks for the code...ill have to work on it tonight =)...anywho...is getHexColorString , getTextSizeFromFont a function you wrote ? what do those functions do if so ? thanks Jesse M The Code Project Is Your Friend...
Yes, getHexColorString and getTextSizeFromFont are my own methods. Sorry...had I realized I had left them in, I'd have taken them out so as not to confuse you. getHexColorString() takes the current char, determines the color and returns the number representing the color as a hexadecimal string (e.g. black text would return "000000"). getTextSizeFromFont() gets the font size and determines which HTML text size most closely matches it. As you may have guessed, what I'm writing is something similar (but not quite) like an RTF2HTML conversion program.
There are only 10 types of people in this world....those that understand binary, and those that do not.
-
Yes, getHexColorString and getTextSizeFromFont are my own methods. Sorry...had I realized I had left them in, I'd have taken them out so as not to confuse you. getHexColorString() takes the current char, determines the color and returns the number representing the color as a hexadecimal string (e.g. black text would return "000000"). getTextSizeFromFont() gets the font size and determines which HTML text size most closely matches it. As you may have guessed, what I'm writing is something similar (but not quite) like an RTF2HTML conversion program.
There are only 10 types of people in this world....those that understand binary, and those that do not.
Well i got the code to work perfectly...but perhaps you could help me optimize it ? it is incredibly unefficent....because it moves char by char...and each time it changes the font / color of that char it has to update the hole rtf box. if i could find away to do all this work in memory...the speed increase would be incredible. The following code seems to work good....but any ideas on how i can perform the same actions in memory?
int iIndex = 0; int SelSt = GetMainFormData.MainTextArea.SelectionStart; Color[] jr = new Color[GetMainFormData.MainTextArea.SelectedText.Length]; Font[] fr = new Font[GetMainFormData.MainTextArea.SelectedText.Length]; // //MessageBox.Show(GetMainFormData.MainTextArea.SelectionStart.ToString()); // int tmp = GetMainFormData.MainTextArea.SelectionLength; GetMainFormData.MainTextArea.Visible = false; while ( iIndex < (tmp) ) { // Go one char at a time GetMainFormData.MainTextArea.Select(SelSt,1); if (null != GetMainFormData.MainTextArea.SelectionFont) { fr[iIndex] = GetMainFormData.MainTextArea.SelectionFont; Font tmpF = (Font)fr[iIndex]; if(tmpF == fd.Font) { GetMainFormData.MainTextArea.SelectionFont = fd.Font; } else if(tmpF != fd.Font) { Font fonf = new Font(tmpF.FontFamily,fd.Font.Size,fd.Font.Style); GetMainFormData.MainTextArea.SelectionFont = fonf; } } if (true != GetMainFormData.MainTextArea.SelectionColor.IsEmpty) { jr[iIndex] = GetMainFormData.MainTextArea.SelectionColor; GetMainFormData.MainTextArea.SelectionColor = jr[iIndex]; } SelSt++; iIndex++; }
Jesse M