Good day. Help me understand how to fill the combobox with the condition. There is a list of authors of books. Pushkin, Tolstoy, Griboedov and so on. They are placed in combobox1. There is a list of works by Pushkin, 2-3 works, Tolstoy, and so on. The number of pieces is placed in combobox2. For example, in combobox1, I choose Pushkin and in combobox2, the list is just his work. And so on with the rest of the writers. In one combobox the authors and in the other their books. And is it possible to produce content reading for combobox 1-2 from one file?

  • one
    You read the file and depending on what you add in the line to 1 or 2 combos. how to distinguish what where - it already depends on how you have written the lines in the file - Volodymyr
  • For example, can you format the source file itself? The name of the writer after him are lists of the work. Pushkin -Dubrovsky -Capitanian daughter Tolstoy -Anna Karenina -War and peace. In general, what would the works begin with a dash sign. And the author himself just started with a new line. and then in the read condition placed in the combobox. Or so it is impossible? - Petr
  • 2
    it is possible and so can be done as in DB. 3. Columns: writer id, writer id (for books) and the title itself .. for example, share |. It turns out in 1 column only of the authors ID (for the books is blank or dashed), 2 - the IDs of the authors themselves who own the book - Volodymyr
  • thank. I'll try the database just tie it up. - Petr
  • then it is better to create 2 tables in the database with authors and books and link them in a similar way - Volodymyr

1 answer 1

You can do without the database. To do this, create the Author class, which will contain the name and surname of the author, as well as all his books. And the class Book, which will store the name of the book.

Class author

public class Author { private List<Book> books; public Author() { books = new List<Book>(); } public string Name { get; set; } public string Surname { get; set; } public void AddBook(Book book) { books.Add(book); } public List<Book> GetBooks() { return books; } public override string ToString() { return string.Format("{0} {1}", Name, Surname); } } 

Book class

 public class Book { public string Title { get; set; } public override string ToString() { return Title; } } 

After that, you will need to create a List (or any other container) with a global scope.

When reading from a file, you will need to create an object of the Author class and add all the books of the author (objects of the Book class) to it, then add it to the List created earlier. [authors - List with a global scope in which all authors + their books are stored]

Example

 Author a1 = new Author { Name = "A.", Surname = "ΠŸΡƒΡˆΠΊΠΈΠ½" }; a1.AddBook(new Book { Title = "Руслан ΠΈ Π›ΡŽΠ΄ΠΌΠΈΠ»Π°" }); a1.AddBook(new Book { Title = "Π•Π²Π³Π΅Π½ΠΈΠΉ ОнСгин" }); authors.Add(a1); 

Add a SelectedIndexChanged event to combobox1 in which the index will be retrieved from combobox1 and the output of all the books of the author in combobox2.

The code for the SelectedIndexChanged event

 int index = comboBox1.SelectedIndex; if (index < 0 && index >= authors.Count) return; //ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ°, Ρ‡Ρ‚ΠΎΠ±Ρ‹ индСкс Π½Π΅ находился Π·Π° Π³Ρ€Π°Π½ΠΈΡ†Π°ΠΌΠΈ массива comboBox2.Items.Clear(); //очистка combobox2, Ρ‡Ρ‚ΠΎΠ±Ρ‹ Π½Π΅ Π±Ρ‹Π»ΠΎ ΠΊΠ½ΠΈΠ³ Π΄Ρ€ΡƒΠ³ΠΈΡ… Π°Π²Ρ‚ΠΎΡ€ΠΎΠ² foreach(Book b in authors[index].GetBooks()) { comboBox2.Items.Add(b); }