This blog is moved to

Friday, March 27, 2009

User resizable panel

using System;
using System.Drawing;
using System.Windows.Forms;

public class SizeablePanel : Panel {
private const int cGripSize = 20;
private bool mDragging;
private Point mDragPos;

public SizeablePanel() {
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.BackColor = Color.White;

protected override void OnPaint(PaintEventArgs e) {
ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor,
new Rectangle(this.ClientSize.Width - cGripSize, this.ClientSize.Height - cGripSize, cGripSize, cGripSize));

private bool IsOnGrip(Point pos) {
return pos.X >= this.ClientSize.Width - cGripSize &&
pos.Y >= this.ClientSize.Height - cGripSize;

protected override void OnMouseDown(MouseEventArgs e) {
mDragging = IsOnGrip(e.Location);
mDragPos = e.Location;

protected override void OnMouseUp(MouseEventArgs e) {
mDragging = false;

protected override void OnMouseMove(MouseEventArgs e) {
if (mDragging) {
this.Size = new Size(this.Width + e.X - mDragPos.X,
this.Height + e.Y - mDragPos.Y);
mDragPos = e.Location;
else if (IsOnGrip(e.Location)) this.Cursor = Cursors.SizeNWSE;
else this.Cursor = Cursors.Default;

Alt Key down - Underline not visible

control pannel-> easy of access center -> make the keyboard easier to use-> make it easier to use keyboard shutcuts->underline keyboard shortcuts and access keys".

Thursday, March 19, 2009

PDF to text

similar thread

it requires that you have the full version of Adobe installed on your PC so that you can gain access to the Adobe APIs (which doesn't technically qualify as a free way to do it). Here is the code I used to read the contents of a PDF. You will have to add a reference to the Adobe APIs in your project:

Dim objPDFPage As AcroPDPage
Dim objPDFDoc As New AcroPDDoc
Dim objPDFAVDoc As AcroAVDoc
Dim objAcroApp As AcroApp
Dim objPDFRectTemp As Object
Dim objPDFRect As New AcroRect
Dim lngTextRangeCount As Long
Dim objPDFTextSelection As AcroPDTextSelect
Dim temptextcount As Long
Dim strText As String
Dim lngPageCount As Long
Dim Fora As Long
lngPageCount = objPDFDoc.GetNumPages
For Fora = 0 To lngPageCount - 1
objPDFPage = objPDFDoc.AcquirePage(Fora)
objPDFRectTemp = objPDFPage.GetSize
objPDFRect.Left = 0
objPDFRect.right = objPDFRectTemp.x
objPDFRect.Top = objPDFRectTemp.y
objPDFRect.bottom = 0
' objPDFTextSelection = objPDFDoc.CreateTextSelect(lngPageCount, objPDFRect)
objPDFTextSelection = objPDFDoc.CreateTextSelect(Fora, objPDFRect)
' Get The Text Of The Range
temptextcount = objPDFTextSelection.GetNumText
For lngTextRangeCount = 1 To objPDFTextSelection.GetNumText
doctextdoctext = doctext & objPDFTextSelection.GetText(lngTextRangeCount - 1)
doctextdoctext = doctext & vbCrLf
doctype = "PDF"

Tuesday, March 17, 2009

How to start a command prompt in a folder in Windows Server 2003, Windows XP, and Windows 2000

Modify the Registry Manually

To add the Command Prompt command to the shortcut menu:
  1. Click Start, click Run, type regedit, and then click OK.
  2. Locate the following registry key:
  3. Right-click the shell key, point to New, and then click Key.
  4. Name the new key OpenNew.
  5. Click the OpenNew key, and then double-click the Default value in the right pane.
  6. Change the value to Command Prompt. Click OK.
  7. Right-click the OpenNew key, point to New, and then click Key.
  8. Name the new key Command.
  9. Double-click the Default item in the right pane.
  10. Change the value to cmd.exe /k cd %1.
If you have multiple drives on your computer, you can add a similar command to the shortcut menu that appears when you right-click a drive in Windows Explorer or My Computer. The steps to do this are the same as those for creating the Command Prompt command, except that you use the HKEY_CLASSES_ROOT\Drive\shell registry key as the starting point. Also, change the value of the Default value in the HKEY_CLASSES_ROOT\Drive\shell\OpenNew\Command key to cmd.exe /k.

Modify the Registry with a Script

To add the Command Prompt command to the shortcut menu:
  1. Copy the following text to a file named Cmdhere.reg:
    Windows Registry Editor Version 5.00
    @="Command Prompt"
    @="cmd.exe /k cd %1"
  2. Save the Cmdhere.reg file.
  3. Double-click the Cmdhere.reg file to automatically add the registry entries.

Play audio files using DirectX

Imports Microsoft.DirectX
Imports Microsoft.DirectX.AudioVideoPlayback
Public Class Form1
Private Sub Play_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Play.Click
Dim a As Audio
a = New Audio("C:\Users\User\Documents\Music_file_1.wma", True)
End Sub
End Class

Friday, March 13, 2009

File Association

1. Easy way is through the Visual Studio Setup & Deployment project.

How to manually set the file association?

Imports System
Imports Microsoft.Win32

1. Determine an arbitrary file type name.

Public Sub SetFileType(ByVal extension As String, ByVal FileType As String)
Dim rk As RegistryKey = Registry.ClassesRoot
Dim ext As RegistryKey = rk.CreateSubKey(extension)
ext.SetValue("", FileType)
End Sub

2. Write this file type name to the Registry, associating it with your extension.

Public Sub SetFileType(ByVal extension As String, ByVal FileType As String)
Dim rk As RegistryKey = Registry.ClassesRoot

Dim ext As RegistryKey = rk.CreateSubKey(extension)
ext.SetValue("", FileType)
End Sub

3. Add a description for your file type.

Public Sub SetFileDescription(ByVal FileType As String, ByVal Description As String)
Dim rk As RegistryKey = Registry.ClassesRoot
Dim ext As RegistryKey = rk.CreateSubKey(FileType)
ext.SetValue("", Description)
End Sub

4. Add actions.

Public Sub AddAction(ByVal FileType As String, ByVal Verb As String, ByVal ActionDescription As String)
Dim rk As RegistryKey = Registry.ClassesRoot
Dim ext As RegistryKey = rk.OpenSubKey(FileType,True).CreateSubKey("Shell").CreateSubKey(Verb)
ext.SetValue("", ActionDescription)
End Sub

5. Setting Action to do something.

Public Sub SetExtensionCommandLine(ByVal Command As String, ByVal FileType As String, ByVal CommandLine As String, Optional ByVal Name As String = "")
Dim rk As RegistryKey = Registry.ClassesRoot
Dim ext As RegistryKey = rk.OpenSubKey(FileType).OpenSubKey("Shell").OpenSubKey(Command, True).CreateSubKey("Command")
ext.SetValue(Name, CommandLine)
End Sub

Make sure you set “CommandLine” exactly as it should be as if you typed in the line from the MS-Dos or from the Command Prompt. Instead of using a File name, however, use “%1”.

6. To set a default action

Public Sub SetDefaultAction(ByVal FileType As String, ByVal Verb As String)
Dim rk As RegistryKey = Registry.ClassesRoot
Dim ext As RegistryKey = rk.OpenSubKey(FileType).OpenSubKey("Shell")
ext.SetValue("", Verb)
End Sub

7. Setting icon

Public Sub SetDefaultIcon(ByVal FileType As String, ByVal Icon As String)
Dim rk As RegistryKey = Registry.ClassesRoot
Dim ext As RegistryKey = rk.OpenSubKey(FileType)
ext.SetValue("DefaultIcon", Icon)
End Sub

Get URL from webbrowser control

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
TextBox1.Text = WebBrowser1.Document.Title
TextBox2.Text = WebBrowser1.Url.ToString
End Sub

Private Sub WebBrowser1_Navigated(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
TextBox1.Text = WebBrowser1.Url.AbsoluteUri
End Sub

Check whether two files are different or not

static public bool FilesDiffer(string filePath1, string filePath2)
if (new FileInfo(filePath1).Length != new FileInfo(filePath2).Length)
return true;

const int BUFFER_SIZE = 1024 * 1024;

byte[] buffer1 = new byte[BUFFER_SIZE];
byte[] buffer2 = new byte[BUFFER_SIZE];

using (FileStream file1 = File.OpenRead(filePath1))
using (FileStream file2 = File.OpenRead(filePath2))
while (true)
int n1 = file1.Read(buffer1, 0, BUFFER_SIZE);
int n2 = file2.Read(buffer2, 0, BUFFER_SIZE);

Debug.Assert(n1 == n2); // Files are supposed to be the same length!

if (n1 == 0) // End of file?
return false;

for (int i = 0; i < n1; ++i)
if (buffer1[i] != buffer2[i])
return true;

Catch All Exceptions

As you may know, an exception is always thrown in the context of a running thread; what happens if there is no try...catch block protecting that thread? The exception is propagated to the application domain level, where, eventually, it will cause your application to crash.

Since you cannot be sure that all threads are running (and you may not want to do so, anyway) inside a try...catch block, there are two things you can do to, 1) at least, be notified when an exception occurs and 2) decide what to do when that happens. In a Windows Forms application, the System.Windows.Forms.Application class is where this is done.

The argument to Application.SetUnhandledExceptionMode can be one of:

* UnhandledExceptionMode.Automatic: exception is caught in the Application.ThreadException handler, if there is no entry in the App.config or Web.config that disables it
UnhandledExceptionMode.CatchException: exception is caught in the ThreadException handler
UnhandledExceptionMode.ThrowException: exception is thrown, the ThreadException handler is ignored

See this example:


Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

void Application_ThreadException(Object sender, ThreadExceptionEventArgs e)


//do something with e.Exception


If you don't have a Windows Forms application, but instead a console application or a Windows Service, you can still catch all exceptions at the application domain level:

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

void CurrentDomain_UnhandledException(Object sender, UnhandledExceptionEventArgs e)

//Object exception = e.ExceptionObject;
//Boolean terminating = e.IsTerminating;

Finally, if you have an ASP.NET application, you add an event handler to the HttpApplication.Error event. This can be done in several ways:

1) Add an event handler to the Error event from anywhere (a page, a control, a module, etc):

HttpContext.Current.ApplicationInstance.Error += new EventHandler(ApplicationInstance_Error);

void ApplicationInstance_Error(object sender, EventArgs e)

//Exception ex = HttpContext.Current.Server.GetLastError();

2) Implement the Application_Error method on Global.asax.cs:

protected void Application_Error(Object sender, EventArgs args)


//Exception ex = HttpContext.Current.Server.GetLastError();


3) Implement a custom IHttpModule and hook to the Error event (my favorite):

public class ErrorModule: IHttpModule


public void Init(HttpApplication app)


app.Error += Application_Error;


void app_Error(object sender, EventArgs e)
//Exception ex = HttpContext.Current.Server.GetLastError();



To Shrink SqlServer log file.

USE DatabaseName

Tuesday, March 10, 2009

ASP.NET Page Events Lifecycle

When using master pages, the normal page event lifecycle is a little different. Here is the actual order:

  1. Page.OnPreInit
  2. MasterPageControl.OnInit (for each control on the master page)
  3. Control.OnInit (for each contol on the page)
  4. MasterPage.OnInit
  5. Page.OnInit
  6. Page.OnInitComplete
  7. Page.OnPreLoad
  8. Page.OnLoad
  9. MasterPage.OnLoad
  10. MasterPageControl.OnLoad (for each control on the master page)
  11. Control.OnLoad (for each contol on the page)
  12. Page.OnXXX (control event)
  13. MasterPage.OnBubbleEvent
  14. Page.OnBubbleEvent
  15. Page.OnLoadComplete
  16. Page.OnPreRender
  17. MasterPage.OnPreRender
  18. MasterPageControl.OnPreRender (for each control on the master page)
  19. Control.OnPreRender (for each contol on the page)
  20. Page.OnPreRenderComplete
  21. MasterPageControl.SaveControlState (for each control on the master page)
  22. Control.SaveControlState (for each contol on the page)
  23. Page.SaveViewState
  24. Page.SavePageStateToPersistenceMedium
  25. Page.OnSaveStateComplete
  26. MasterPageControl.OnUnload (for each control on the master page)
  27. Control.OnUnload (for each contol on the page)
  28. MasterPage.OnUnload
  29. Page.OnUnload

Thursday, March 5, 2009

Limit Memory Usage In SQL Server 2005

Sometimes during development I see that the instance of sqlservr.exe is using > 500MB of memory. When this happens, performance for the rest of the apps running on my machine take a I needed a way to control how much memory sql server consumed. Turned out to be very easy:

1) Open SQL Server Management Studio, right click the server instance in object explorer:

2) Click on 'Memory' and change the min/max memory usage to your liking.

Wednesday, March 4, 2009

Dos command - IP to Hostname

nbtstat -a ipaddress