如何从arraylist中的文本框输入文本

时间:2019-05-11 19:52:56

标签: c# parsing arraylist combobox textbox

我必须用C#编写表格。在我的表单中,我放置了一些文本框:idBox,descBox,priceBox和qtdBox。我一直想做的是从文本框中获取文本并将其解析为十进制和整数等不同类型,因为这些是我的Product类中所需的类型,它们是构造函数的参数。从这里,我将它们添加到Product对象的数组列表中。然后,我想在具有相同名称的ComboBox中将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}");
            }
        }
    }
}

感谢您的帮助

0 个答案:

没有答案