首页 > 解决方案 > 如何从arraylist中的文本框输入文本

问题描述

我必须用 C# 做一个表格。在我的表单中,我放置了一些文本框:idBox、descBox、priceBox 和 qtdBox。我一直在尝试做的是从文本框中获取文本并将其解析为十进制和 int 等不同类型,因为这些类型是我的 Product 类中需要作为构造函数的参数的类型。从这里我将它们添加到 Product 对象的数组列表中。然后我想将产品的 ToString 显示为具有相同名称的 ComboBox 中的单个项目。但是当我只正常显示字符串 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}");
            }
        }
    }
}

任何帮助表示赞赏

标签: c#parsingarraylistcomboboxtextbox

解决方案


推荐阅读