الأربعاء، 1 يونيو 2011

T : 1-6-2011

        private void button1_Click(object sender, EventArgs e)
        {
            // -------------------------------
            // To return control name for error provider
            Control ctrl = new Control();
            // -------------------------------

            string emailexpr = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
            string urlExpr = @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
            string usphoneNum = @"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}";
            string numLength = @"\d{8}";
            //string expr = @"^[a-zA-Z\s'.]{1,50}$";
            string expr = @"^[a-zA-Z\s'.]{1,20}$";
            try
            {
                string invoice = "";
                if (string.IsNullOrEmpty(Invoic_txt.Text.Trim()))
                {
                    ctrl = Invoic_txt;
                    throw new Exception("Can not be embty");
                }
                else {
                     ClearErr();
                    invoice = Invoic_txt.Text; }
                string name = "";

                if (Regex.IsMatch(name_txt.Text.Trim(), expr))
                {
                    ClearErr();
                    name = name_txt.Text;
                }
                else
                {
                   
                    ctrl = name_txt;
                    throw new Exception("Char Only.");

                }
                string mail = "";
                if (Regex.IsMatch(mail_txt.Text, emailexpr))
                {
                    ClearErr();
                    mail = mail_txt.Text;

                }
                else
                {
                   
                    ctrl = mail_txt;
                    throw new Exception("Enter Valid Email");

                }
                string price = "";
                if (Information.IsNumeric(price_txt.Text))
                {
                    price = price_txt.Text;
                    ClearErr();
                }
                else
                {
                   
                    ctrl = price_txt;

                    throw new Exception("Enter Valid Price");
                }

             

                string date = dateTimePicker1.Value.ToShortDateString();
                string center = comboBox1.SelectedItem.ToString();
                string course = "";
                foreach (object o in checkedListBox1.CheckedItems)
                {
                    course += o.ToString();
                }
                string result = string.Format("Invoice Number:{0}\nName:{1}\nEmail:{2}\nDate:{3}\nCourses:{4}\nPrice:{5}", invoice, name, mail, date, course, price);
                res_lbl.Text = result;

            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                errorProvider1.SetError(ctrl, ex.Message);
            }

           
        }



        private void ClearErr()
        {
            errorProvider1.Clear();
        }



        private void Invoic_txt_KeyPress(object sender, KeyPressEventArgs e)
        {

            string InvoiceTxt = Invoic_txt.Text;

            if (e.KeyChar == 46)
            {
                if (InvoiceTxt.Contains('.') == true)
                {
                    e.Handled = true;
                }
            }
            else
            {
                if (e.KeyChar == 8)
                    e.Handled = false;
                else
                if (e.KeyChar < 48 || e.KeyChar > 57    )
                    e.Handled=true;
                
                    
            }
               

                }