The RichTextBox control is possibly the most advanced control in the .Net collection.With relatively small amounts of code, a program to rival wordpad can be created.
Microsoft created the RichTextBox using the com-object RichEdit(RichEdit32.dll) which is included in windows.
Thanks to Microsoft, loading a complete rtf document including pictures and tables can be loaded into the box using 1 simple method: loadfile which could use the output from an OpenFileDialog.
Adjusting fonts/colours in the RichTextBox is a bit more complex, but .Net takes care of asking the user what fonts / colours to use through the FontDialog and ColorDialog dialogs( :) ). This just leaves us to set the selected texts font.
The hardest thing to do with a RichTextBox is to do any form of pagination (creating virtual pages, rather than a wordpad style long multi-line textbox.) Currently there is no easy way of doing this. It is easier to do in Publisher style (tabbed pages) than Word (scrolling pages). I will leave it up to you and your imagination to implement this very advanced feature.

I have thought about this, and have decided that in future all code in this blog will be written in C#. This is not to say that I don’t like the other languages, it is simply because C# can be easily understood through logical reading, and as such can be quickly converted into VB .Net or C++/CLI
If there is a large/un-logical difference between C# and any of these languages (for example C# ref and C++ pointers) then I will add code in the other languages.
I am aware that this is not ideal for beginners, but it will be best for maintaining this blog.
modsRule

Drawing in .Net is actually quite simply due to the System.Drawing namespace. (Must be referenced into the project before tyring to use it! A simple mistake often overlooked!)

Normally a picture box is used for drawing custom shapes directly into a form, since it allows the location that the drawings will beto be modified by simply moving the picture box in the designer. However any control that has a Paint Method can be quite simply drawn on in the same way that i will demonstrate with the picture box! (Although drawing onto toolstrips and menu strips is best done through creating a new ToolStripRenderer class!)

The first stage is to get the designer to create a method for the painting of a picture box.
Below is the method for the paint event of the picture box.
(VB .NET)

    Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

    End Sub

(C#)

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
        }

(C++/CLI)

    private: System::Void pictureBox1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
             }

From now on I shall use C# to demonstrate the rest of this drawing process. But I will put the final code in all 3 languages.

It is easiest if I show you line by line.

Pen p = new Pen(new SolidBrush(Color.Blue));

So, first we need to create a Pen to draw with. Here we create a pen pass it a SolidBrush which we have set to be blue.
Imagine a normal pen. The Solid Brush is like using an Ink Cartridge, it will always be the colour we set it (In this case blue)
We then fill the pen with the Ink Cartridge (The Solid Brush)

Other Brush types are:

  • LinearGradientBrush – This allows you to have stripes
  • HatchBrush – Allows Hatching
  • TextureBrsuh – Allows textures such as images to be painted

The next step is to draw something.

e.Graphics.DrawLine(p, new Point(5, 5), new Point(20, 5));

This step uses e (The PaintEventArgs) .Graphics class
This allows us access to method such as the one we use here DrawLine.
DrawLine takes a reference to the Pen (p) a start point for the line and an end point for the line, (5,5) and (20,5) respectively, in the form (x coord, y coord). For those who haven’t used coordinates for a while: The x axis goes across horizontally. The Y axis goes vertically. With (0,0) being the top left of the control.

If you run it now you will get a line in blue going horizontally in the top-left corner of the picture box. It is not a very long line just 15 units long.
Try changing the start and end coordinates of the line and see what happens. And notice what happens to allow the line to go diagonally!

The next stage is to draw a rectangle. This is done using the DrawRectangle Method.
To do this we need to specify a rectangle to draw.

Rectangle rect = new Rectangle(5, 20, 15, 10);

This creates a new Rectangle rect and specifies in order:

  • X Coordinate of the Top Left corner of the rectangle
  • Y Coordinate of the Top Left corner of the rectangle
  • The width of the rectangle
  • The Height of the rectangle

Now we just use the DrawRectangle Method.

e.Graphics.DrawRectangle(p, rect);

Now hit F5 and sit back and admire the very small rectangle you have created.
Exit the form, and try changing the coordinates and dimensions of the rectangle.
Next time I shall demonstrate drawing more complex shapes and filling shapes with colour.

