-1

How to save information from TextBoxes and I want to get advise about the best way how to Implement info record.

I have the main Form1. There're ListBox1 with products names and TextBox1. When user is choosing any product from ListBox1 in TextBox1 is appeared info about this product. Form1

Also when user click on the button "Add product" Form2 opens. There (Form2) user input info about new product in TextBox1-4. And after when user click "Ok" the entered info is saved. Form2

My question is: how best to save info from TextBox1-4 (Form2) - in an array, list, use a class?

Filburt
  • 17,626
  • 12
  • 64
  • 115
steris
  • 1
  • I think an List<> would work well, if you create a Product class - but do you need to save this information after your application closes? Would a text file be sufficient, or do you need a database? – Robin Bennett Jan 24 '20 at 09:53
  • You can use a Product class and a list – Marwen Jaffel Jan 24 '20 at 09:54
  • What is your idea of "best" ? To represent a product you should use a class. – Manuel Fabbri Jan 24 '20 at 09:56
  • Yes, I need to save this new information after app closes. – steris Jan 24 '20 at 09:56
  • 1
    [Binding a TextBox to a ListBox SelectedItem](https://stackoverflow.com/a/57235083/7444103) – Jimi Jan 24 '20 at 10:15
  • Are you talking about in-memory save? Then create a dedicated `class Product` and `class Order` will have to contain a property `List`. If you are talking about files, then look into serialization and databases. – Sinatr Jan 24 '20 at 10:43
  • If you need to save the data to disc without a database, you can use JSON serialization. It's just two commands to both save an reload the whole data structure. The classes must be marked as `[Serializable]`, as shown in the answer I linked. – Jimi Jan 24 '20 at 11:48
  • Or for a simple life I'd make a dataset, datatable for all your properties, it will then appear in visual studio's data sources window and can be dragged to the form to create text boxes that are binded to the datatable. Put a couple of buttons on there to load and save and use the built in ReadXml/WriteXml methods of a dataset. Could all be designed visually using the mouse in about 5-10 minutes and completed with a couple of lines of code for saving, maybe another couple of lines for adding a new row to the datatable – Caius Jard Jan 25 '20 at 09:10
  • Jimi linked an example though he set the bindings in code rather than in the forms designer; most that code in the linked answer can be written automagically with a few mouse clicks – Caius Jard Jan 25 '20 at 09:14

1 Answers1

0

In my opinion the best way to save this information is using a class of Product, then also having a List<Product> in another class.

 public class Product
    {
        public string Name { get; set; }
        public string Company { get; set; }
        public double Price { get; set; }
        public double Discount { get; set; }

        public Product(string name, string company, double price, double discount)
        {
            Name = name;
            Company = company;
            Price = price;
            Discount = discount;
        }
    }

Some other class:

public List<Product> Products = new List<Product>();

If you need to save this when the application closes, you can use a database or simply use JSON. Link to JSON serialization: newtonsoft

Don't forget to add the NuGet package 'Newtonsoft.Json': Right click class -> Manage NuGet Packages -> Browse -> Install Newtonsoft.Json

Mayfair
  • 358
  • 1
  • 11