Disable "X" Close Button On Your Windows Form Application

12. December 2010 07:55

C# Code Snippets 

Recently, someone on MSDN forums asked this question on how to disable the "X" a.k.a. close button in his windows form application so the user is forced to close the application from the cancel button on the form and not from the close button. He wanted this for his application which requires registration first before a user can start using his application.

I don't know the answer so I bing arond a bit and find a solution posted by someone though I don't have the name but it worked like a charm....!!

We need some interoperability to do this, therefore get the namespace System.Runtime.InteropServices
Code:

private 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);

Below code goes to the load event of your form:

IntPtr hMenu = GetSystemMenu(this.Handle, false);
int menuItemCount = GetMenuItemCount(hMenu);
RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);

Time to press 'F5'....and the close button is now disabled on the form.

Download: CloseButton.zip (43.08 kb)

 No Rating

A Time Wrapper Class in C#

30. November 2010 13:27

C# Code Snippets 

I was just surfing the net in search for some fundoo tutorial on WPF and by chance (I don't meant to visit that site), I get this class which I feel is pretty helpful. A simple wrapper class to conver time. the class allows you to convert:

  1. To Days
  2. To Hours
  3. To Minutes
  4. To Seconds
  5. To MilliSeconds

This can be pretty usefull. The class is as follows:

 

using System;

public static class TimeSpanUtil
{
    #region To days
    public static double ConvertMillisecondsToDays(double milliseconds)
    {
        return TimeSpan.FromMilliseconds(milliseconds).TotalDays;
    }

    public static double ConvertSecondsToDays(double seconds)
    {
        return TimeSpan.FromSeconds(seconds).TotalDays;
    }

    public static double ConvertMinutesToDays(double minutes)
    {
        return TimeSpan.FromMinutes(minutes).TotalDays;
    }

    public static double ConvertHoursToDays(double hours)
    {
        return TimeSpan.FromHours(hours).TotalDays;
    }
    #endregion

    #region To hours
    public static double ConvertMillisecondsToHours(double milliseconds)
    {
        return TimeSpan.FromMilliseconds(milliseconds).TotalHours;
    }

    public static double ConvertSecondsToHours(double seconds)
    {
        return TimeSpan.FromSeconds(seconds).TotalHours;
    }

    public static double ConvertMinutesToHours(double minutes)
    {
        return TimeSpan.FromMinutes(minutes).TotalHours;
    }

    public static double ConvertDaysToHours(double days)
    {
        return TimeSpan.FromHours(days).TotalHours;
    }
    #endregion

    #region To minutes
    public static double ConvertMillisecondsToMinutes(double milliseconds)
    {
        return TimeSpan.FromMilliseconds(milliseconds).TotalMinutes;
    }

    public static double ConvertSecondsToMinutes(double seconds)
    {
        return TimeSpan.FromSeconds(seconds).TotalMinutes;
    }

    public static double ConvertHoursToMinutes(double hours)
    {
        return TimeSpan.FromHours(hours).TotalMinutes;
    }

    public static double ConvertDaysToMinutes(double days)
    {
        return TimeSpan.FromDays(days).TotalMinutes;
    }
    #endregion

    #region To seconds
    public static double ConvertMillisecondsToSeconds(double milliseconds)
    {
        return TimeSpan.FromMilliseconds(milliseconds).TotalSeconds;
    }

    public static double ConvertMinutesToSeconds(double minutes)
    {
        return TimeSpan.FromMinutes(minutes).TotalSeconds;
    }

    public static double ConvertHoursToSeconds(double hours)
    {
        return TimeSpan.FromHours(hours).TotalSeconds;
    }

    public static double ConvertDaysToSeconds(double days)
    {
        return TimeSpan.FromDays(days).TotalSeconds;
    }
    #endregion

    #region To milliseconds
    public static double ConvertSecondsToMilliseconds(double seconds)
    {
        return TimeSpan.FromSeconds(seconds).TotalMilliseconds;
    }

    public static double ConvertMinutesToMilliseconds(double minutes)
    {
        return TimeSpan.FromMinutes(minutes).TotalMilliseconds;
    }

    public static double ConvertHoursToMilliseconds(double hours)
    {
        return TimeSpan.FromHours(hours).TotalMilliseconds;
    }

    public static double ConvertDaysToMilliseconds(double days)
    {
        return TimeSpan.FromDays(days).TotalMilliseconds;
    }
    #endregion
}
Currently rated 5.0 by 1 person

How To Find Whether The Machine Is of 32-Bit/64-Bit Architecture

13. May 2010 22:22

.NET Framework C# Code Snippets 

The easiest way to find out the processor architecture is to use the Environment class GetEnvironmentVariable method

Console.WriteLine(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"));

This will print the processor architecture on the console window i.e. x86 and x64 for 32-bit and 64-bit respectively.

Currently rated 5.0 by 1 person

Removing Duplicates from a List in C#

24. December 2009 18:25

.NET Framework C# Code Snippets 

For more details and detailed explaination of the code visit this link.

static List removeDuplicates(List inputList)
        {

            Dictionary uniqueStore = new Dictionary();
            List finalList = new List();
	    foreach (string currValue in inputList)
            {

                if (!uniqueStore.ContainsKey(currValue))
                {

                    uniqueStore.Add(currValue, 0);
                    finalList.Add(currValue);

                }

            }
            return finalList;
         }
 No Rating

Retrieve Key from Value in Hash Table

16. December 2009 02:10

C# Code Snippets 

Working with hash tables is pretty simple but few days back I was having a problem in retrieving a key from a value in hash table. I was bit lazy to find a way myself, so I searched the net and here is what I got....a simple piece of code which lead to me to complete my task and so I thought I should share it with everyone here.

public string FindKey(string Value, Hashtable HT)
{
       string Key = "";
       IDictionaryEnumerator e = HT.GetEnumerator();
       while (e.MoveNext())
       {
            if (e.Value.ToString().Equals(Value))
            {
               Key = e.Key.ToString();
            }
       }
       return Key;
}
Currently rated 4.2 by 11 people