Как ввести текст из текстового поля в arraylist - PullRequest
0 голосов
/ 11 мая 2019

Я должен сделать форму в C #.В моей форме я поместил несколько текстовых полей: idBox, descBox, priceBox и qtdBox.То, что я пытался сделать, это получить текст из текстовых полей и разобрать его на различные типы, такие как десятичные и целые, потому что это типы, необходимые в моем классе Product как параметры из конструктора.Отсюда я добавляю их в массив объектов Product.Затем я хотел бы отобразить ToString продукта в виде отдельного элемента в поле со списком с тем же именем.Но когда я делаю только строку, поле Product отображается нормально, а другие отображают 0. Что не так там?

Класс продукта

    class Product
    {
    private int id;
    private String description;
    private decimal price;
    private int quantity;

    public int ID
    {
        get
        {
            return id;
        }
        set
        {
            if( id > 0)
            {
                id = value;
            }  
        }
    }
    public String Description
    {
        get
        {
            return description;
        }
        set
        {
            description = value;
        }
    }
    public decimal Price
    {
        get
        {
            return price;
        }
        set
        {
            if (price > 0)
            {
                price = value;
            }
        }
    }
    public int Quantity
    {
        get
        {
            return quantity;
        }
        set
        {
            if (quantity > 0)
            {
                quantity = value;
            }
        }
    }

    public Product(int formID, String formDescription, decimal formPrice, int formQuantity)
    {
        this.ID = formID;
        this.Description = formDescription;
        this.Price = formPrice;
        this.Quantity = formQuantity;
    }

    public override string ToString()
    {
        return $"ID: {this.ID}, Description: {this.Description}, Price: {this.Price}, Quantity: {this.Quantity} \n"; 
    }
}

Форма класса

    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    ArrayList productList = new ArrayList();

    private void button4_Click(object sender, EventArgs e)
    {
        String text = null;
        text += "All Products \n";
        foreach(Product p in productList)
        {
            text += p.ToString();
        }
        MessageBox.Show(Text);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if(idBox.Text!=null && descBox.Text != null && priceBox.Text != null && qtdBox.Text != null)
        {
            String text1 = idBox.Text;
            int number = int.Parse(text1);
            decimal price;
            decimal.TryParse(priceBox.Text, out price);
            String text2 = qtdBox.Text;
            int quantity = int.Parse(text2);
            String normal = descBox.Text;
            String error = null;
            try
            {
                int num = Convert.ToInt32(idBox.Text);
            }
            catch (FormatException)
            {
                error += "The product has an incorrect ID format \n";
            }

            try
            {
                decimal pr = Decimal.Parse(priceBox.Text);
            }
            catch (FormatException)
            {
                error += "The product has an incorrect price format \n";
            }

            try
            {
                int qt = Convert.ToInt32(qtdBox.Text);
            }
            catch (FormatException)
            {
                error += "The product has an incorrect quantity format \n";
            }

            if(error == null)
            {
                Product p = new Product(number, normal, price, quantity);
                productList.Add(p);
                label6.Text = p.ToString();
                comboBox.Items.Add(p.ToString());
                MessageBox.Show("Product Added Successfully");
            }
            else
            {
                MessageBox.Show($"{error}");
            }
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (idBox.Text != "" && descBox.Text != "" && priceBox.Text != "" && qtdBox.Text != "")
        {
            int number = Convert.ToInt32(idBox.Text);
            decimal price = Decimal.Parse(priceBox.Text);
            int quantity = Convert.ToInt32(qtdBox.Text);
            String error = null;

            try
            {
                int num = Convert.ToInt32(idBox.Text);
            }
            catch (FormatException)
            {
                error += "The product has an incorrect ID format \n";
            }

            try
            {
                decimal pr = Decimal.Parse(priceBox.Text);
            }
            catch (FormatException)
            {
                error += "The product has an incorrect price format \n";
            }

            try
            {
                int qt = Convert.ToInt32(qtdBox.Text);
            }
            catch (FormatException)
            {
                error += "The product has an incorrect quantity format \n";
            }

            if (error == null)
            {
                MessageBox.Show("Product Removed Successfully");
                Product p1 = new Product(number, descBox.Text, price, quantity);
                foreach(Product p in productList)
                {
                    if (p.ID.Equals(p1.ID))
                    {
                        productList.Remove(p);
                    }
                }
                comboBox.Items.Remove(p1.ToString());
            }
            else
            {
                MessageBox.Show($"{error}");
            }
        }
    }
}

Любая помощь приветствуется

...