Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. reqaurding rtf boxes

reqaurding rtf boxes

Scheduled Pinned Locked Moved C#
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jtmtv18
    wrote on last edited by
    #1

    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...

    K 1 Reply Last reply
    0
    • J jtmtv18

      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...

      K Offline
      K Offline
      KingTermite
      wrote on last edited by
      #2

      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.

      J 1 Reply Last reply
      0
      • K KingTermite

        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.

        J Offline
        J Offline
        jtmtv18
        wrote on last edited by
        #3

        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...

        K 1 Reply Last reply
        0
        • J jtmtv18

          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...

          K Offline
          K Offline
          KingTermite
          wrote on last edited by
          #4

          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.

          J 1 Reply Last reply
          0
          • K KingTermite

            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.

            J Offline
            J Offline
            jtmtv18
            wrote on last edited by
            #5

            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...

            K 1 Reply Last reply
            0
            • J jtmtv18

              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...

              K Offline
              K Offline
              KingTermite
              wrote on last edited by
              #6

              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.

              J 1 Reply Last reply
              0
              • K KingTermite

                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.

                J Offline
                J Offline
                jtmtv18
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups