C# Free Code for All
Posted: Tue May 05, 2009 1:20 am
I'm thinking about giving you guy a free code that was coded by me.
It main purpose is to parse the message in rich text box in raw speed. It all utilized in C# so I'm hoping some of you can program C#. It is still in alpha version so, I'm just doing it on my free time.
The Source Code:
I have added the DLL in a file share that can be used in a COM Link assembly for programming.
Download Link: http://cid-754dbfe4bfff17c5.skydrive.live.com/self.aspx/.Public/RTF%20Word%20Parser.dll
How do I use it?
It can be utilized by adding reference of the DLL. Add the control to the form and make it have a text. In coding view, just do RTFControl.Dictionary.Add("Hello"); and RTFControl.Text = "Hello Everyone"; then provoke the method by using RTFControl.ParseText();
This code is published under public domain. That means you can use it commercially or whichever you want. I published this code under Visual Studio Standard 2008 Commercial License.
ENJOY!
It main purpose is to parse the message in rich text box in raw speed. It all utilized in C# so I'm hoping some of you can program C#. It is still in alpha version so, I'm just doing it on my free time.
The Source Code:
Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace RTF_Word_Parser
{
public partial class RTFControl : RichTextBox
{
public Color TextFoundColor = Color.Blue;
public char[] ToDismiss = new char[]
{
'<', '>', '\r', ';', ':', '(', ')', '=', '+',
'@', '#', '$', '%', '^', '&', '*', '_', '1', '2', '3', '4', '5', '6'
, '7', '8', '9', '0', '\\', '/', '|'
};
public char[] ToSplitBy = new char[] { ' ', '\n', '\t', '.', '?', '!', ',' };
public List<string> Dictionary = new List<string>();
public bool CorrectFormat = false;
public RTFControl()
{
InitializeComponent();
}
public void ParseText()
{
#region Spelling Checking
// If the activity is Enabled then end event to prevent interferences with the running event
// process
if (!Enabled)
// Get out of here
return;
// Disable the to prevent inferences
Enabled = false;
// To hold the text so that it can reset the RTF what it currently have
var contain = Text;
// Clearing the rtf and text
Rtf = string.Empty;
// Reset the
Text = contain;
// Remove all the characters the spell do not look for
foreach (var Dismisschar in ToDismiss)
{
// Take out the unneeded characters
contain = contain.Trim(Dismisschar);
}
// Make a list of misspelled words
var misspelledwords = new List<string>();
// Now having a words splitted with the "ToSplitBy"
var words = contain.Split(ToSplitBy);
// Pick every word in an array of words
foreach (var word in words)
{
Application.DoEvents(); // To prevent non-responsive program
if (word == string.Empty)
continue;
var count = Dictionary.IndexOf(word); // A easier method to get the dictionary match
// If the count is not more than -1 then it is not in the dictionary
if (count == -1)
// Add it to the misspelled word
misspelledwords.Add(word);
// Increase the progression to keep the user happy
}
// Get the count of misspell words and if it have more than 0 then it not spell correctly
CorrectFormat = misspelledwords.Count <= 0;
//Get RTF from richtextbox
var strRTF = Rtf;
// It is designed by format in RTF so it find the index of Colorbl
var TableStart = strRTF.IndexOf("colortbl;");
// If it exists then it add the color table of it own
if (TableStart != -1)
{
// Find the ending index of the table
var TableEnd = strRTF.IndexOf('}', TableStart);
// Remove the whole color table
strRTF = strRTF.Remove(TableStart, TableEnd - TableStart);
// Now add its own to the rtf
strRTF = strRTF.Insert(TableStart,
@"{\colortbl ;\red" + TextFoundColor.R + @"\green" + TextFoundColor.G + @"\blue" + TextFoundColor.B + @";}");
}
else
{
// Find the location of the rtf introduction
var RTFLoc = strRTF.IndexOf(@"\rtf");
// Finding the insert location by finding the curly bracket of RTFLoc
var InsertLoc = strRTF.IndexOf('{', RTFLoc);
// If not existed then it find the other curly bracket and index one character before it
if (InsertLoc == -1) InsertLoc = strRTF.IndexOf('}', RTFLoc) - 1;
// Now it insert the RTF color table to the RTF
strRTF = strRTF.Insert(InsertLoc,
@"{\colortbl ;\red" + TextFoundColor.R + @"\green" + TextFoundColor.G + @"\blue" + TextFoundColor.B + @";}");
}
// Word after word for each misspelled words
foreach (var word in misspelledwords)
{
// Index scalar variable, just to find the position of character within string
// Repeat boolean to keep repeating until the Index is equal to or greater than Max
// Reset Variables
var Repeated = false;
var Index = 0;
// Loop until the Index is greater than Max
while (true)
{
// Finding the end index of a string
var Max = strRTF.LastIndexOf(word);
// Getting the index point of the string and added by 1 to keep it above max
Index = strRTF.IndexOf(word, Index, StringComparison.CurrentCultureIgnoreCase);
// If it is -1 from the method then break away from the loop
if (Index == -1)
break;
// Insert the red color at the index point
strRTF = strRTF.Insert(Index, @"\cf1 ");
// Get the end index of a string
Max = strRTF.LastIndexOf(word);
// Get the last index of the string again to insert the black color
Index = Index + word.Length + @"\cf1 ".Length;
// Break away from the loop if -1
if (Index == -1)
break;
// Insert the black color at given splitted position of the document to the end of the word
strRTF = strRTF.Insert(Index, @"\cf0 ");
// Having Index is equal or above max is the furthest the string can go, whichs stop the loop
if (Index >= Max)
{
break;
}
}
}
// Set the RTF of the Rich text box to the fixed RTF we have now
Rtf = strRTF;
// Select the end of the text
Select(TextLength, 0);
// Turn on the Rich text box
Enabled = true;
Refresh();
#endregion
}
public void InvertParseText()
{
#region Spelling Checking
// If the activity is Enabled then end event to prevent interferences with the running event
// process
if (!Enabled)
// Get out of here
return;
// Disable the to prevent inferences
Enabled = false;
// To hold the text so that it can reset the RTF what it currently have
var contain = Text;
// Clearing the rtf and text
Rtf = string.Empty;
// Reset the
Text = contain;
// Remove all the characters the spell do not look for
foreach (var Dismisschar in ToDismiss)
{
// Take out the unneeded characters
contain = contain.Trim(Dismisschar);
}
// Make a list of misspelled words
var misspelledwords = new List<string>();
// Now having a words splitted with the "ToSplitBy"
var words = contain.Split(ToSplitBy);
// Pick every word in an array of words
foreach (var word in words)
{
Application.DoEvents(); // To prevent non-responsive program
if (word == string.Empty)
continue;
var count = Dictionary.IndexOf(word); // A easier method to get the dictionary match
// If the count is not more than -1 then it is not in the dictionary
if (count != -1)
// Add it to the misspelled word
misspelledwords.Add(word);
// Increase the progression to keep the user happy
}
// Get the count of misspell words and if it have more than 0 then it not spell correctly
CorrectFormat = misspelledwords.Count <= 0;
//Get RTF from richtextbox
var strRTF = Rtf;
// It is designed by format in RTF so it find the index of Colorbl
var TableStart = strRTF.IndexOf("colortbl;");
// If it exists then it add the color table of it own
if (TableStart != -1)
{
// Find the ending index of the table
var TableEnd = strRTF.IndexOf('}', TableStart);
// Remove the whole color table
strRTF = strRTF.Remove(TableStart, TableEnd - TableStart);
// Now add its own to the rtf
strRTF = strRTF.Insert(TableStart,
@"{\colortbl ;\red" + TextFoundColor.R + @"\green" + TextFoundColor.G + @"\blue" + TextFoundColor.B + @";}");
}
else
{
// Find the location of the rtf introduction
var RTFLoc = strRTF.IndexOf(@"\rtf");
// Finding the insert location by finding the curly bracket of RTFLoc
var InsertLoc = strRTF.IndexOf('{', RTFLoc);
// If not existed then it find the other curly bracket and index one character before it
if (InsertLoc == -1) InsertLoc = strRTF.IndexOf('}', RTFLoc) - 1;
// Now it insert the RTF color table to the RTF
strRTF = strRTF.Insert(InsertLoc,
@"{\colortbl ;\red" + TextFoundColor.R + @"\green" + TextFoundColor.G + @"\blue" + TextFoundColor.B + @";}");
}
// Word after word for each misspelled words
foreach (var word in misspelledwords)
{
// Index scalar variable, just to find the position of character within string
// Repeat boolean to keep repeating until the Index is equal to or greater than Max
// Reset Variables
var Repeated = false;
var Index = 0;
// Loop until the Index is greater than Max
while (true)
{
// Finding the end index of a string
var Max = strRTF.LastIndexOf(word);
// Getting the index point of the string and added by 1 to keep it above max
Index = strRTF.IndexOf(word, Index, StringComparison.CurrentCultureIgnoreCase);
// If it is -1 from the method then break away from the loop
if (Index == -1)
break;
// Insert the red color at the index point
strRTF = strRTF.Insert(Index, @"\cf1 ");
// Get the end index of a string
Max = strRTF.LastIndexOf(word);
// Get the last index of the string again to insert the black color
Index = Index + word.Length + @"\cf1 ".Length;
// Break away from the loop if -1
if (Index == -1)
break;
// Insert the black color at given splitted position of the document to the end of the word
strRTF = strRTF.Insert(Index, @"\cf0 ");
// Having Index is equal or above max is the furthest the string can go, whichs stop the loop
if (Index >= Max)
{
break;
}
}
}
// Set the RTF of the Rich text box to the fixed RTF we have now
Rtf = strRTF;
// Select the end of the text
Select(TextLength, 0);
// Turn on the Rich text box
Enabled = true;
Refresh();
#endregion
}
}
}
Download Link: http://cid-754dbfe4bfff17c5.skydrive.live.com/self.aspx/.Public/RTF%20Word%20Parser.dll
How do I use it?
It can be utilized by adding reference of the DLL. Add the control to the form and make it have a text. In coding view, just do RTFControl.Dictionary.Add("Hello"); and RTFControl.Text = "Hello Everyone"; then provoke the method by using RTFControl.ParseText();
This code is published under public domain. That means you can use it commercially or whichever you want. I published this code under Visual Studio Standard 2008 Commercial License.
ENJOY!