C#

DateTimePicker – reset focus

Code Snippet – VB.NET Private Sub DateTimePicker1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.Enter     Dim fmt As DateTimePickerFormat = DateTimePicker1.Format       DateTimePicker1.Format = DateTimePickerFormat.Long     DateTimePicker1.Format = DateTimePickerFormat.Custom     DateTimePicker1.Format = fmt End Sub   Code Snippet – C# private void dateTimePicker1_Enter(object sender, EventArgs e) {     DateTimePickerFormat fmt = dateTimePicker1.Format;          dateTimePicker1.Format = DateTimePickerFormat.Long;     dateTimePicker1.Format [...]

Inherit DataGridView in Windows Forms

  Code Snippet using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace DataGridViewInheritance {     [Designer(typeof(System.Windows.Forms.Design.ControlDesigner))]     public partial class DataGridViewEx : DataGridView     {         public DataGridViewEx()         {             InitializeComponent();         }     } }

Decimal to Byte array (vice versa)

1: public static byte[] DecimalToBytes(Decimal dec) 2: { 3: using (MemoryStream stream = new MemoryStream()) 4: { 5: using (BinaryWriter writer = new BinaryWriter(stream)) 6: { 7: writer.Write(dec); 8: return stream.ToArray(); 9: } 10: } 11: } 1: public static Decimal BytesToDecimal(byte[] src) 2: { 3: if (src.Length == 1) 4: { 5: return Decimal.Parse(((char)src[0]).ToString()); [...]

Byte array to String (vice versa)

1: public static string BytesToString(byte[] bytes) 2: { 3: ASCIIEncoding enc = new System.Text.ASCIIEncoding(); 4: return enc.GetString(bytes); 5: } 1: public static byte[] StringToBytes(string str) 2: { 3: ASCIIEncoding enc = new System.Text.ASCIIEncoding(); 4: return enc.GetBytes(str); 5: }

Byte array to Integer (vice versa)

1: public static int BytesToInt(byte[] bytes) 2: { 3: return int.Parse(BytesToString(bytes).Trim()); 4: } 5:  6: public static string BytesToString(byte[] bytes) 7: { 8: ASCIIEncoding enc = new System.Text.ASCIIEncoding(); 9: return enc.GetString(bytes); 10: } 1: public static byte[] IntToBytes(int value) 2: { 3: ASCIIEncoding enc = new System.Text.ASCIIEncoding(); 4: return enc.GetBytes(value.ToString()); 5: } Code Snippet         /// [...]

Hexadecimal to Integer (vice versa)

1: public static int HexToInteger(string HexString) 2: { 3: return int.Parse(HexString, System.Globalization.NumberStyles.HexNumber); 4: } 1: public static string IntegerToHex(int IntValue) 2: { 3: return IntValue.ToString("X"); 4: }

Hexadecimal string to Bytes array

1: public static byte[] HexToBytes(String hexString) 2: { 3: byte [] byteArray = null; 4: int byteCount = 0; 5: if (hexString != null && hexString.Length > 0) 6: { 7: if (hexString.Length % 2 != 0) //make sure we have an even number of characters 8: hexString = "0" + hexString; 9: byteCount = [...]

Bytes array to Hexadecimal string

  1: public static string BytesToHex(byte[] bytes) 2: { 3: string hex = BitConverter.ToString(bytes); 4: return hex.Replace("-",""); 5: } 1: public static string BytesToHex(byte[] bytes) 2: { 3: StringBuilder hexString = new StringBuilder(bytes.Length); 4: for (int i = 0; i < bytes.Length; i++) 5: { 6: hexString.Append(bytes[i].ToString("X2")); 7: } 8: return hexString.ToString(); 9: }

Auto Complete ComboBox – C# Windows Forms

Download source files – 8.50 Kb Download demo project – 11.0 Kb Introduction This is a simple code snippet which is used to make an Auto Complete ComboBox. Using the code Usage: Call the function AutoComplete from within the ComboBox’s KeyPress event handler. 1: AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e, bool blnLimitToList)   1: // AutoComplete 2: [...]

DataGrid Paging using DataReader – C# Windows Forms

Download source files – 21.5 Kb Download demo project – 8.0 Kb Introduction This is an example of Windows Forms DataGrid paging using c#. I have used Microsoft SQL Server 2000 database. Using the code Create a table: 1: CREATE TABLE tblEmp (E_ID int PRIMARY KEY, E_Name varchar(60), E_Salary money, E_DOJ datetime) 2: GO   [...]