Page 1 of 2

RTF document editor

Posted: Fri Aug 21, 2009 1:23 am
by CodenStuff
Hello,

I was just going through my project folder and found this document editor I was working on some time ago. Its only basic but may help you if you want to make a text editor.

Image

Features include:
Save & Load as *.rtf file (saves font/sizes/colour etc rather than just plain text)
Change text colour
Change text font/size
Change font style


Here is how to make this little application. First you will need to add the following controls to your form:

MenuStrip - add 3 fields into this control "Save"..."Load"...and "Exit"
14 Buttons
1 RichtextBox


Button 1 - 5 = Used to select font style
Buttons 6 - 14 = Used to select colours
Button 15 = Choose font

OK once you have done that and arranged the controls as you want them you need to stretch out the richtextbox so it fits most of the form, make it nice and big.

Then you can simply copy and paste this code over:
Code: Select all
Public Class Form1

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        RichTextBox1.SelectionColor = Color.LightSlateGray
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RichTextBox1.SelectionFont = New Font(Me.RichTextBox1.SelectionFont, FontStyle.Bold)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        RichTextBox1.SelectionFont = New Font(Me.RichTextBox1.SelectionFont, FontStyle.Regular)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        RichTextBox1.SelectionFont = New Font(Me.RichTextBox1.SelectionFont, FontStyle.Italic)
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        RichTextBox1.SelectionFont = New Font(Me.RichTextBox1.SelectionFont, FontStyle.Strikeout)
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        RichTextBox1.SelectionFont = New Font(Me.RichTextBox1.SelectionFont, FontStyle.Underline)
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        RichTextBox1.SelectionColor = Color.Red
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        RichTextBox1.SelectionColor = Color.Blue
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        RichTextBox1.SelectionColor = Color.Purple
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        RichTextBox1.SelectionColor = Color.Black
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        RichTextBox1.SelectionColor = Color.Pink
    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        RichTextBox1.SelectionColor = Color.Yellow
    End Sub

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        RichTextBox1.SelectionColor = Color.Green
    End Sub

    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
        RichTextBox1.SelectionColor = Color.Orange
    End Sub
    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        Dim savefiledialog1 As New SaveFileDialog
        savefiledialog1.Title = "Save File"
        savefiledialog1.FileName = "*.rtf"
        savefiledialog1.Filter = "RTF |*.rtf"
        If savefiledialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            RichTextBox1.SaveFile(savefiledialog1.FileName, RichTextBoxStreamType.RichText)
        End If
    End Sub

    Private Sub LoadToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadToolStripMenuItem.Click
        Dim openfiledialog1 As New OpenFileDialog
        openfiledialog1.Title = "Save File"
        openfiledialog1.FileName = "*.rtf"
        openfiledialog1.Filter = "RTF |*.rtf"
        If openfiledialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            RichTextBox1.LoadFile(openfiledialog1.FileName, RichTextBoxStreamType.RichText)
        End If
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub
    
    Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
        Dim fontdialog1 As New FontDialog
        If fontdialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            RichTextBox1.SelectionFont = fontdialog1.Font
        End If
    End Sub
End Class

If everything went ok you should now be able to run and use your basic text editor :D .

Source-Code:
DocPad.zip
Happy coding cooll;

Re: RTF document editor

Posted: Thu Aug 27, 2009 1:21 pm
by Martin
Looks Good

Re: RTF document editor

Posted: Fri Sep 18, 2009 5:47 pm
by CodemaN
very good program!!!!

Re: RTF document editor

Posted: Mon Nov 16, 2009 11:07 pm
by Robby
wow come on i would have put a tutorial up but u already have.

And i like the colour thing i guess

Re: RTF document editor

Posted: Wed Feb 17, 2010 1:06 pm
by hungryhounduk
Hi Codenstuff
Wow i missed this little Gem :)

Cheers fella, as always quality apps from da man :)

** I have used this code and modified it slightly here and there to create the CnSDesktop RTF Editor** :)

Chris

Re: RTF document editor

Posted: Wed Feb 17, 2010 2:21 pm
by Scottie1972
This is cool.
Simple yet functinal.

Great job!

Scottie

Re: RTF document editor

Posted: Fri Feb 19, 2010 8:36 am
by mylescomputer
Thanks that really helped me!

Re: RTF document editor

Posted: Mon Nov 22, 2010 6:47 pm
by Usman55
Hello codenstuff,

Its a cool wordpad to be a beginner's one. But when I click the bold button, the text is bold and when I click the italic button, the text is italic. But it doesn't becomes bold and italic at one time. Isn't it possible that the fontstyle buttons behave like a real wordpad? Please reply.

Thank you.

Re: RTF document editor

Posted: Mon Nov 22, 2010 6:59 pm
by MrAksel
Try this usman:
This is for italic button
Code: Select all
RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont.FamilyName, RichTextBox1.SelectionFont.FontStyle Or FontStyle.Italic)
An this is for the bold button
Code: Select all
RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont.FamilyName, RichTextBox1.SelectionFont.FontStyle Or FontStyle.Bold)
I have never tested it, you might have to edit a little.

Re: RTF document editor

Posted: Tue Nov 23, 2010 3:30 am
by CodenStuff
Well this tutorial was just a quicky to demonstrate font selections you will probably need to add a function somewhere that checks if a specific font style is activated..like if you click the bold button and if you then want it to be italic aswell you will need to check if bold is already active. Or something like that lol.

You could do it in one button like:
Code: Select all
RichTextBox1.SelectionFont = New Font(Me.RichTextBox1.SelectionFont, FontStyle.Bold + FontStyle.Italic)