基于组合框选择更新文本框(.CSV /阵列) [英] Update textbox based on combobox selection (.CSV/Array)

查看:124
本文介绍了基于组合框选择更新文本框(.CSV /阵列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含列的.csv文件。我已经有一个形式,它允许用户插入和信息保存到此.csv文件

在一个单独的形式,我创建了选择他们的项目ID项目的能力。我有一个组合框是显示下降从.csv文件项ID下来。现在,唯一的项目ID是1000至1003年。

 公共无效项()
    {
        INT ITEMID = 0;
        盘点[] IArray = Inventory.getInv();
        为(中间体ν= 0; V族; IArray.Length; v ++)
        {
            如果(IArray [V]!= NULL)
            {
                itID = IArray [V] .getIID();
                Field.Items.Add(itID +\ r \ N);
            }
        }
    }
 

当我选择一个特定的项目ID,我想有我的只读文本框用适当的名称/折扣/价格根据文件更新。问题是,不管我选择什么样的标识,其显示.csv文件的最后一行(现在最后一行是物品ID 1003)。

 私人无效Field_SelectedIndexChanged(对象发件人,EventArgs的)
    {
        字符串itName =;
        双盘= 0;
        双价= 0;
        盘点[] IArray = Inventory.getInv();
        为(中间体ν= 0; V族; IArray.Length; v ++)
        {
            如果(IArray [V]!= NULL)
            {
                itName = IArray [V] .getItName();
                圆盘= IArray [V] .getDisc();
                价格= IArray [V] .getPrice();

                itNameS.Text =(itName);
                itPriceS.Text = Convert.ToString(价格);
                itDiscountS.Text = Convert.ToString(盘);
            }
        }
    }
 

解决方案

您可以利用项目的列表,如下面的绑定到你的组合框。然后,您可以设置文本框文本值的指数变化。

我的属性不匹配你完全,但会节省您的时间becasue你不需要每次更改索引的时间来获取项目。

 类项目:INotifyPropertyChanged的
{

    公共事件PropertyChangedEventHandler的PropertyChanged;

    保护无效NotifyPropertyChanged(字符串信息)
    {
        如果(的PropertyChanged!= NULL)
            的PropertyChanged(这一点,新PropertyChangedEventArgs(信息));
    }

    私人字符串_itemId;
    公共字符串的ItemID
    {
        {返回_itemId; }
        集合{_itemId =价值; NotifyPropertyChanged(项目ID); }
    }

    私人字符串_itemName;
    公共ItemName字符串
    {
        {返回_itemName; }
        集合{_itemName =价值; NotifyPropertyChanged(了itemname); }
    }

    私人双人_discountValue;
    大众双DiscountValue
    {
        {返回_discountValue; }
        集合{_discountValue =价值; NotifyPropertyChanged(DiscountValue); }
    }

    私人双人_price;
    公共双人大
    {
        {返回_price; }
        集合{_price =价值; NotifyPropertyChanged(价格); }
    }
}


公共部分类Form1中:形态
{
    的BindingList<项目> OurItems =新的BindingList<项目>();

    公共Form1中()
    {
        的InitializeComponent();
    }

    私人无效Form1_Load的(对象发件人,EventArgs的)
    {
        PopulateItems();
        BindComboBox();


    }

    私人无效BindComboBox()
    {
        cboItems.DataSource = OurItems;
        cboItems.DisplayMember =项目ID;
        cboItems.ValueMember =项目ID;
    }

    私人无效PopulateItems()
    {
        StreamReader的SR =新的StreamReader(@新建文本文档(4).TXT);
        的foreach(字符串SLINE在sr.ReadToEnd()。斯普利特(新的String [] {\ r \ N},StringSplitOptions.RemoveEmptyEntries))
        {
            VAR oProps = sLine.Split(新的char [] {','});
            OurItems.Add(新项目(){的ItemID = oProps [0],了itemname = oProps [1],DiscountValue = Double.Parse(oProps [2]),价格= Double.Parse(oProps [3])});
        }
    }

    私人无效cboItems_SelectedIndexChanged(对象发件人,EventArgs的)
    {
        VAR oSelected =(项目)cboItems.SelectedItem;

        tbName.Text = oSelected.ItemName;
        tbDiscount.Text = oSelected.DiscountValue.ToString();
        tbPrice.Text = oSelected.Price.ToString();

    }

    私人无效的button1_Click(对象发件人,EventArgs的)
    {
        OurItems [0]。价格= OurItems [0]。价格+ 50;
    }
}
 


I have a .csv file containing columns. I already have a form which allows the user to insert and save the information to this .csv file.

On a separate form, I created the ability to select items by their Item ID. I have a combobox which is displaying a drop down of Item IDs from the .csv file. Right now, the only Item IDs are 1000-1003.

    public void Items()
    {
        int itemID = 0;
        Inventory[] IArray = Inventory.getInv();
        for (int v = 0; v < IArray.Length; v++)
        {
            if (IArray[v] != null)
            {
                itID = IArray[v].getIID();
                Field.Items.Add(itID + "\r\n");
            }
        }
    }

When I select a certain Item ID, I would like to have my ReadOnly textboxes update with the appropriate Name/Discount/Price according to the file. The problem is, no matter what ID I choose, it's displaying the last line of the .csv file (right now the last line is Item ID 1003).

    private void Field_SelectedIndexChanged(object sender, EventArgs e)
    {
        string itName = "";
        double disc = 0;
        double price = 0;
        Inventory[] IArray = Inventory.getInv();
        for (int v = 0; v < IArray.Length; v++)
        {
            if (IArray[v] != null)
            {
                itName = IArray[v].getItName();
                disc = IArray[v].getDisc();
                price = IArray[v].getPrice();

                itNameS.Text = (itName);
                itPriceS.Text = Convert.ToString(price);
                itDiscountS.Text = Convert.ToString(disc);
            }
        }
    }

解决方案

You may utilize a List of Items such as the following to bind to your combobox. You may then set the text boxes text values on index changed.

My properties don't match yours exactly, but will save you time becasue you do not need to fetch the items every time you change index.

class Item : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(info));
    }

    private string _itemId;
    public String ItemID
    {
        get { return _itemId; }
        set { _itemId = value; NotifyPropertyChanged("ItemID"); }
    }

    private string _itemName;
    public String ItemName
    {
        get { return _itemName; }
        set { _itemName = value; NotifyPropertyChanged("ItemName"); }
    }

    private double _discountValue;
    public Double DiscountValue
    {
        get { return _discountValue; }
        set { _discountValue = value; NotifyPropertyChanged("DiscountValue"); }
    }

    private double _price;
    public Double Price
    {
        get { return _price; }
        set { _price = value; NotifyPropertyChanged("Price"); }
    }
}


