Recent site activity

Tutorials‎ > ‎

C# Tutorials


Trap Enter Key

posted Mar 28, 2012 9:00 AM by Tim Key

        private void txtTextbox_KeyPress(object sender, EventArgs e)
        {
            //Trap the enter key
            string info = e.KeyChar.ToString();
            if (info == "\r")
            {
                //Your code here

                //This signals the event that you have managed the outcome
                e.Handled = true;
            }
        }

Common Mask for DevExpress TextEdit Control

posted Nov 30, 2011 9:38 AM by Tim Key

Simple Mask Type:
    Phone - (999) 000-0000

RegEx Mask Type:
    Email - [a-zA-Z0-9.-]+@[a-zA-Z0-9-]+\.[A-Za-z]{2,}

How to use CSLA NameList

posted Sep 27, 2011 6:37 AM by Tim Key   [ updated Sep 27, 2011 6:40 AM ]

using System;
using Csla;
using System.Data.SqlClient;
using System.Data;
using Csla.Data;
 
namespace CSLASample.NameList
{
    [Serializable]
    public class FindItemCollection : NameValueListBase<stringstring>
    {
        #region Factory Methods
 
        private static FindItemCollection _list;
 
        public static FindItemCollection GetFindItemCollection()
        {
            if (_list == null)
                _list = DataPortal.Fetch<FindItemCollection>();
            return _list;
        }
 
        public static void InvalidateCache()
        {
            _list = null;
        }
 
        private FindItemCollection()
        { /* require use of factory methods */ }
 
        #endregion
 
        #region Data Access
 
        private void DataPortal_Fetch()
        {
            RaiseListChangedEvents = false;
            IsReadOnly = false;
            //Load Values
            using (SqlConnection cn = new SqlConnection(Database.Connection))
            {
                cn.Open();
                using (SqlCommand cm = cn.CreateCommand())
                {
                    cm.CommandType = CommandType.StoredProcedure;
                    cm.CommandText = "getFindItemList";
                    using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
                    {
                        while (dr.Read())
                        {
                            Add(new NameValueListBase<stringstring>.NameValuePair(
                                dr.GetString("IMA_ItemID"), dr.GetString("IMA_ItemName")));
                        }
                    }
                }
            }
            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }
 
        #endregion
    }
}

Restrict textbox to numeric only

posted Nov 23, 2010 7:32 AM by Tim Key   [ updated Nov 26, 2010 6:21 AM ]

Instructions:  Place the code below into a KeyPress event for the textbox you wish to restrict


Code Snippet:

//The first "if" statement is for numeric only.  The second section "else if" allows one decimal place.

//You may leave off the "else if" if you do not wish to allow a decimal.

if (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar))
{
    //Allow
}
else if (e.KeyChar == Convert.ToChar(".") && textbox1.Text.IndexOf(".") == -1)
{
    //Allow
}
else
{
    e.Handled = true;
}

1-4 of 4