Monthly Archives: June, 2013

windows forms validation for textbox to enter only numbers and only characters

//Allows only characters

private void txtName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar) || Char.IsPunctuation(e.KeyChar))
            {
                MessageBox.Show(“Please Enter only Characters “);
                e.Handled = true;
            }

        }
//Allows only numbers
        private void txtPhone_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= 48 && e.KeyChar <= 57 || e.KeyChar == 46 || e.KeyChar == 8)
            {

                e.Handled = false;
            }

            else
            {
                MessageBox.Show(“Please Enter only Numbers”);
                e.Handled = true;
            }

        }

Windows forms Creating folder with given name in given directory

string folderName = Application.StartupPath + “\\Myfolder\\”;
            if (!Directory.Exists(folderName))
            {
                Directory.CreateDirectory(folderName);
            }