public partial class Form1 : Form
{
    BindingList<Item> OurItems = new BindingList<Item>();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        PopulateItems();
        BindComboBox();


    }

    private void BindComboBox()
    {
        cboItems.DataSource = OurItems;
        cboItems.DisplayMember = "ItemID";
        cboItems.ValueMember = "ItemID";
    }

    private void PopulateItems()
    {
        StreamReader sr = new StreamReader(@"New Text Document (4).txt");
        foreach (string sLine in sr.ReadToEnd().Split(new string[] {"\r\n"}, StringSplitOptions.RemoveEmptyEntries ))
        {
            var oProps = sLine.Split(new char[] {','});
            OurItems.Add(new Item() {ItemID = oProps[0], ItemName = oProps[1], DiscountValue = Double.Parse(oProps[2]), Price = Double.Parse(oProps[3])});
        }
    }

    private void cboItems_SelectedIndexChanged(object sender, EventArgs e)
    {
        var oSelected = (Item)cboItems.SelectedItem;

        tbName.Text = oSelected.ItemName;
        tbDiscount.Text = oSelected.DiscountValue.ToString();
        tbPrice.Text = oSelected.Price.ToString();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        OurItems[0].Price = OurItems[0].Price + 50;
    }
}


这篇关于基于组合框选择更新文本框(.CSV /阵列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