Exemplo n.º 1
0
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            // Save recipe into database
            try
            {
                // DataContext exists, user had clicked existing recipe
                if (dpList.DataContext != null)
                {
                    Recipe r = (Recipe)dpList.DataContext;
                    r.Name         = tbRecipeName.Text;
                    r.Time         = tbRecipeTime.Text;
                    r.Instructions = tbInstructions.Text;
                    r.Writer       = tbRecipeWriter.Text;

                    // Save to database
                    BLRecipes.SaveRecipe(r, lbRecipeType.SelectedItems);

                    // Update datagrid
                    //dgRecipes.ItemsSource = null;

                    Recipe temp  = recipes.Single(s => s.Id == r.Id);
                    int    index = recipes.IndexOf(temp);
                    recipes[index]        = r;
                    dgRecipes.ItemsSource = recipes;

                    // Set feedback string
                    tbFeedBack.Text = "Resepti " + r.Name + " päivitetty.";
                }
                // If DataContext is set to null, that means that recipe is brand new
                else
                {
                    Recipe r = new Recipe(tbRecipeName.Text, tbRecipeTime.Text, tbInstructions.Text, tbRecipeWriter.Text);

                    // Save to database, Get id
                    int index = BLRecipes.SaveRecipe(r, lbRecipeType.SelectedItems);
                    // Set id
                    r.Id = index;
                    // Update datagrid
                    //dgRecipes.ItemsSource = null;
                    recipes.Add(r);
                    dgRecipes.ItemsSource = recipes;

                    // Set feedback string
                    tbFeedBack.Text = "Resepti " + r.Name + " lisätty.";
                }

                StateMachine(States.NoSelection);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Exemplo n.º 2
0
        private void Init()
        {
            try
            {
                // Load database configurations
                BLRecipes.GetConnStr();

                // Allocate memory
                recipes     = new List <Recipe>();
                recipeTypes = new List <string>();

                // Set state to NoSelection
                StateMachine(States.NoSelection);

                // Get all recipetypes and populate ListBox control
                lbRecipeType.ItemsSource = BLRecipes.GetAllTypes();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Exemplo n.º 3
0
        private void btnGetRecipes_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                System.Collections.IList types = lbRecipeType.SelectedItems;

                // Set state to NoSelection
                StateMachine(States.NoSelection);

                // Call business logic, find recipes
                recipes = BLRecipes.GetAllRecipes(tbGetRecipe.Text, types);

                // Set datagrid itessource
                dgRecipes.ItemsSource = recipes;

                // Set feedback string
                tbFeedBack.Text = "Löytyi " + recipes.Count + " reseptiä.";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Exemplo n.º 4
0
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            PrintDialog pd = new PrintDialog();

            try
            {
                if (pd.ShowDialog() == true)
                {
                    if (dpList.DataContext != null)
                    {
                        // Get recipe from dockpanel
                        Recipe r = (Recipe)dpList.DataContext;
                        r.Name         = tbRecipeName.Text;
                        r.Time         = tbRecipeTime.Text;
                        r.Instructions = tbInstructions.Text;
                        r.Writer       = tbRecipeWriter.Text;

                        // Generate flow document
                        FlowDocument doc = BLRecipes.PrintRecipe(r);
                        doc.Name = "FlowDoc";

                        // Create IDocumentPaginatorSource from FlowDocument
                        IDocumentPaginatorSource idpSource = doc;

                        // Call PrintDocument method to send document to printer
                        pd.PrintDocument(idpSource.DocumentPaginator, "Resepti");

                        // Set feedback string
                        tbFeedBack.Text = "Resepti " + r.Name + " tulostettu.";
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Exemplo n.º 5
0
        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            // Remove recipe from database
            try
            {
                // Cast selected datacontext to Recipe object
                Recipe r = (Recipe)dpList.DataContext;
                // Send object to business tier
                string sMessageBoxText = "Haluatko varmasti poistaa reseptin " + r.Name + "?";
                string sCaption        = "Respentin poistaminen";

                MessageBoxButton btnMessageBox = MessageBoxButton.YesNoCancel;
                MessageBoxImage  icnMessageBox = MessageBoxImage.Warning;

                MessageBoxResult rsltMessageBox = MessageBox.Show(sMessageBoxText, sCaption, btnMessageBox, icnMessageBox);

                if (rsltMessageBox == MessageBoxResult.Yes)
                {
                    BLRecipes.RemoveRecipe(r);

                    // Remove recipe from datagrid
                    dgRecipes.ItemsSource = null;
                    recipes.Remove(r);
                    dgRecipes.ItemsSource = recipes;

                    // Set state to NoSelection
                    StateMachine(States.NoSelection);

                    // Set feedback string
                    tbFeedBack.Text = "Resepti " + r.Name + " poistettu.";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }