C#可视化 商品信息管理系统(具体做法及全部代码)
发布人:shili8
发布时间:2024-01-05 15:21
阅读次数:140
以下是一个简单的C#可视化商品信息管理系统的示例代码:
csharpusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProductManagementSystem{
public partial class MainForm : Form {
private List products;
public MainForm()
{
InitializeComponent();
products = new List();
}
private void AddButton_Click(object sender, EventArgs e)
{
string name = nameTextBox.Text;
string category = categoryTextBox.Text;
decimal price = decimal.Parse(priceTextBox.Text);
Product product = new Product(name, category, price);
products.Add(product);
RefreshProductList();
ClearInputFields();
}
private void RefreshProductList()
{
productList.Items.Clear();
foreach (Product product in products)
{
productList.Items.Add(product);
}
}
private void ClearInputFields()
{
nameTextBox.Text = "";
categoryTextBox.Text = "";
priceTextBox.Text = "";
}
}
public class Product {
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
public Product(string name, string category, decimal price)
{
Name = name;
Category = category;
Price = price;
}
public override string ToString()
{
return $"{Name} - {Category} - {Price:C}";
}
}
}
上述代码中,我们创建了一个名为`MainForm`的窗体类,用于显示商品信息管理系统的主界面。该窗体包含了三个文本框(用于输入商品名称、类别和价格)、一个添加按钮和一个商品列表框。在窗体的构造函数中,我们初始化了一个空的商品列表。
当用户点击添加按钮时,我们从文本框中获取输入的商品信息,并创建一个`Product`对象。然后将该对象添加到商品列表中,并调用`RefreshProductList`方法刷新商品列表框。最后,我们调用`ClearInputFields`方法清空输入文本框。
`RefreshProductList`方法用于刷新商品列表框。我们首先清空列表框中的所有项,然后遍历商品列表,将每个商品对象添加到列表框中。
`ClearInputFields`方法用于清空输入文本框。我们将所有文本框的`Text`属性设置为空字符串。
`Product`类表示一个商品对象,包含名称、类别和价格属性。我们重写了`ToString`方法,以便在商品列表框中显示商品的字符串表示形式。
请注意,上述代码只是一个简单的示例,可能还需要添加其他功能,如编辑、删除商品等。这取决于具体的需求。

