//Constructor #1 (for creating a new set of flash cards) public CreateAndModify() { InitializeComponent(); //creating a new list of cards (empty) listOfCards = new ListOfCards(); }
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 } }
//Constructor #2 (for modifying an existing set of flash cards) public CreateAndModify(ListOfCards tmpListOfCards, string fileName) //Takes an Object because it does not like that it when it is of type ListOfCards { InitializeComponent(); //Setting the text of the form this.Text = "C & M Flash Cards: " + fileName; this.listOfCards = tmpListOfCards; //Casting the object to be a list of cards //Updating the listbox to show the cards updateListBox(); }
private bool answerShown; //Tells us whether the answer is being shown public UsingFlashCards(ListOfCards c, string fileName) { InitializeComponent(); //Setting the name of the form this.Text = "Flash Card: " + fileName; //Putting the Card into the listOfCards listOfCards = c; //Setting variable to there default answerShown = false; //This is becuase the answer is first hidden answerHiddenMessage = "Click the button below to reveal!"; //This is the message that will be displayed when the answer is hidden //Loading first question when the form loads loadQuestion(listOfCards.getNextCard()); }
//Reads a given file path, and then returns the content as a 'ListOfCards' public static ListOfCards readFromFile(string fileName) { //Reading from the file the user selected using a stream using (StreamReader r = new StreamReader(fileName)) { //Reading file into a string string json = r.ReadToEnd(); //Converting the json string into a 'Card' object (Using the below card class) List <tmpCard> tmpListOfCards = JsonConvert.DeserializeObject <List <tmpCard> >(json); //Creating a instance of ListOfCards, so we can return it once the cards have been put in ListOfCards returnListOfCards = new ListOfCards(); //Putting the 'Cards' from the 'tmpListOfCards' into the 'listOfCards' (Covering from a tmpCard to a Card, foreach element) foreach (tmpCard c in tmpListOfCards) { returnListOfCards.addCard(new Card(c.question, c.answer)); } //Returning the 'ListOfCard' that was read from the file return(returnListOfCards); } }