Skip to content
Loading
  • Amit Gupta
    +1
    richTxtLetter.SelectionFont = new Font(richTxtLetter.Font, FontStyle.Bold| FontStyle.Italic);
    +1
  • Amit Gupta
    +1
    FontStyle is an enum, you can apply all their styles by | symbol
    +1
  • +1
    Hey Amit,
     
    Nice to see you... thanks for the reply...
    buddy, that's not working in my case. user types a paragraph and then selects font, bold, italics etc by selecting the desired text and then selects the options form right click.
     
    let's assume that I select Times New Roman as letter font. The default font is Arial.
     
    now my problems... suppose I want a word to show as Bold with Underline.
    1. When I select a word and apply Bold,  the font changes to Arial and Bold.
    2. When I select the same word for apply Underline, the Bold removes and Underline applied.
     
    If I apply the code...
    richTxtLetter.SelectionFont = new Font(richTxtLetter.Font, FontStyle.Bold| FontStyle.Underline);
    then when I select Bold only, it gets Underline with Bold.
     
    Please tell me solution of this and also let me know if you want to know anything else...
     
    Thanks
     
    Mayank Jani
    +1
  • James Axsom
    +1
    Hi Mayank.
    Try something like this... 

    Create a new function that returns a font which your menu item click events will call. Create an enumeration and name it something like FontStyleSelection like this…

    1. public enum FontStyleSelection {Bold, Italic, Underline, BoldItalic, BoldUnderline, ItalicUnderline, UnderlineBold }   
    then your function..

    1. public Font GetFont(string fontFamily, int fontSize, FontStyleSelection selections )  
    2. {  
    3.    FontStyle fontStyle;  
    4.    // go the through the litany of evaluating  
    5.    If (selections == FontStyleSelection.Bold)  
    6.    {  
    7.       fontStyle = FontStyle.Bold;  
    8.    }  
    9.    If (selections == FontStyleSelection.Italic)  
    10.    {  
    11.       fontStyle = FontStyle.Italic;  
    12.    }  
    13.    If (selections == FontStyleSelection.Underline)  
    14.    {  
    15.       fontStyle = FontStyle.Underline;  
    16.    }  
    17.    If (selections == FontStyleSelection. BoldItalic)  
    18.    {  
    19.       fontStyle = FontStyle.Bold | FontStyle.Italic;  
    20.    }  
    21.    If (selections == FontStyleSelection. ItalicUnderline)  
    22.    {  
    23.       fontStyle = FontStyle.Italic | FontStyle.Underline;  
    24.    }  
    25.    If (selections == FontStyleSelection.BoldUnderline)  
    26.    {  
    27.       fontStyle = FontStyle.Bold | FontStyle.Underline;  
    28.    }  
    29.    Font font = new Font( fontfamily, fontSize);  
    30.    font.Font.FontStyle = fontStyle;  
    31.    return font;  
    32. }  

    The main take away from this is, if you want to combine options use the or operator, the bar character | to separate each style choice. You can even combine more than 2 or 3 if you want. You shouldn’t need to keep track of selection states because one state will replace the previous state.

    +1
  • 0
    Hey Amit,
     
    Thank You sooooooo much once again....
     
    It's done. I know what I want to do but (sometimes) I do not know how to do. I learn myself with help of the net. I think by the user's point of view and try to make it as easy as possible. also I try to keep coding simple, that's why I do not know the many functions or 'shortcuts' except I found on the net...
     
    ...and learn from expert like you :) you have helped me many times. I thank you for that once again.
     
    Mayank Jani.
    0
  • 0
    Hiiii James,
     
    Thank you for your help and time. I tried the code but got some error. I think your code is correct but may be I am unable to understand properly or unable to implement.
     
    Amit's solution worked because it fitted with the format I am used to. I will try the code you provided and it will definately teach me something new.
     
    Thank you
     
    Mayank Jani
    0