In my last project I got an opportunity to make my application compatible with Windows Vista and Windows 7. The main issue I faced was UAC; User Account Control (UAC) which introduced with the launch of Windows Vista; this provides users a better and safer computing experience. If UAC enabled, Windows will prompt every time when Applications try to access File System, like writing / creating Files in Program Files Folder, Windows Folder etc. (It’s not a best practice to write / create files in System directories like Windows, Program Files etc.). It’s also not a best practice to ask the customer to disable the UAC and run the application. The alternate is to make the application compatible with UAC, so that when the customer runs the application the system will prompts the UAC dialog, and run the application with Administrator privileges.
The procedure to make the application compatible with UAC is simple.
- Right-click project and add a new item.
- From the Add New Item box select Application Manifest File.
- In the manifest file un-comment the following line:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Build your application. And when the build is complete you will see the security shield icon accompanying your application icon.

Security shield icon accompanying application icon
A dialogue box appears in front of the user to run the application with full administrative privileges.

UAC Dialog box when double clicking on the Application
We can disable the close button of a Windows Form in three ways
- Set controlbox property to false – It is most easy way to do this. But it also hides the Minimize and Maximize buttons.
- Using WIN32 API – Using Win32 API, we are getting the system menu and removes the close menu item using Remove menu item.
Here is the Code.
const int MF_BYPOSITION = 0x400;
[DllImport("User32")]
private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);
[DllImport("User32")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("User32")]
private static extern int GetMenuItemCount(IntPtr hWnd);
//In the Form load event
IntPtr hMenu = GetSystemMenu(this.Handle, false);
int menuItemCount = GetMenuItemCount(hMenu);
RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);
And here is the screenshot

Using WIN32 API
- By Overriding the CreateParams property – The CreateParams property gets the required creation parameters when the control handle is created. By overriding the property we are removing the close button. You can get more information about CreateParams in MSDN
Here is the code.
private const int CS_CLOSE = 0x200;
protected override CreateParams CreateParams
{
get
{
CreateParams parms = base.CreateParams;
parms.ClassStyle |= CS_CLOSE; //
return parms;
}
}
And here is the screenshot

By Overriding CreateParams property
All of above three methods will work fine. But if you are using Windows 7 none of the above will work or OS will override all these.

Windows 7 - Close Window
Most of the forums, it’s a common question that how can we create PDF files from C#. Few days back I got a chance to work with iTextSharp library which helps to create PDF files from .Net. You can download the iTextSharp from sourceforge.net. It also supports HTML Parsing too.
Here is the code which converts and HTML File into PDF using iTextSharp libraries.
using System.Collections.Generic;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
class Program
{
static void Main(string[] args)
{
//Creating the instance of the document object.
Document doc = new Document();
//Creating the PDF File
StreamWriter streamWriter = new StreamWriter(@"C:\Sample.pdf");
PdfWriter.GetInstance(doc, streamWriter.BaseStream);
doc.Open();
IEnumerable<IElement> elements;
//Reading the HTML Contents
using (StreamReader streamReader = new StreamReader(@"C:\SamplePage.htm"))
{
//Parsing HTML Contents.
elements = HTMLWorker.ParseToList(streamReader, new StyleSheet());
}
foreach (IElement item in elements)
{
doc.Add(item);
}
//Custom Text which will be appended using Paragraph class.
string paragraph = "This is custom text which will be appended";
doc.Add(new Paragraph(paragraph));
//Closing the document.
doc.Close();
}
}
Thanks to Rahul for providing initial inputs on iTextSharp library.
Long back one of my cousins Rajesh gives me an introduction to Operator overloading in C++, but at that time I was VB 6.0 guy and I didn’t give much importance to it. But yesterday I got a chance to work on some sample applications where I tried operator overloading on C#.
I overloaded the + operator for adding two persons objects, and here is the code.
//Person class.
public class Person
{
//Name of the Person
public string Name
{
get;
set;
}
//Operator overloading.
public static Person operator +(Person first, Person second)
{
//Returning person, by adding the Names.
return new Person()
{
Name = first.Name + " " + second.Name
};
}
}
And here is the void Main()
static void Main(string[] args)
{
Person firstPerson = new Person()
{
Name = "Person 1"
};
Person secondPerson = new Person()
{
Name = "Person 2"
};
//Adding Person objects
Person resultPerson = firstPerson + secondPerson;
//It will return Person 1 Person 2
Console.WriteLine(resultPerson.Name);
}
You can find Operator Overloading Tutorial in MSDN
In my first WinForms application, I have to create various wizard screens, I couldn’t find any ready made Controls for it from Microsoft. Then one of my colleague suggested a way to tweak it, like Add a Tab Control, Set the Alignment property to bottom and add a panel on top of the Tab Control, which contains Next / Previous / Cancel buttons. It was working fine. Few days back again I got the same situation for one of my personal project, and I thought about writing a control for the same. After doing some research I found a simple code snippet, which helps to hide the Tab Pages title in Runtime.
Here is the code, in this I am extending the Tab control and overriding the WndProc event.
protected override void WndProc(ref Message m)
{
// Hide tabs by trapping the TCM_ADJUSTRECT message
if (m.Msg == 0x1328 && !DesignMode)
{
m.Result = (IntPtr)1;
}
else
{
base.WndProc(ref m);
}
}
You can find more details about TCM_ADJUSTRECT Message on MSDN.
HTTP modules differ from HTTP handlers. An HTTP handler returns a response to a request that is identified by a file name extension or family of file name extensions. In contrast, an HTTP module is invoked for all requests and responses. It subscribes to event notifications in the request pipeline and lets you run code in registered event handlers. The tasks that a module is used for are general to an application and to all requests for resources in the application.
You can get more information on MSDN – HTTP Handlers and HTTP Modules Overview