简体   繁体   English

如何在列表框中获取特定字符

[英]How to get specific character in listbox

I have a listbox in which my selected products are stored like this ... 我有一个列表框,其中存储了我选择的产品,就像这样...

'Product name'.padright(30) 'price' 'quantity' '产品名称'.padright(30)'价格''数量'

listBox1.Items.Add(details.Name.PadRight(30) + details.Price.ToString() + " " + 1 );

but when I read price of a product it selects price and quantity 但是当我阅读产品的价格时,它会选择价格和数量

string currentPriceString = foundItem.Replace(details.Name.PadRight(30), "");

string quantityString = foundItem.Replace(details.Name.PadRight(33), "");

I only want price in currentPriceString and quantity in quantityString 我只想要currentPriceString价格和quantityString中的quantityString

here is complete code of this method 这是此方法的完整代码

private void ProductButton_Click(object sender, EventArgs e)
{

    Button ProductButton = sender as Button;
    DataAccess dataAccess = new DataAccess();
    int ProductID = Convert.ToInt32(ProductButton.Tag);

    Details details = dataAccess.ReadProductDetails(ProductID);

    decimal price = details.Price;

    string foundItem = CheckProductInListBox(details.Name);

    if (!String.IsNullOrEmpty(foundItem))
    {
        string currentPriceString = foundItem.Replace(details.Name.PadRight(30), "");
        decimal currentPriceValue;
        string quantityString = foundItem.Replace(details.Name.PadRight(33), "");
        int quantiy;
        MessageBox.Show(currentPriceString);

        if (Decimal.TryParse(currentPriceString, out currentPriceValue))
        {
            quantiy = Convert.ToInt16(quantityString);

            currentPriceValue += price;
            quantiy++;
            string newItem = details.Name.PadRight(30) + currentPriceValue.ToString()  + quantiy.ToString();

            int index = listBox1.Items.IndexOf(foundItem);
            listBox1.Items[index] = newItem;

        }
        else
        {
            MessageBox.Show("Error");
        }

    }
    else
    {
        listBox1.Items.Add(details.Name.PadRight(30) + details.Price.ToString() + " " + 1 );
    }
}

private string CheckProductInListBox(string name)
{
    foreach (string item in listBox1.Items)
    {
        if (item.Contains(name))
        {
            return item;
        }
    }
    return String.Empty;
}

On replacing ( foundItem.Replace(details.Name.PadRight(33), ""); ), you are just removing the name part from the string, so the price and quantity will be there for sure. 替换( foundItem.Replace(details.Name.PadRight(33), ""); )时,您只是从字符串中删除名称部分,因此价格和数量肯定会在那里。

You should can try this code, 您应该可以尝试以下代码,

// suppose your found text is like this,
//foundItem = "AMIT".PadRight(30) + "200" + " " + "1";

You can get price and quantity separately like below: 您可以分别获得价格和数量,如下所示:

string currentPriceQuantityString = foundItem.Replace(details.Name.PadRight(30), "");
//currentPriceQuantityString => "200 1"

string[] strArray = currentPriceQuantityString.Split();

string currentPriceString = strArray[0]; //currentPriceString => "200"
string quantityString = strArray[1];     //quantityString => "1"

Side note: 边注:

I guess your line: 我猜你的台词:

listBox1.Items.Add(details.Name.PadRight(30) + details.Price.ToString() + " " + 1 );

..should be: ..应该:

listBox1.Items.Add(details.Name.PadRight(30) + details.Price.ToString() + " " + "1" );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM