RTF document editor

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
13 posts Page 1 of 2
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

RTF document editor
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;
You do not have the required permissions to view the files attached to this post.
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
Martin
Supreme VIP
Supreme VIP
Posts: 369
Joined: Sat Aug 01, 2009 12:26 pm

Re: RTF document editor
Martin
Looks Good
Image
User avatar
CodemaN
VIP Access - Denied
VIP Access - Denied
Posts: 74
Joined: Fri Sep 18, 2009 3:18 pm

Re: RTF document editor
CodemaN
very good program!!!!
User avatar
Robby
VIP - Donator
VIP - Donator
Posts: 417
Joined: Mon Aug 03, 2009 4:01 am

Re: RTF document editor
Robby
wow come on i would have put a tutorial up but u already have.

And i like the colour thing i guess
My current Projects Listed Below:
Toixt++ Text Editor Just 10 or less more features to go!
Image
Image
User avatar
hungryhounduk
VIP - Site Partner
VIP - Site Partner
Posts: 2870
Joined: Mon Jul 27, 2009 11:58 am

Re: RTF document editor
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
Last edited by hungryhounduk on Fri Feb 19, 2010 11:52 am, edited 1 time in total.
Image
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

Re: RTF document editor
Scottie1972
This is cool.
Simple yet functinal.

Great job!

Scottie
Image
User avatar
mylescomputer
Just Registered
Just Registered
Posts: 7
Joined: Fri Sep 04, 2009 7:39 pm

Re: RTF document editor
mylescomputer
Thanks that really helped me!
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Re: RTF document editor
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.
Image
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: RTF document editor
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.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

Re: RTF document editor
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)
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
13 posts Page 1 of 2
Return to “Tutorials”