h1.post-title { color:orange; font-family:verdana,Arial; font-weight:bold; padding-bottom:5px; text-shadow:#64665b 0px 1px 1px; font-size:32px; } -->

Pages

Use of a multicast delegate

A multicast delegate may be used to call more than one method.

Remove unused using in VS 2008

Open Class File
Edit>>Intellisense >> Organise Usings >> Remove unused usings

Data Table Function for LINQToSQL

public static DataTable AsDataTable<T>(IEnumerable<T> varlist)
{
DataTable dtReturn = new DataTable();
// column names
PropertyInfo[] oProps = null;
if (varlist == null) return dtReturn;
foreach (T rec in varlist)
{
// Use reflection to get property names, to create table, Only first time, others will follow if (oProps == null)
{
oProps = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
DataRow dr = dtReturn.NewRow();
foreach (PropertyInfo pi in oProps)
{
dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
(rec, null);
}
dtReturn.Rows.Add(dr);
}
return dtReturn;
}

Sort using in VS 2008


Open Class File
Edit>>Intellisense >> Organise Usings >> Sort Usings

Business Validation via Keypress Event in C#.Net

Today we will discuss about Display Form Icon in the Menu Strip of MDI Form
public static void BOValidation(KeyPressEventValidation mode, KeyPressEventArgs e, object sender)
{
switch (mode)
{
case KeyPressEventValidation.Alphabets:
if (!char.IsLetter(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 32)
e.Handled = true;
break;
case KeyPressEventValidation.AlphabetValidation:
if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar) && e.KeyChar != 32)
e.Handled = true;
break;
case KeyPressEventValidation.AlphaNumeric:
if (!char.IsLetter(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 32 && e.KeyChar != 45)
e.Handled = true;
break;
case KeyPressEventValidation.CompanyName:
if (!char.IsLetter(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 32 && e.KeyChar != 38 && e.KeyChar != 45 && e.KeyChar != 40 && e.KeyChar != 46 && e.KeyChar != 40)
e.Handled = true;
break;
case KeyPressEventValidation.DecimalValidation:
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') e.Handled = true;
// To allow only one decimal point if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1 )
e.Handled = true;
break; case KeyPressEventValidation.FilePathvalidation:
if ((e.KeyChar >= 48 && e.KeyChar <= 57) && (e.KeyChar >= 65 && e.KeyChar <= 122) && e.KeyChar == 92)
e.Handled = true;
break;
case KeyPressEventValidation.IntegerValidation:
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
e.Handled = true;
break;
case KeyPressEventValidation.PhoneValidation:
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != 32 && e.KeyChar != 40 && e.KeyChar != 41 && e.KeyChar != 43 && e.KeyChar != 45)
e.Handled = true;
break;
case KeyPressEventValidation.ValidateSpecialCharacters:
if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar))
e.Handled = true;
break;
case KeyPressEventValidation.PercentageValidation:
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
if (!char.IsControl(e.KeyChar))
{
TextBox textBox = (TextBox)sender;
if (textBox.Text.IndexOf('.') > -1 && textBox.Text.Substring(textBox.Text.IndexOf('.')).Length >= 3)
{
e.Handled = true;
}
}
break;
}
}

Clear Rich Text Box Data in C#.net

public static void ClearRichTextBox(Control parent)
{
foreach (Control child in parent.Controls)
{
RichTextBox rtxtBox = child as RichTextBox;
if (rtxtBox == null)
ClearRichTextBox(child);
else
rtxtBox.Text = string.Empty;
}
}

Business Validation Via Regular Expression in C#.Net

public static bool BOValidation(Validate mode, TextBox input)
{
switch (mode)
{
case Validate.EmailValidation:
_pattern = @"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.TimeValidation:
_pattern = @"^(20|21|22|23|[01]\d|\d)(([:][0-5]\d){1,2})$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.AlphabetValidate:
_pattern = @"^[a-zA-Z]*$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.AlphabetSpaceDotValidate:
_pattern = @"^[a-zA-Z\s\.]*$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.AlphaNumericCommaSpaceHyphenUnderscoreValidate:
_pattern = @"^[a-zA-Z0-9\s,-_&]*$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.DecimalValidate:
_pattern = @"^[-+]?\d+(\.\d+)?$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.NumberValidate:
_pattern = @"^[0-9]*$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.PhoneNumberValidate:
_pattern = @"^[-+\s]?[0-9\s,()-]*$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.PostalCodeValidate:
_pattern = @"^[-+]?[0-9]*$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.UserNameValidate:
_pattern = @"^[a-zA-Z0-9_]*$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.FTPFilePathValidation:
_pattern = @"^([a-zA-Z0-9])*/[a-zA-Z0-9]+)$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.URLValidate:
_pattern = @"^((https?|ftp|news):\/\/)?www\.([a-z]([a-z0-9\-]*)+\.
(aero|arpa|asia|biz|cat|com|coop|edu|gov|info|int|jobs|lan|mil|mobi|museum|nato|name|net|org|pro|store|tel|travel|web|[a-z]{2}|[a-z]{2}\.[a-z]{2}))$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.FilePathValidate:
_pattern = @"([a-zA-Z]:(\\w+)*\\[a-zA-Z0_9]+)?.doc|.docx |.pdf";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
case Validate.PercentageValidation:
_pattern = @"^(100(?:\.0{1,2})?|0*?\.\d{1,2}|\d{1,2}(?:\.\d{1,2})?)$";
_match = Regex.Match(input.Text.Trim(), _pattern, RegexOptions.IgnoreCase);
break;
}
return _match.Success;
}

