none
how do i set selected text to italic c# RRS feed

  • Question

  • The docs describe a FontStyle class with an Italic member but don't show how to assign a FontStyle to selected text in a RichTextBox.

    I tried a Font Dialog box and checked the italic box. The selected text changed to italic. But when I saved the file containing the RichTextBox (saving the .Rtf of the RichTextBox) and re-opened the file it was no longer italic - altho the font name and size were correct.

    Thanks for any help in doing this.

    Ken

    Friday, November 1, 2019 9:14 PM

All replies

  • Open Internet Options/select Font tab/edit..

    https://docs.microsoft.com/en-us/typography/

    the above link gets you Font installations.

    Friday, November 1, 2019 10:30 PM
  • Thanks for your reply.

    I explained in my original post that I successfully changed the selected text to italic, but it didn't save it - when I saved the file and then re-opened it the selected text was no longer in italic.

    The fact that the selected text did appear in italics seems to indicate that the font is installed complete with italics, no? The problem seems to be that it didn't store the italics.

    What am I missing?

    Regards,

    Ken

    Saturday, November 2, 2019 11:02 AM
  • OK. I went to the Control Panel and checked my fonts. I have all the Arial fonts installed - italic, bold italic, &c.

    So what next?

    Thanks &

    Regards

    Ken

    Sunday, November 3, 2019 10:35 AM
  • More specifially I'm using:                   

    tempNote.textBox.SaveFile(saveTBFileName);

    to save the RichTextBox

    Regards,

    Ken

    Sunday, November 3, 2019 10:42 AM
  • Hi ken quirici,

    >> tempNote.textBox.SaveFile(saveTBFileName) to save the RichTextBox;

    What is relation between the method textBox.SaveFile(saveTBFileName) and RichTextBox?
    In my test, I used method RichTextBox.SaveFile and didn’t reproduce your situation. Here is my code and you can refer to it.

          private void RichTextBox1_TextChanged(object sender, EventArgs e)
            {
                richTextBox1.SelectionFont = new Font("Arial", 16, FontStyle.Italic);
            }
          private void Button1_Click(object sender, EventArgs e)
            {
                richTextBox1.SaveFile(@"D:\test3.rtf");
            } 
          private void Button2_Click(object sender, EventArgs e)
            {
                richTextBox1.LoadFile(@"D:\test3.rtf");
            }
    Best Regards,
    Daniel Zhang

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, November 4, 2019 2:54 AM
  • Thanks! I'll check it out

    Regards,

    Ken

    Monday, November 4, 2019 2:56 AM
  • Hi,

    Thanks for your reply.

    The textbox in my previous sample is a RichTextBox - your way of including 'Rich' in the name is better.

    I'm using a FontDialog to get the font. When I select, I select "Italic" as well as "Arial" and size (usually 18). Please note that this code successully changes the selected text to italic except that the save doesn't save it, or maybe the open doesn't open it. I'll check that next.

    So hang on until I get that part.

    Here's the code:

            if (fontDialog1.ShowDialog() != DialogResult.Cancel)
            {
                if (noteTextBox.SelectionLength == 0)
                {
                    noteTextBox.Font = fontDialog1.Font;
                    noteTextBox.ForeColor = fontDialog1.Color;
                }
                else
                {
                    noteTextBox.SelectionFont = fontDialog1.Font;
                    noteTextBox.SelectionColor = fontDialog1.Color;
                }
            }
    

    Ken

    Monday, November 4, 2019 12:06 PM
  • I just checked. When I open the file I do:

                    mytextBox = new textBox1();
                    if(File.Exists(saveTBFileName)) nttextBox.LoadFile(saveTBFileName);
    

    mytextbox is defined as a textBox1 which is classed as a RichTextBox. I assume SaveFile and LoadFile preserve all the variations in text fonts, sizes, and styles in the RichTextBox.

    Regards,

    Ken

    Monday, November 4, 2019 12:30 PM
  • Hi ken quirici,

    So does this issue handled?

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, November 5, 2019 6:01 AM
  • No it isn't. I've been working on other stuff and just now returned to that problem. IT's still a problem.

    I can set a selected word (e.g.) to italic. It looks fine. But as soon as a select the 'Save' option in the file menu, it reverts to non-italic.

    I've set this aside for now. Thanks for your help. I just don't have time right now. Apart from this problem the app is working and I'm able to use it.

    Thanks for help.

    Regards,

    Ken

    Wednesday, November 6, 2019 12:50 AM