Monday 24 February 2014

Charles is assigned the task to develop a Windows Store app named MyToDo, which allows users to maintain a to-do list. It allows the user to add or delete the tasks to be performed. For this, the controls are available on the App bar of the app. The form for accepting the details from the user should be similar to the one shown in the following figure.

Charles is assigned the task to develop a Windows Store app named MyToDo, which allows users
to maintain a to-do list. It allows the user to add or delete the tasks to be performed. For this, the
controls are available on the App bar of the app. The form for accepting the details from the user
should be similar to the one shown in the following figure.
The Expected Interface of the App



When the user adds tasks, they should be displayed in the form as shown in the following figure.
The Expected Interface of the App after Adding Tasks
To delete an existing task, the user needs to select the task, and then right-click the blank area of
the app. The user can delete the selected task by clicking the Remove button that appears on the
App bar, as shown in the following figure.
The Remove Button on the App Bar
Charles has added the required controls to allow addition of tasks in the app. However, when a
user adds a task, he/she is able to see only the name of the task. Further, when a user clicks the
Remove button available on the App bar, the records are not deleted.
Help Charles to identify the reason and the solution to the problem.

Solution;

XAML code:

<Page
    x:Class="MyToDo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyToDo"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

   


    <Viewbox>
        <Grid Background="#FFA800FF" Width="1600" Height="900" Tapped="Grid_Tapped_1">
            <Rectangle Fill="Black" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="1878"/>
            <TextBlock Margin="639,28,561,0" HorizontalAlignment="Center" Text="My To Do" VerticalAlignment="Top" Height="72" FontSize="48" Width="400"/>

            <Grid x:Name="grd" Margin="0,7,0,-7">
                <TextBlock HorizontalAlignment="Left" Height="Auto" TextWrapping="Wrap" Text="Task Description" VerticalAlignment="Top" Width="Auto" Margin="17,132,0,0" FontSize="36"/>

                <TextBox x:Name="txtTaskDescription" HorizontalAlignment="Left" Margin="286,137,0,0" AcceptsReturn="True" Text="" VerticalAlignment="Top" Width="384" Height="109" GotFocus="txtTaskDescription_GotFocus"/>
                <TextBlock HorizontalAlignment="Left" Height="Auto" TextWrapping="Wrap" Text="Task Category" VerticalAlignment="Top" Width="Auto" Margin="17,267,0,0" FontSize="36"/>
                <ListView x:Name="lstViewTaskCategories" Foreground="White" FontSize="20"  HorizontalAlignment="Left" Height="100" Margin="289,276,0,0"  VerticalAlignment="Top" Width="384" BorderBrush="AliceBlue" BorderThickness="1">

                </ListView>
                <TextBlock x:Name="txtdesc" Visibility="Collapsed" HorizontalAlignment="Left" Height="Auto" TextWrapping="Wrap" Foreground="Red" Text="Please provide Task Description." VerticalAlignment="Top" Width="656" Margin="17,549,0,0" FontSize="36"/>
                <TextBlock HorizontalAlignment="Left" Height="81" TextWrapping="Wrap" Text="To be Completed by" VerticalAlignment="Top" Width="257" Margin="17,397,0,0" FontSize="36"/>
                <ComboBox x:Name="cboxYear" HorizontalAlignment="Left" Margin="289,427,0,0" VerticalAlignment="Top" Width="120"/>
                <ComboBox x:Name="cboxMonth" HorizontalAlignment="Left" Margin="422,427,0,0" VerticalAlignment="Top" Width="120"/>
                <ComboBox x:Name="cboxDay" HorizontalAlignment="Left" Margin="553,427,0,0" VerticalAlignment="Top" Width="120"/>
                <GridView x:Name="grdToDos" SelectionChanged="grdToDos_SelectionChanged_1" HorizontalAlignment="Left" Margin="719,132,0,0" VerticalAlignment="Top" Width="825" Height="460" BorderThickness="2" />
             SelectionChanged="grdToDos_SelectionChanged_1"HorizontalAlignment="Left" Margin="719,132,0,0" VerticalAlignment
            </Grid>
            <AppBar x:Name="appbar" VerticalAlignment="Bottom">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Button Grid.Row="0" Grid.Column="0" x:Name="bttnAddToDo" Content="Add"  Click="bttnAddToDo_Click_1"></Button>
                    <Button Grid.Row="0" Grid.Column="1" x:Name="bttnRemoveToDo" Visibility="Collapsed"   Content="Remove" Click="bttnRemoveFromGrid_Click_1"></Button>
                </Grid>
            </AppBar>



        </Grid>
    </Viewbox>

</Page>

========================================================================

