Sep 27, 2010

C# Clipboard Event Textbox

A Textbox in C# has a number of useful events to indicate when certain actions have been taken. For example, .NET textboxes have an event to indicate when the text has changed or when the user has pressed a key. These events allow C# developers to write clean code that interacts with textboxes.

Following the same principles, we can manually implement events that are triggered by clipboard actions, i.e. text is cut, copied, or pasted in the textbox. The .NET Framework does not come with these events, but they are not difficult to implement.

Custom Textbox

To implement custom events, we are going to have to create our own textbox user control. The user control will inherit the Textbox class since we want all the default behaviors of a .NET textbox.
Creating a custom user control will also let us override the WndProc function, which processes messages passed to the control. By overriding the function, we can detect messages such as when text is cut, copied, or pasted, before allowing the control to process them.

Textbox Cut Event
Detecting each of the clipboard messages will be the same. Specifically for the cut message, we want to compare the message ID to the WM_CUT constant:
private const int WM_CUT = 0x0300;
protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_CUT)
    {
        //Cut Event
    }
}
We will discuss how to trigger the event below. For now, the important thing to consider is how to figure out what text is being cut. For this to a useful event, we want to give the developer the text which is being cut.
Remember that we are processing the message before it is applied. Which means that the text to be cut is not in the clipboard yet. Thus to access the text that will be cut we need to look directly at the control. Luckily textboxes have a SelectedText property which gives us the value we are looking for.

Textbox Copy Event

The copy event will work almost the same, the only difference will be the WM_COPY constant:
private const int WM_COPY = 0x0301;
protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_COPY)
    {
        //Copy Event
    }
}
The same concept applies here as well, the text select has not been copied yet, so it is not in the clipboard yet. The SelectedText property will give the text that will be copied.

Textbox Paste Event

Similarly, the paste event will be triggered when the WM_PASTE constant is matched:
private const int WM_PASTE = 0x0302;
protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_PASTE)
    {
        //Paste Event
    }
}
In this case however, since the text is being pasted, it already exists on the clipboard. It does not yet exist on the textbox. To access the text we need to use the Clipboard.GetText() function.

Raising the Events

The "tricky" part to consider when raising our textbox cut, copy, and paste events, is how to pass the text values we figured out above (from SelectedText or Clipboard.GetText()).
Luckily for us, the .NET Framework has a built-in EventArgs class which is designed to do just that. By default, EventArgs gives basic information about the raised event. But by creating a class that inherits EventArgs, we can add custom properties.
So for example, in our case, we want to set the clipboard text (whether it is going to exist in the clipboard or it already does):
public class ClipboardEventArgs : EventArgs
{
    public string ClipboardText
    {
        get;
        set;
    }

    public ClipboardEventArgs(string clipboardText)
    {
        this.ClipboardText = clipboardText;
    }
}
With this we can use delegates to define and raise our events. Here is a quick example of how to raise the textbox paste event:
public delegate void ClipboardEventHandler(object sender, ClipboardEventArgs e);
public event ClipboardEventHandler PastedText;

//Raising the event
if (PastedText != null)
    PastedText(this, new ClipboardEventArgs(Clipboard.GetText()));
I will not get into the details but it is a straightforward event definition and call. If you do not know what is going on, I suggest you look into .NET events.

Essential XlsIO

 Essential XlsIO is a .NET library that can read and write Microsoft Excel files. It features a full-fledged object model similar to the Microsoft Office Automation libraries. It can be used on systems that do not have Microsoft Excel installed, making it an excellent report engine for tabular data.



http://www.syncfusion.com/products/reporting-edition/xlsio

Data validation for .Net

Data validation acts as the first step of defense against the bugs. To maintain a solution it is important to have validations to prevent the invalid data creeping into the database.
This class consists of the commonly used validations which you will find useful in your project. Each line of code is commented in detail for quick understanding of the method.
Implementation is pretty simple. You will just have to pass your input data and check for the boolean.

Usage of the methods in class are pretty straight forward
1 ) IsInteger (string value);
if (DataValidator.IsInteger("123")
    {
       //Valid Data
    }
    else
    {
        //Invalid Data
    }
2 ) IsAlpha(string value) ;
if (DataValidator.IsAlpha("data2"))
    {
       //Valid Data
    }
    else
    {
        //Invalid Data
    }
3 ) IsDecimal(string value);
if (DataValidator.IsDecimal("2.234"))
    {
       //Valid Data
    }
    else
    {
        //Invalid Data
    }
4 ) IsIPAddress(string value) ;
if (DataValidator.IsIPAddress("127.0.0.1"))
    {
       //Valid Data
    }
    else
    {
        //Invalid Data
    }
5 ) IsURL(string value);
if (DataValidator.IsURL("http://www.codecanyon.net"))
    {
       //Valid Data
    }
    else
    {
        //Invalid Data
    }
6 ) IsEMailAddress(String value);
if (DataValidator.IsEMailAddress("me(at)example.com"))
    {
       //Valid Data
    }
    else
    {
        //Invalid Data
    }
7 ) IsHexColor(string value);
if (DataValidator.IsHexColor("#FFFFFF"))
    {
       //Valid Data
    }
    else
    {
        //Invalid Data
    }
8 ) IsDate(string value);
if (DataValidator.IsDate("12-Jan-2009"))
    {
       //Valid Data
    }
    else
    {
        //Invalid Data
    }