Numeric Only TextBox

2:31 AM
It's Very Easy to make a Numeric only TextBox ..
Just right the Following Code on The Textbox's KeyPress Event..

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar > 31 && (e.KeyChar < '0' || e.KeyChar > '9'))
            {
                e.Handled = true;
            }
        }


Above Code Wont Let you enter anything other than 0 to 9 Numerics

To Limit the Number of Character in Texbox use the MaxLength Property  of Texbox

Example:

TextBox1.MaxLength = 10;


Vb.net

 Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar > "31" And (e.KeyChar < "0" Or e.KeyChar > "9") Then
            e.Handled = True
        End If
    End Sub


Example:

TextBox1.MaxLength = 10


A Simple Log Writer And Log Searching Class

2:10 AM
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

class clsLog
{

    private StringBuilder strLog;
    private string strFileName = string.Empty;

    //Initialize Logging
    //###############################################################################################################################
    public void StartLogging(string strLogFileName)
    {
        strLog = new StringBuilder();
        strLog.Append("Logging Started At :     [" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt") + "]" + Environment.NewLine + Environment.NewLine);
        strFileName = strLogFileName;
    }
    //###############################################################################################################################


    //Write Log Entry
    //###############################################################################################################################
    public void WriteLogEntry(string strEntry)
    {
        strLog.Append("[" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt") + "]     " + strEntry + Environment.NewLine);
    }
    //###############################################################################################################################


    //New Line
    //###############################################################################################################################
    public void NewLine()
    {
        strLog.Append(Environment.NewLine);
    }
    //###############################################################################################################################


    //Stop Logging
    //###############################################################################################################################
    public void StopLogging()
    {
        strLog.Append(Environment.NewLine + "Logging Stopped At :     [" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt") + "]" + Environment.NewLine);
        if (System.IO.File.Exists(strFileName))
        {
            using (StreamWriter sw = File.AppendText(strFileName))
            {
                sw.Write(strLog.ToString());
                sw.Close();
            }
        }
        else
        {
            System.IO.File.WriteAllText(strFileName,strLog.ToString());
        }
    }
    //###############################################################################################################################

    //Search Log
    //###############################################################################################################################
    public string SearchLog(string strLogFileName, DateTime dtFrom, DateTime dtTo)
    {
        if (!File.Exists(strLogFileName)) { return null; }

        string strReturnString = string.Empty;
        using (StreamReader sr = new StreamReader(strLogFileName)) //strFileName
        {
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                DateTime strDateTime;
                if (line.Length > 22)
                {
                    DateTime.TryParse(line.Substring(0, 25).Replace('[', ' ').Replace(']', ' ').Trim(), out strDateTime);
                    if (strDateTime >= dtFrom && strDateTime <= dtTo)
                    {
                        strReturnString += line + Environment.NewLine;
                    }
                }
            }
        }

        return strReturnString;
    }
    //###############################################################################################################################


}


Getting Local IP Address

2:05 AM
public static  string GetLocalIP()
        {
            string _IP = null;

            System.Net.IPHostEntry _IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());

            foreach (System.Net.IPAddress _IPAddress in _IPHostEntry.AddressList)
            {
                if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
                {
                    _IP = _IPAddress.ToString();
                }
            } 
            return _IP;
        }

Getting Local Host Name

2:03 AM
 public string GetLocalHostName()
        {
            return System.Net.Dns.GetHostName();
        }

Class To Change Wallpaper in Xp,Vista,Windows 7 from C#.Net

1:48 AM
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Win32;
using System.Drawing;


        public sealed  class clsWallpaper
        {
            
            const int SPI_SETDESKWALLPAPER = 20;
            const int SPIF_UPDATEINIFILE = 0x01;
            const int SPIF_SENDWININICHANGE = 0x02;

            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

            public enum Style : int
            {
                Tiled,
                Centered,
                Stretched
            }

            public static void Set(Uri uri, Style style)
            {
                System.IO.Stream s = new System.Net.WebClient().OpenRead(uri.ToString());

                System.Drawing.Image img = System.Drawing.Image.FromStream(s);
                string tempPath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");
                img.Save(tempPath, System.Drawing.Imaging.ImageFormat.Bmp);

                RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
                if (style == Style.Stretched)
                {
                    key.SetValue(@"WallpaperStyle", 2.ToString());
                    key.SetValue(@"TileWallpaper", 0.ToString());
                }

                if (style == Style.Centered)
                {
                    key.SetValue(@"WallpaperStyle", 1.ToString());
                    key.SetValue(@"TileWallpaper", 0.ToString());
                }

                if (style == Style.Tiled)
                {
                    key.SetValue(@"WallpaperStyle", 1.ToString());
                    key.SetValue(@"TileWallpaper", 1.ToString());
                }

                SystemParametersInfo(SPI_SETDESKWALLPAPER,
                    0,
                    tempPath,
                    SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
            }
          
        }

A Class To Hide Show Task Bar (Works with XP)

1:46 AM
using System;
using System.Runtime.InteropServices;


 /// 
 /// This class will show or hide windows taskbar for full screen mode.
 /// 
 internal class HandleTaskBar
 {
  private const int SWP_HIDEWINDOW = 0x0080;
  private const int SWP_SHOWWINDOW = 0x0040;

        /// 
        /// Default Constructor.
        /// 
  public HandleTaskBar()
  {
  }

  [DllImport("User32.dll", EntryPoint="FindWindow")]
  private static extern int FindWindow(string lpClassName, string lpWindowName);

  [DllImport("User32.dll")]
  private static extern int SetWindowPos(int hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);

  /// 
  /// Show the TaskBar.
  /// 
  public static void showTaskBar()
  {
   int hWnd = FindWindow("Shell_TrayWnd", "");
   SetWindowPos(hWnd, 0, 0, 0, 0, 0, SWP_SHOWWINDOW);
  }

  /// 
  /// Hide the TaskBar.
  /// 
  public static void hideTaskBar()
  {
   int hWnd = FindWindow("Shell_TrayWnd", "");
   SetWindowPos(hWnd, 0, 0, 0, 0, 0, SWP_HIDEWINDOW);
  }
 }


Convert a File to Byte Arrary Function

1:44 AM
  public byte[] ReadByteArrayFromFile(string fileName)
       {
           byte[] buff = null;
           FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
           BinaryReader br = new BinaryReader(fs);
           long numBytes = new FileInfo(fileName).Length;
           buff = br.ReadBytes((int)numBytes);
           return buff;
       }