Exemplo n.º 1
0
        private static void GetListName(ShoppingList sList)
        {
            //sList.NameChanged += new NameChangedDelegate(OnNameChanged); // Whenever somone invokes this delegate instance, call OnNameChanged
            // code below is equivalent to that above. C# does the above behind the scenes.
            sList.NameChanged += OnNameChanged;

            sList._name = "Default list"; // sets the field, rather than the property which doesn't fire the event.

            try
            {
                Console.WriteLine("Name your shopping list.");
                sList.Name = Console.ReadLine();
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 2
0
        static void OnExit(object sender, ExitingEventArgs args) // here's where the args are packaged. not su
        {
            ShoppingList shopList = new ShoppingList();

            bool success = false;
            int  tries   = 3;

            while (!success && tries > 0)
            {
                try
                {
                    using (StreamWriter outputFile = File.CreateText(@"c:\Created_Lists\" + args.listName + "-List.txt"))
                    {
                        shopList.PrintList(outputFile, args.listContents); // Some troube calling as the list itself was hard to access
                    }

                    using (StreamWriter outputFile = File.CreateText(@"c:\Created_Lists\" + args.listName + "-List.csv"))
                    {
                        shopList.CSVList(outputFile, args.listContents);
                    }
                    success = true;
                }
                catch (DirectoryNotFoundException)
                {
                    Console.WriteLine($"The file wasn't saved. There may be illegal characters in your list name \"{args.listName}\". Try another (y/n)?");
                    string userResponse = Console.ReadLine().ToLower();
                    if (userResponse == "y" || userResponse == "yes")
                    {
                        Console.WriteLine("Enter the new name.");
                        args.listName = Console.ReadLine();
                    }
                    tries--;
                    if (tries <= 0)
                    {
                        Console.WriteLine("This didn't work. Sorry, closing without saving.");
                    }
                }
                catch (ArgumentException)
                {
                    Console.WriteLine($"The file wasn't saved. There were illegal characters in your list name \"{args.listName}\". Try another (y/n)?");
                    string userResponse = Console.ReadLine().ToLower();
                    if (userResponse == "y" || userResponse == "yes")
                    {
                        Console.WriteLine("Enter the new name.");
                        args.listName = Console.ReadLine();
                    }
                    tries--;
                    if (tries <= 0)
                    {
                        Console.WriteLine("This didn't work. Sorry, closing without saving.");
                    }
                }
                catch (NotSupportedException)
                {
                    Console.WriteLine($"The file wasn't saved. There may be illegal characters in your list name, probably at the beginning. \"{args.listName}\". Try another (y/n)?");
                    string userResponse = Console.ReadLine().ToLower();
                    if (userResponse == "y" || userResponse == "yes")
                    {
                        Console.WriteLine("Enter the new name.");
                        args.listName = Console.ReadLine();
                    }
                    tries--;
                    if (tries <= 0)
                    {
                        Console.WriteLine("This didn't work. Sorry, closing without saving.");
                    }
                }
            }
        }