about richtext

Hi

I'm new to avalon and i've a small doubt on richtext box. Here i'm entering some message in rich text box and i dont know how to save that message also how to load external file(.rtf) into current richtext box.

thanx

Nagu



Answer this question

about richtext

  • ikamran65536

    private static void LoadFile(string filename, RichTextBox richTextBox) {
    if (string.IsNullOrEmpty(filename)) {
    throw new ArgumentNullException();
    }
    if (!File.Exists(filename)) {
    throw new FileNotFoundException();
    }

    // open the file for reading
    using (FileStream stream = File.OpenRead(filename)) {
    // create a TextRange around the entire document
    TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);

    // sniff out what data format you've got
    string dataFormat = DataFormats.Text;
    string ext = System.IO.Path.GetExtension(filename);
    if (String.Compare(ext, ".xaml",true) == 0) {
    dataFormat = DataFormats.Xaml;
    }
    else if (String.Compare(ext, ".rtf", true) == 0) {
    dataFormat = DataFormats.Rtf;
    }
    documentTextRange.Load(stream, dataFormat);
    }
    }


  • O.S.S

    Hi viliescu i'm giving like this but it shows like : The name 'LoadFile' does not exist in the current context , i'm using Feb2006 ctp. Is there any alternate solution for this and also how to apply alignment properties like right, left , justify for richtext box content

    thanx for reply

    Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
    ofd.Filter = "Text Files (*.txt; *.xaml; *.rtf)|*.txt;*.xaml;*.rtf";
    ofd.Multiselect = false;
    if (ofd.ShowDialog() == true)
    {
    LoadFile(ofd.SafeFileName, myTextBox);
    }


  • glebur

    Several things to note here.

    Looks like you are extracting the text dataformat from the problem you describing.

    From the code provided, it can be seen that the rtf format is retrieved on reading a file with the .rtf extension Otherwise, the text format is read.

    If the problem is not the above, its likely that the rtf being generated is faulty. Simple way to test this is to copy the content from the file and paste it in RichTextbox. If it renders incorrectly you are generating wrong rtf.

    HTH


  • Mike Wentzel

    Can anybody give me a simple example about the richtext box . I already mentioned the problem. With the above code i get the external file in current richtext box its ok but i didnt get the fontstyles and color properities thogh the external file has that effects i'm mad about that

    thanx - Nagu


  • sdmf

    Hi I dont know any thing about blog i'm using this from just few days would you give me a simple example based on that

    thans

    Nagu


  • hege

    those are static functions provided in the blog :)

    as for alignment you could set the TextAlignment property on the FlowDocument of the richtextbox


  • Steve.W

    would you give me a simple example

    thanx

    Nagu


  • tintin1969

  • dutch blue

    LoadFile/SaveFile methods are defined in the same blog entry. Did you add them to your code


  • LuckyHusband

    oh its working fine thanx for your reply. But a small problem while Loading external file i.e the content of external file is like this

    1.One
    2.Two
    3.Three
    4.Four

    with some other text effects like font colors , font styles but it displays like this

    1.One2.Two3.Three4.Four and also its not displaying the text effects.

    Thanx
    Nagu


  • about richtext