CS code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Collections.ObjectModel;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace MyToDo
{
    public class ToDos
    {
        public string TaskDescription { get; set; }
        public string TaskCategory { get; set; }
        public string TaskDeadLine { get; set; }
        public bool isCompleted { get; set; }
        public ToDos(string description, string category, string deadline, bool isCompleted = false)
        {
            TaskDescription = description;
            TaskCategory = category;
            TaskDeadLine = deadline;
            this.isCompleted = isCompleted;
        }
    }
    public sealed partial class MainPage : Page
    {


        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
       ObservaleCollection<ToDos> toDosCollection = new  ObservaleCollection<ToDos>
        
        public MainPage()
        {
            this.InitializeComponent();
            fillYear(2012, 2090);
            fillMonth();
            fillDays();
            cboxMonth.SelectionChanged += cboxMonth_SelectionChanged;
            cboxYear.SelectionChanged += cboxYear_SelectionChanged;
            lstViewTaskCategories.Items.Add("Personal");
            lstViewTaskCategories.Items.Add("Official");
            lstViewTaskCategories.SelectedIndex = 0;

        }

        void cboxYear_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            fillDays();
        }

        void cboxMonth_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            fillDays();
        }
        void fillDays()
        {
            cboxDay.Items.Clear();
            cboxDay.Items.Add("");
            try
            {
                int days = DateTime.DaysInMonth(Convert.ToInt32(cboxYear.SelectedValue), getMonth(cboxMonth.SelectedValue.ToString()));
                for (int i = 1; i <= days; i++)
                    cboxDay.Items.Add(i);
                cboxDay.SelectedIndex = 0;
            }
            catch (Exception ex)
            {


            }

        }
        int getMonth(string monthName)
        {
            switch (monthName)
            {
                case "January":
                    return 1;
                case "February":
                    return 2;
                case "March":
                    return 3;
                case "April":
                    return 4;
                case "May":
                    return 5;
                case "June":
                    return 6;
                case "July":
                    return 7;
                case "August":
                    return 8;
                case "September":
                    return 9;
                case "October":
                    return 10;
                case "November":
                    return 11;
                case "December":
                    return 12;
                default:
                    return -1;

            }
        }
        void fillMonth()
        {
            cboxMonth.Items.Add("");
            cboxMonth.Items.Add("January");
            cboxMonth.Items.Add("February");
            cboxMonth.Items.Add("March");
            cboxMonth.Items.Add("April");
            cboxMonth.Items.Add("May");
            cboxMonth.Items.Add("June");
            cboxMonth.Items.Add("July");
            cboxMonth.Items.Add("August");
            cboxMonth.Items.Add("September");
            cboxMonth.Items.Add("October");
            cboxMonth.Items.Add("November");
            cboxMonth.Items.Add("December");
            cboxMonth.SelectedIndex = 0;
        }
        void fillYear(int startYear, int endYear)
        {
            cboxYear.Items.Add("");
            for (int i = startYear; i <= endYear; i++)
                cboxYear.Items.Add(i);
            cboxYear.SelectedIndex = 0;
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
        private void bttnAddToDo_Click_1(object sender, RoutedEventArgs e)
        {
           txtdesc.Text = "Please provide complete information.";

            if (string.IsNullOrEmpty(txtTaskDescription.Text.Trim()) || cboxYear.SelectedIndex==0 || cboxMonth.SelectedIndex==0 || cboxDay.SelectedIndex==0)
            {
                txtdesc.Visibility = Windows.UI.Xaml.Visibility.Visible;
               
                return;
            }
            else
            {
                txtdesc.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                
                
                grdToDos.Items.Add(txtTaskDescription.Text);
                txtTaskDescription.Text = "";
                cboxDay.SelectedIndex = cboxMonth.SelectedIndex = cboxYear.SelectedIndex = 0;
                txtTaskDescription.Focus(Windows.UI.Xaml.FocusState.Programmatic);
            }

        }


        private void bttnRemoveFromGrid_Click_1(object sender, RoutedEventArgs e)
        {
           if(grdToDos.SelectedItams.Count > 0)
            {
               try
                {
                  if(toDosCollection.Count >1)
                   {
                     toDosCollection.Remove(grdToDos.SelectedItam as ToDos);
                     grdToDos.ItemsSource = toDosCollection;
                   }
                   else
                   {
                      toDosCollection.Clear();
                      gedToDos.ItemsSource = null;
                   }
                }
                catch (Exception)
                 {
                    
                 }
            }
        }


                    

        private void grdToDos_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (grdToDos.Items.Count > 0 && grdToDos.SelectedItems.Count > 0)
                bttnRemoveToDo.Visibility = Windows.UI.Xaml.Visibility.Visible;
            else
                bttnRemoveToDo.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        }

        private void Grid_Tapped_1(object sender, TappedRoutedEventArgs e)
        {
            appbar.IsOpen = false;
        }

        private void txtTaskDescription_GotFocus(object sender, RoutedEventArgs e)
        {
            appbar.IsOpen = false;
        }
    }
}

No comments:

Post a Comment