Thursday, May 24, 2012

Adding a Table to a RichTextBox Control in C#

As I have searched, there are not many tutorials, examples, or projects specifically to C# for how to going about adding a user defined table to a RichTextBox control. One of last stands for a programmer would be to program a custom control for them self that would perform what they could not already find. What everybody's goal in development is going the best affordable rout and as 'this' example is so simple, it would be kind of pointless to shell out +$250 on a library that does the same thing.

Note: for developers programming in VB.Net, try this link; it basically does just what the code in C# does - in fact, I used that link to construct this code by example. WPF, Java, ASP.Net, and Silverlight devs, there are plenty of tutorials and examples already... kind of annoying when your trying to search for only pure Windows Forms - C#!

To the code! I have tested this method bellow and it works... there was a side effect though that I couldn't fix so I went the long rout, but still looks the same. Greater success to those who can fix that.

public void InsertTable(RichTextBox Rtb, int Rows, 
        int Cols, int BrdrW, int TableWidth)
{
    String A; int i = 1, j = 1, a = 1, b = 1;
    A = @"{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang1033" +
        @"{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}" +      
        @{\f1\fnil\fcharset0 Microsoft Sans Serif;}}" +
        @"\viewkind4\uc1\trowd";
    for (; j <= Rows; j++) // for every row
    {
        for (; i <= Cols; i++) // for every column
        {
            A += @"\clbrdrt\brdrw" + BrdrW + "\brdrs" +
                 @"\clbrdrl\brdrw" + BrdrW + "\brdrs" +
                 @"\clbrdrb\brdrw" + BrdrW + "\brdrs" +
                 @"\clbrdrr\brdrw" + BrdrW + "\brdrs" +
                 @"\cellx" + ((TableWidth / Cols) * 10 * i);
        }
        A += @"\pard\intbl\ltrpar\lang1023\f0\fs24";
        for (; a <= Rows; a++)
        {
            A += @"\intbl\clmrg";
            for (; b <= Cols; b++) A += @"\cell";
            A += @"\row";
        }
    }
    A += "}";

    Rtb.SelectedRtf = A; // send table to RichTextBox 
    Rtb.Rtf = Rtb.Rtf.Replace("\\\'08rdrs", ""); 
    // (\'08rdrs seems to be the side effect here)

    // center table in RichTextBox (and everything else XP )
    Rtb.SelectAll();
    Rtb.SelectionAlignment = HorizontalAlignment.Center;
}

If you would also like to combine this method with this project, you'll be one step closer to creating your own Microsoft Word Program.
I also came across something interesting relating to RichTextBox's, maybe it may be of use to any of you viewing. Check it out!

Friday, May 11, 2012

Programming Imaginary-Complex Numbers [C#]

Now I know there is a library with a class created supposedly for this, and though I haven't fully explored it to know what it is capable of, I wanted to create my own custom Imaginary Number i class with the basic desired features I was after.

If you would rather(or in the mean time) check out that class I was talking about, follow these instructions:
  1. New Project >> Add Reference >> System.Numerics
  2. Add using System.Numerics; to the top of your class file.
  3. The class is called Complex: Complex i = new Complex();
  4. ...new Complex(2, 3); would look something like this in mathematical notation: 2 + 3i
[Important Note]
I recently read this article: to understand how I would go about finding the root of an imaginary number. Now, unless you wish to spend much time reading and figuring out what they are trying to describe and explain(like I did), the simplified answer goes as follows:
√i = cos(90[1/2]) + sin(90[1/2])i
OR
n
√i = cos(90n) + sin(90n)i
If this doesn't explain or satisfy what you are looking for, then I recommend reading on in that article.

The code is pretty explanatory and I have added comments to things that may not so easily be seen...
Alright, enough talk, here is the [C#] code:
namespace Math
{
    public sealed class i
    {
        // set value to default
        private double val = 1; // i^0
        public string Value
        {
            get
            {
                String final;
                if (double.IsPositiveInfinity(val)) 

                     final = "i";
                else if (double.IsNegativeInfinity(val))
                     final = "-i";
                else final = val.ToString();
                return final;
            }
            // only set value through operators
            // private set
            {
                String test = value;
                if (!double.TryParse(test, out val))
                    val = String.Equals(test, "-i") ?
                          double.NegativeInfinity :
                          double.PositiveInfinity;
            }
        }

        // main targeted desire for this class
        public static object Pow(double x)
        {
            object ans = null;
            // root powers
            if (System.Math.Ceiling(x) > x)
               ans = System.Math.Cos(90 * x) + " + " +
                     System.Math.Sin(90 * x) + "i";
            else
               // returns real numbers if power is a multiple
               // of 2
               if (x % 2 == 0) ans = System.Math.Pow(-1, x/2);
               // returns a string representation if 

               // power is odd
               else ans = (System.Math.Pow(-1, (x-1)/2) == 1
                          ? "i" : "-i");
            return ans;
        }

        // Allows for there to be 'i * i * i'
        // instead of just i.Pow(#);
        // This is also the only way to set the 

        // 'Value' property.
        public static i operator *(i complex, i complex2)
        {
            // this section may not be entirely accurate
            i c = new i();
            short tag = 0;

            // i^1
            if (double.IsPositiveInfinity(complex.val)) 

                     tag = 1;
            else if (double.IsNegativeInfinity(complex.val))
                     tag = 3; // i^3
            else if (complex.val == -1) tag = 2; // i^2

            if (double.IsPositiveInfinity(complex2.val))
                     tag += 1; // i^1
            else if (double.IsNegativeInfinity(complex2.val))
                     tag += 3;// i^3
            else if (complex2.val == -1) tag += 2; // i^2

            c.Value = (string)Pow((double)tag);
            return c;
        }
    }
}

Interesting fact: as I developed this I realized I could have just rewritten the equation: 
[(i2n)-1]/-2 as (-1)n

Edit: I also recently gained experience with extensions in C# allowing no need to create an additional separate class - just an extensions class.