Display Form Icon in the Menu Strip of MDI Form

Today we will discuss about Display Form Icon in the Menu Strip of MDI Form

childform.MdiParent = this; childform.WindowState = FormWindowState.Maximized; childform.BringToFront();
var bmp = new Bitmap(16, 16);
using (var g = Graphics.FromImage(bmp))
{
g.DrawImage(childform.Icon.ToBitmap(), new Rectangle(0, 0, 16, 16));
}
var newIcon = Icon.FromHandle(bmp.GetHicon());
childform.Icon = newIcon; childform.Show();

ASP.NET Page Directives

ASP.NET Page Directives
Page directives : Page directives configure the runtime environment that will execute the page.
The complete list of directives is as follows:
@ Assembly - Links an assembly to the current page or user control declaratively.
@ Control - Defines control-specific attributes used by the ASP.NET page parser and compiler and can be included only in .ascx files (user controls).
@ Implements - Indicates that a page or user control implements a specified .NET Framework interface declaratively.
@ Import - Imports a namespace into a page or user control explicitly.
@ Master - Identifies a page as a master page and defines attributes used by the ASP.NET page parser and compiler and can be included only in .master files.
@ MasterType - Defines the class or virtual path used to type the Master property of a page.
@ OutputCache - Controls the output caching policies of a page or user control declaratively.
@ Page - Defines page-specific attributes used by the ASP.NET page parser and compiler and can be included only in .aspx files.
@ PreviousPageType - Creates a strongly typed reference to the source page from the target of a cross-page posting.
@ Reference - Links a page, user control, or COM control to the current page or user control declaratively.
@ Register - Associates aliases with namespaces and classes, which allow user controls and custom server controls to be rendered when included in a requested page or user control.

FxCop and StyleCop

It runs on compiled DLLs.
As it runs on compiled IL code, it can be used for C#, VB.NET, in short any language which compiles to IL code.
StyleCop
Keep in mind that it is not a Microsoft product. It is not even a Team System Power Tool. It is a tool developed by a very passionate developer at Microsoft (on evenings and weekends). There's no support, servicing, evolution or anything else beyond what he can get done in his spare time. Style checking is an interesting feature and may show up in an official product at some point down the road.
It runs on actual source code.
Currently it runs only on C#.
The ultimate goal of StyleCop is to allow to produce elegant, consistent code that team members and others who view developer code will find highly readable.
Code Analysis
No spell checking - FxCop uses a dictionary (plus custom dictionary) to check the names of methods, classes, etc. Code Analysis doesn't seem to do that.
Help - FxCop not only complained when something was wrong but provided a great deal of help/hints to resolve the problem. Code Analysis just seems to present the problem with no sign of a hint.
Note that Code Analysis is only available in the Premium and Ultimate editions of Visual Studio 2010 and also Visual Studio 2005 & 2008 Team System.

FxCop analyzes programming elements in managed assemblies, called targets, and provides an informational report that contains messages about the targets, Messages include suggestions about how to improve the source code used to generate them. FxCop represents the checks it performs during an analysis as rules. A rule is managed code that can analyze targets and return a message about its findings. Rule messages identify any relevant programming and design issues and, when possible, supply information about how to fix the target.
Using FxCop, you can perform the following tasks:
Control which rules are applied to targets.
Exclude a rule message from future reports.
Apply style sheets to FxCop reports.
Filter and save messages.
Save and reuse application settings in FxCop projects.
Working in the FxCop Application Window
The FxCop application window displays the targets and rules included in a project, and the messages that are generated when an analysis is performed. The window is divided into three major areas: the configuration pane on the left, the messages pane on the right, and the properties pane at the bottom.