Thursday, May 3, 2007

Providing Custom Validations in VS.NET IDE

Hi all,

Just recently, working on Reporting system, I was required to create PDF reports out of HTML pages. I did those successfully using iTextSharp, a free online library for rendering PDFs from HTML.

In the process, I had to customize the particular HTMLs to contain several constraints and hense I had to validate them at the design level.

I found a great designer that did the trick. The well known Visual Studio.NET Designer. I decided to use that for my purpose as it did nost ofthe validations all by itself and I just had to add a few more of my custom ones.

Say for example, in my report, I was always required to have three DIVs in my tag in HTML, namely "HEADER", "FOOTER" and "DETAIL"

Now by default there is not such binding for user in case he is making it a regular HTML. But if it a Report then I had to put these validation checks.

For this I had to tweak the Designer to throw messages to the user if required.

The best way out seemed to be MACROS at that is what did the trick.

While creating any macro or a module in a macro project, we always have one Module in it "EnvironmentEvents". This module contains all events pertaining to all the events that occur in the Visual Studio IDE.

In the EnvironmentEvents module, choose the event

This event will be called every time a document is saved in Visual Studio IDE.

Here in you could have all your validations and all those will be thrown back to IDE using your own system of exception throwing, which could possibly be via MessageBox or some message in Output Window etc.

Now in order to get the items into Output window or task list, we can use the following piece of code.

Dim projItem As EnvDTE.Solution
Dim tw As TaskList = CType(win.Object, TaskList)
win = projItem.DTE.Windows.Item(Constants.vsWindowKindTaskList)

tw = CType(win.Object, TaskList)tw.TaskItems.Add("ErrorCategory", Document.Name, "Error Message", vsTaskPriority.vsTaskPriorityHigh, , True, Document.Name, -1, True, True)

-- Ashutosh

No comments:

Post a Comment