The Full Code
(VB .Net)

    Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        Dim p As New Pen(New SolidBrush(Color.Blue))
        e.Graphics.DrawLine(p, New Point(5, 5), New Point(20, 5))

        Dim rect As New Rectangle(5, 20, 15, 10)
        e.Graphics.DrawRectangle(p, rect)
    End Sub

(C#)

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Pen p = new Pen(new SolidBrush(Color.Blue));
            e.Graphics.DrawLine(p, new Point(5, 5), new Point(20, 5));

            Rectangle rect = new Rectangle(5, 20, 15, 10);
            e.Graphics.DrawRectangle(p, rect);
        }

(C++/CLI)

    private: System::Void pictureBox1_Paint(System::Object^  sender, System::Windows::Forms::PaintEventArgs^  e) {
                 Pen ^p = gcnew Pen(gcnew SolidBrush(Color::Blue));
                 e->Graphics->DrawLine(p,System::Drawing::PointF(5,5),System::Drawing::Point(20,5));

                 Rectangle rect = Rectangle(5,20,15,10);
                 e->Graphics->DrawRectangle(p,rect);
             }

Colorized by http://puzzleware.net/CodeHtmler/default.aspx

VB .Net

I started coding with HTML, however my first real desktop program language was VB .Net which I tried after I read an article in Computer Shopper. This simple little VB program simply tested the input of one textbox to see if it was a palindrome! It did this by copying letter by letter from textbox1 to another textbox textbox2 and seeing if the content matched! This brought me into the wonderful world of desktop programming.

C# .Net

This is the language I would classify that I truely started with. This is the language I used to create a simple RTF editor. (I’m still working on it, albeit slightly more complex than when I started! :) )

Back to VB .Net

Since I think some of you might be interested in the palindrome testing program I mention above, here are the issue numbers and page numbers of the Computer Magazine I created the program from:

  • Issue -220
  • Page – 144

OR

The code of the main function to reverse the strings.
This assumes you have a form with 2 Text Boxes. Named TextBox1 and TextBox2

  1 For Each letter As Char In TextBox1.Text
  2     TextBox2.Text = letter & TextBox2.Text
  3 Next
  4 If TextBox1.Text = TextBox2.Text Then
  5     MsgBox("Palindrome Detected")
  6 End If

Colorized by http://puzzleware.net/CodeHtmler/default.aspx

As I promised, I shall start this Blog properly, by talking about the different .Net languages and why each one is good for its own thing. Although some people may use J# I don’t think that altogether it is one of the more common languages, because of this I shall not include it in my talk.

The first language I shall talk about is VB .NET, Microsoft has advertised this as a simple, easy to use language for those with no experience with programming or for those advancing from VBS or VB6. I chose not to start at this language, simply because Microsoft advertised it as a beginner language. I have however since learnt the basics of it. I wonder how many others skipped VB .Net and went straight into the deep end with C/C++ or did what I did, which was decided to start with the in between language C#.

C# was created by Microsoft to be an improvement on C++ with a stricter compiler, which enables more accurate error messages to be given by the compiler, and to fix some of the bugs that C++ had, for example the fact that C++ tried to be the language for everything from Kenels to Business apps, meant that overall it became very generalized. C# however will only work as part of the .Net framework and was advertised by Microsoft to be the language for writing Games (using the XNA framework) and for writing applications simply because it was specialzied in that rather than trying to do everything.

C++ is the language that most parts of the computer where writen in, from the Kernel that drives the OS, be it Windows or Linux to the range of applications, Office Suite, Photo Editing, etc. that are used every day, to the language to be used for coding the latest high-end games. C++ can be used for everything. Natively C++ can not be used with the .Net framework however by compiling your project with CLR (Common Language Runtime) you are able to reference .Net DLLs and use the classes it provides in your applications.

In reality most people will learn one programming language, for example Microsoft wants you to learn VB .Net then most people will advance to learning C++ purely because it is used almost everywhere. C# is gaining in popularity however C++ is still the leading language because it can be used for everything. For those with no experince I recommend using VB .Net if you have no idea of programming, or for those who are feeling brave, they can start with C# .Net, which allows simple drag-drop controls. C++ is still the best language for long-term usage, and the CLR option keeps C++ going strong by adding the ease of use of .Net classes to the power of C++.

Hello Readers,

This is my first blog, so i don’t know what i should say.

I have decided to focus this blog on the .Net Framework.

I think that I shall start by talking about which lanugage is best for what!

That will come soon.