Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取列表框中的特定字符_C#_Winforms - Fatal编程技术网

C# 如何获取列表框中的特定字符

C# 如何获取列表框中的特定字符,c#,winforms,C#,Winforms,我有一个列表框,在其中我选择的产品存储如下 “产品名称”。padright(30)“价格”“数量” listBox1.Items.Add(details.Name.PadRight(30) + details.Price.ToString() + " " + 1 ); 但当我阅读产品的价格时,它会选择价格和数量 string currentPriceString = foundItem.Replace(details.Name.PadRight(30), ""); string quanti

我有一个列表框,在其中我选择的产品存储如下

“产品名称”。padright(30)“价格”“数量”

listBox1.Items.Add(details.Name.PadRight(30) + details.Price.ToString() + " " + 1 );
但当我阅读产品的价格时,它会选择价格和数量

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

string quantityString = foundItem.Replace(details.Name.PadRight(33), "");
我只想要当前价格字符串中的价格和数量字符串中的数量

下面是这个方法的完整代码

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;
}
在替换(
foundItem.Replace(details.Name.PadRight(33),“”);
时,您只是从字符串中删除了名称部分,因此价格和数量肯定会存在

你可以试试这个代码

// suppose your found text is like this,
//foundItem = "AMIT".PadRight(30) + "200" + " " + "1";
您可以分别获得价格和数量,如下所示:

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"

旁注:

我猜你的台词是:

listBox1.Items.Add(details.Name.PadRight(30) + details.Price.ToString() + " " + 1 );
…应该是:

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

从标记的wiki:“如果您对Visual Studio的功能和特性有特殊疑问,请使用此标记。不要在有关代码的问题上使用此标记,因为这些代码恰好是在VisualStudio中编写的。考虑标记您的问题链接到的确切技术区域,并标记一个更具体的VisualStudio版本。请在问题中提及您的确切VS版本、版本和更新级别。“能否共享
foundItem
的一个示例值?它返回列表框中已存在的产品名称,为什么不创建自己的产品类型来保存所有这些值?”。重写ToString方法以在列表框中按所需方式显示它。然后在需要时从selecteditem获取属性值。这是错误的方法。ListBox是一个显示信息的小工具,总是努力将数据和视图分开。您只需要一个
列表
,即可跟踪框中项目的所有属性。这也是利用数据绑定的下一步。