コード例 #1
0
        //sumbit a new user
        private void Submit_Click(object sender, RoutedEventArgs e)
        {
            Brush fill = (SolidColorBrush) new BrushConverter().ConvertFromString("#FF622F96");

            nameTextBox.BorderBrush         = fill;
            nationalCodeTextBox.BorderBrush = fill;
            typeDetectorBox.BorderBrush     = fill;

            ReadWriteJson file = new ReadWriteJson();

            try
            {
                string name         = nameTextBox.Text;
                int    nationalCode = Int32.Parse(nationalCodeTextBox.Text);
                string imageAddress = imageAdd;
                string role         = typeDetectorBox.Text;
                string description  = descriptionTextBox.Text;
                bool   isAdmin;

                if (adminChecker.IsChecked == true)
                {
                    isAdmin = true;
                }
                else
                {
                    isAdmin = false;
                }

                if (typeDetectorBox.SelectedIndex == -1)
                {
                    throw new NullReferenceException();
                }
                else if (file.CheckUser(name) != false)
                {
                    resultTextBox.Content   = "User already existed!Choose another Name";
                    nameTextBox.BorderBrush = Brushes.Red;
                    throw new Exception("Duplicate User");
                }
                string imageExtension = Path.GetExtension(imageAdd);
                assistant.CopyFile(imageAdd, assistant.AppPath + @"Images\" + name + imageExtension);
                imageAddress = assistant.AppPath + @"Images\" + name + imageExtension;
                User newUser = new User(name, nationalCode, imageAddress, role, nationalCode.ToString(), isAdmin, description);

                file.WriteUser(newUser);

                resultTextBox.Content = "Registered!";

                nameTextBox.Text         = "";
                nationalCodeTextBox.Text = "";
                string      PicName = assistant.AppPath + @"NoImage.PNG";
                BitmapImage bitmap  = new BitmapImage();
                bitmap.BeginInit();
                bitmap.UriSource = new Uri(PicName);
                bitmap.EndInit();
                mainPic.Source = bitmap;
                typeDetectorBox.SelectedIndex = -1;
                adminChecker.IsChecked        = false;
                descriptionTextBox.Text       = "";
            }
            catch (FormatException)
            {
                resultTextBox.Content           = "Wrong format,please check national code!";
                nationalCodeTextBox.BorderBrush = Brushes.Red;
                //throw new  userControl/ArgumentFormatException();
            }
            catch (NullReferenceException)
            {
                resultTextBox.Content           = "Fill the blank boxes!";
                nameTextBox.BorderBrush         = Brushes.Red;
                nationalCodeTextBox.BorderBrush = Brushes.Red;
                typeDetectorBox.BorderBrush     = Brushes.Red;
                //throw new  userControl/ArgumentNullException();
            }
            catch (Exception excp)
            {
                MessageBox.Show(excp.Message);
                ErrorLogger.LogError(excp);
                //pop error in center
                //UserControls.ErrorBox Error = new UserControls.ErrorBox();
            }
        }