示例#1
0
        public static bool writeToFile(string fileName, ListOfCards content)
        {
            try
            {
                //Putting the cards in 'content' into a List<tmpCard> for processing
                List <tmpCard> tmpListOfCards = new List <tmpCard>();

                for (int x = 0; x <= (content.getNumOfCards() - 1); x++)
                {
                    tmpListOfCards.Add(new tmpCard()
                    {
                        question = content.getCardAt(x).getQuestion(),
                        answer   = content.getCardAt(x).getAnswer()
                    });
                }
                //Converting 'tmpListOfCards' into JSON
                string json = JsonConvert.SerializeObject(tmpListOfCards.ToArray());

                //Writing to the file 'json'
                System.IO.File.WriteAllText(fileName, json);

                return(true);    //Writing to the file was successful
            }
            catch
            {
                return(false);   //Writing to file failed
            }
        }
示例#2
0
        //An event handler that executes when the user click's on the delete button
        private void btnDelete_Click(object sender, EventArgs e)
        {
            //Checking if the user has nothing selected in the listBox
            if (listBoxOfQuestions.SelectedIndex <= -1)       //*** Need to test this ***
            {
                MessageBox.Show("Please select something to delete in the list first!");
            }
            else
            {
                try
                {
                    //Finding the item the user has selected in 'listOfCards'
                    Card selectedCard = listOfCards.getCardAt(listBoxOfQuestions.SelectedIndex);

                    //Checking if the item the user has selected exists (not null)
                    if (selectedCard != null)
                    {
                        //removing selected card
                        listBoxOfQuestions.Items.RemoveAt(listBoxOfQuestions.SelectedIndex); //Removing it from the listBox
                        listOfCards.removeCard(selectedCard);                                //Removing it from the list of cards
                    }
                    else
                    {
                        //Telling the user an error has happened
                        MessageBox.Show("The card you have selected was not deleted becuase of an error (1)");
                    }
                }
                catch
                {
                    //Telling the user an error has happened
                    MessageBox.Show("The card you have selected was not deleted becuase of an error (2)");
                }
            }
        }