I am new to Windows Forms and my assignment is to create a program that upon launch, populates a list of books from a txt file and then allow the user to click a button and display a random book as a suggestion of what to read. This is all working as intended.
My problem is that I want to be able to click a button, open a 2nd form which then allows the user to add a new book to both the List and the txt file. I have successfully made the new Book object add to the txt file and upon closing and relaunching the program, the new book shows up when I display random books but I am having trouble adding it to List books. The list has to be created by an object of the class “Import” but I cant figure out how to pass import into Form2 to be able to access the List when adding the new Book.
Classes.cs
namespace Tipsmaskinen
{
internal class Book
{
public string Title { get; set; }
public string Author { get; set; }
public string BookType { get; set; }
public bool Available = false;
public Book(string title, string author)
{
Title = title;
Author = author;
}
public override string ToString()
{
if (Available)
{
return Title + " av " + Author + Environment.NewLine + "(" + BookType + ")" + Environment.NewLine + "Boken finns tillgänglig!";
}
else
{
return Title + " av " + Author + Environment.NewLine + "(" + BookType + ")" + Environment.NewLine + "Boken är tyvärr inte tillgänglig!";
}
}
}
internal class Novel : Book
{
public Novel(string title, string author)
: base(title, author)
{
BookType = "Roman";
}
}
internal class Magazine : Book
{
public Magazine(string title, string author)
: base(title, author)
{
BookType = "Tidskrift";
}
}
internal class ShortStories : Book
{
internal ShortStories(string title, string author)
: base(title, author)
{
BookType = "Novellsamling";
}
}
internal class Import
{
public List<Book> books = new List<Book>();
public void FileLoader()
{
List<string> itemSaver = new List<string>();
if (File.Exists("library.txt"))
{
StreamReader reader = new StreamReader("library.txt", Encoding.UTF8, false);
string item = "";
while ((item = reader.ReadLine()) != null)
{
itemSaver.Add(item);
}
foreach (string a in itemSaver)
{
string[] vektor = a.Split(new string[] { "###" }, StringSplitOptions.None);
if (vektor[2] == "Roman")
{
Novel newNovel = new Novel(vektor[0], vektor[1]);
if (vektor[3] == "true")
{
newNovel.Available = true;
}
if (newNovel.Title != null && newNovel.Author != null)
{
books.Add(newNovel);
}
}
if (vektor[2] == "Tidskrift")
{
Magazine newMagazine = new Magazine(vektor[0], vektor[1]);
if (vektor[3] == "true")
{
newMagazine.Available = true;
}
if (newMagazine.Title != null && newMagazine.Author != null)
{
books.Add(newMagazine);
}
}
if (vektor[2] == "Novellsamling")
{
ShortStories newShorts = new ShortStories(vektor[0], vektor[1]);
if (vektor[3] == "true")
{
newShorts.Available = true;
}
if (newShorts.Title != null && newShorts.Author != null)
{
books.Add(newShorts);
}
}
}
reader.Close();
}
else
{
using (FileStream fs = File.Create("library.txt"))
MessageBox.Show("library.txt hittades inte och har skapats!");
}
}
}
}
Form1.cs
namespace Tipsmaskinen
{
public partial class Form1 : Form
{
Import import = new Import();
public Random rnd = new Random();
public int lastResult;
public Form1()
{
InitializeComponent();
import.FileLoader();
}
private void SuggestButton_Click(object sender, EventArgs e)
{
textBox1.Clear();
bool repeat = true;
while(repeat)
{
int result = rnd.Next(0, import.books.Count);
if (result != lastResult)
{
textBox1.Text = import.books[result].ToString();
repeat = false;
lastResult = result;
}
}
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void addButton_Click(object sender, EventArgs e)
{
var form2 = new Form2();
form2.Show();
}
}
}
Form2.cs
namespace Tipsmaskinen
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void saveButton_Click(object sender, EventArgs e)
{
if (titleBox.Text != "" && authorBox.Text != "" && booktypeBox.Text != "")
{
Book newBook = new Book(titleBox.Text, authorBox.Text);
newBook.BookType = booktypeBox.Text;
newBook.Available = availableBox.Checked;
import.books.Add(newBook);
string newBookAvailable = "false";
if (availableBox.Checked)
{
newBookAvailable = "true";
}
string newBookExport = titleBox.Text + "###" + authorBox.Text + "###" + booktypeBox.Text + "###" + newBookAvailable;
using (StreamWriter writer = new StreamWriter("library.txt", append: true))
{
writer.WriteLine(newBookExport);
}
MessageBox.Show("Boken tillagd!");
this.Close();
}
else
{
MessageBox.Show("Titel, författare och boktyp är alla obligatoriska!");
}
}
}
}
I tried adding import as a parameter to the method but this gave me an error with the event handler.