rich text box

Hi,

I placed the rich text box,and i applied some font settings to the selected text of rich text box.

Now to save the changes to a file i used the following statement

richTextBox1.SelectionFont = new Font(fontname, fontsize, fstyle);

richTextBox1.SaveFile(fn, RichTextBoxStreamType.RichText);

where fn contains th filename.

but now also the changes are not applied to the file.

Please help me.




Answer this question

rich text box

  • thewizster

    man you are open the file and display it in richtextbox then you simply change the font style in your ritchtextbox when you finish editing every thing you simply save the ritchtextbox back to the origional file so your formating suppose to be applied to the richtextbox control then you save your richtextbox control to .rtf file, you don't need to care about formating data in the file

    best regards



  • brent_d

    hi,

    i have did it like this

    to change font property in the rtb select part of the text and click on this button

    private void btnbold_Click(object sender, EventArgs e)
    {
    Font oldFont;
    Font newFont;

    //get the font that is used in the rtf selection font
    oldFont = rtftext.SelectionFont;

    //if the font is allready bold we remove the format
    if (oldFont.Bold)
    {
    newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);
    }
    else
    {
    newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);
    }

    //insert the new font instead selected font and return forcus to rtftext
    rtftext.SelectionFont = newFont;
    rtftext.Focus();
    }

    to save the textbox tex

    private void btnsave_Click(object sender, EventArgs e)
    {
    try
    {
    rtftext.SaveFile("C:\\Documents and Settings\\Kakamo\\Desktop\\mimi.rtf");
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }

    hope this helps



  • Sammu

    Hi,

    I tried already what u said.

    but now also it's not working.

    Please help me.



  • pnuruddin

    ok here its the entire example that i wrote as professional c# book indecated just make form and put the following control and change the file path in save and load buttons

    also it doesn't deal with font color in the richtextbox control but you can add to it

    //this form contain 4 buttons at top (btnbold,btnitalic,btnunderline,btnCenter)

    //also contain textbox txtsize and a richtextbox called rtftext

    //also contain 2button at buttom(btnload,btnsave)

    namespace WindowsControls

    {

    public partial class RichTextBox : Form

    {

    public RichTextBox()

    {

    InitializeComponent();

    //add handlers for the size textbox

    txtsize.KeyPress += new KeyPressEventHandler(txtsize_KeyPress);

    txtsize.Validating += new CancelEventHandler(txtsize_Validating);

    //this part to start IE if we click a link in the page

    this.rtftext.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.rtftext_LinkClicked);

    }

    private void btnbold_Click(object sender, EventArgs e)

    {

    Font oldFont;

    Font newFont;

    //get the font that is used in the rtf selection font

    oldFont = rtftext.SelectionFont;

    //if the font is allready bold we remove the format

    if (oldFont.Bold)

    {

    newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);

    }

    else

    {

    newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);

    }

    //insert the new font instead selected font and return forcus to rtftext

    rtftext.SelectionFont = newFont;

    rtftext.Focus();

    }

    private void btnitalic_Click(object sender, EventArgs e)

    {

    Font oldFont;

    Font newFont;

    //get the font that is used in the rtf selection font

    oldFont = rtftext.SelectionFont;

    //if the font is allready bold we remove the format

    if (oldFont.Italic)

    {

    newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic);

    }

    else

    {

    newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic);

    }

    //insert the new font

    rtftext.SelectionFont = newFont;

    //give the focus back to the rtfbox

    rtftext.Focus();

    }

    private void btnunderline_Click(object sender, EventArgs e)

    {

    Font oldFont;

    Font newFont;

    //get the font that is used in the rtf selection font

    oldFont = rtftext.SelectionFont;

    //if the font is allready bold we remove the format

    if (oldFont.Underline)

    {

    newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Underline);

    }

    else

    {

    newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline);

    }

    //insert the new font

    rtftext.SelectionFont = newFont;

    //give the focus back to the rtfbox

    rtftext.Focus();

    }

    private void btncenter_Click(object sender, EventArgs e)

    {

    if (rtftext.SelectionAlignment == HorizontalAlignment.Center)

    {

    rtftext.SelectionAlignment = HorizontalAlignment.Left;

    }

    else

    {

    rtftext.SelectionAlignment = HorizontalAlignment.Center;

    }

    //give focuse back to rtftext box

    rtftext.Focus();

    }

    private void txtsize_KeyPress(object sender, KeyPressEventArgs e)

    {

    //don't allow any character that is not a number

    //but allow back space and enter

    if ((e.KeyChar < 48 || e.KeyChar > 57) &&

    e.KeyChar != 8 && e.KeyChar != 13)

    {

    e.Handled = true;

    }

    else if (e.KeyChar == 13)

    {

    //apply the size if user hit enter

    TextBox txt = (TextBox)sender;

    if (txt.Text.Length > 0)

    {

    ApplyTextSize(txt.Text);

    e.Handled = true;

    //give focus to rtf

    rtftext.Focus();

    }

    }

    }

    private void txtsize_Validating(object sender, CancelEventArgs e)

    {

    TextBox txt = (TextBox)sender;

    ApplyTextSize(txt.Text);

    rtftext.Focus();

    }

    private void ApplyTextSize(string TextSize)

    {

    //convert the text to a float

    float newSize = float.Parse(TextSize);

    FontFamily CurrentFontFamily;

    Font NewFont;

    //capture the current fontfamily and make a new font

    //from the same family but with different size

    CurrentFontFamily = rtftext.SelectionFont.FontFamily;

    NewFont = new Font(CurrentFontFamily, newSize);

    //replace the oldfont witht the new one

    rtftext.SelectionFont = NewFont;

    }

    private void rtftext_LinkClicked(object sender, LinkClickedEventArgs e)

    {

    System.Diagnostics.Process.Start(e.LinkText);

    }

    private void btnload_Click(object sender, EventArgs e)

    {

    try

    {

    rtftext.LoadFile("C:\\Documents and Settings\\Kakamo\\Desktop\\mimi.rtf");

    }

    catch (Exception ex)

    {

    MessageBox.Show(ex.Message);

    }

    }

    private void btnsave_Click(object sender, EventArgs e)

    {

    try

    {

    rtftext.SaveFile("C:\\Documents and Settings\\Kakamo\\Desktop\\mimi.rtf");

    }

    catch (Exception ex)

    {

    MessageBox.Show(ex.Message);

    }

    }

    }

    }

    hope this helps



  • orid

    Hai,

    I know richtextbox.savefile("C:\..\.rtf");

    iam developing a notepad application.i opened a file and apply the font and color settings.now i want to save the file.fonts are applying to the file,but again i opened the file,fonts are not applied.

    Please help me in this aspect.

    Thank u for ur valuble information.



  • rich text box