Thursday, February 28, 2013

How to modify the default new class template for C# in Visual Studio 2008 or 2010?

Here is what it a new class looks like:

using System;

 namespace MyNameSpace
 {
    class MyClass
    {
    }
 }

Here is what I want it to look like:

using System;

namespace MyNameSpace
{
    public class MyClass
    {

        #region << Constant Variables >>

        #endregion

        #region << Private Variables >>

        #endregion

        #region << Public Variables >>

        #endregion

        #region << Properties >>

        #endregion

        #region << Constructors  >>

        /// <summary>
        /// Default constructor.
        /// </summary>
        public $safeitemrootname$()
        {

        }

        #endregion

        #region << Private Methods >>

        #endregion

        #region << Public Methods >>

        #endregion

    }
}

So making this change is easy to do. All you have to do is edit a text file that is compressed.
Copy the zip file, file located here to the desktop:

Visual Studio 2008

For 64 bit:

C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

For 32 bit:

C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

Visual Studio 2010

For 64 bit:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

For 32 bit:

C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
Extract the zip file.
Using a text editor, open the Class.cs file.
The file will have the following text:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;

namespace $rootnamespace$
{
    class $safeitemrootname$
    {
    }
}

Change it to have this text:
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;

namespace $rootnamespace$
{
    public class $safeitemrootname$
    {
        #region << Constant Variables >>

        #endregion

        #region << Private Variables >>

        #endregion

        #region << Public Variables >>

        #endregion

        #region << Properties >>

        #endregion

        #region << Constructors  >>

        /// <summary>
        /// Default constructor.
        /// </summary>
        public $safeitemrootname$()
        {

        }

        #endregion

        #region << Private Methods >>

        #endregion

        #region << Public Methods >>

        #endregion
    }
}

Save the file.
Rebuild the zip file with the new Class.cs.  Be careful to build the zip file correctly.
Copy the new zip file back here and overwrite the existing one:

For 64 bit:

C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

For 32 bit:

C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

Visual Studio 2010

For 64 bit:

C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

For 32 bit:
C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
Now, you have to rebuild the template classes. To do this:
Open a command prompt as Administrator.
Change to the appropriate directory:

cd C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\

32-bit

cd C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\

Visual Studio 2010

cd C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\

32-bit

cd C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\

Run this command:

devenv.exe /installvstemplates

Now any new class you create will have your new format.

Tuesday, February 19, 2013

Detecting browser close event in javascript

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Detecting browser close event in javascript</title>
</head>
<body>
    <form id="form1" runat="server">
  
    </form>
</body>
</html>

<script language="javascript" type="text/javascript">
function closeEditorWarning() {
            return 'Are you sure?'
        }
window.onbeforeunload = closeEditorWarning;
</script>

*******************************************************************************

using System;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session.Timeout = 3;//in minute
    }
}