|
using System;
using System.Windows.Forms;
namespace Moonlight
{
internal class RichTextboxHelper
{
#region Methods
public static string GetLastWord(RichTextBox richTextbox)
{
int pos = richTextbox.SelectionStart;
while (pos > 1)
{
string substr = richTextbox.Text.Substring(pos - 1, 1);
if (Char.IsWhiteSpace(substr, 0))
{
return richTextbox.Text.Substring(pos, richTextbox.SelectionStart - pos);
}
pos--;
}
return richTextbox.Text.Substring(0, richTextbox.SelectionStart);
}
public static string GetLastLine(RichTextBox richTextbox)
{
int charIndex = richTextbox.SelectionStart;
int currentLineNumber = richTextbox.GetLineFromCharIndex(charIndex);
// the carriage return hasn't happened yet...
// so the 'previous' line is the current one.
string previousLineText;
if (richTextbox.Lines.Length <= currentLineNumber)
previousLineText = richTextbox.Lines[richTextbox.Lines.Length - 1];
else
previousLineText = richTextbox.Lines[currentLineNumber];
return previousLineText;
}
public static string GetCurrentLine(RichTextBox richTextbox)
{
int charIndex = richTextbox.SelectionStart;
int currentLineNumber = richTextbox.GetLineFromCharIndex(charIndex);
if (currentLineNumber < richTextbox.Lines.Length)
{
return richTextbox.Lines[currentLineNumber];
}
else
{
return string.Empty;
}
}
public static int GetCurrentLineStartIndex(RichTextBox richTextbox)
{
return richTextbox.GetFirstCharIndexOfCurrentLine();
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I'm working as a software developer since 2005 mostly using C# and MS SQL Server for building windows and web applications. I have 3 years experience in developing CRM softwares, but I'm also interested in Game development using C++, DirectX, and C#-XNA.
I'm always open for new technologies, preferring code reusability, stability, and "nice solutions".