简体   繁体   English

从列表框行中隔离值

[英]Isolating values from a listbox line

I have got a line in a lisbox that i need so i can print out my receipt for the end of my 12 grade project im doing. 我在礼盒中有一条生产线,因此我可以在我12年级项目结束时打印收据。
Example of my line : "cha1 Adidas Stan Smith White 1 2" (its padded). 我的路线示例: "cha1 Adidas Stan Smith White 1 2" (填充)。 Now what i want to do is isolate like cha1, Adidas stan Smith White,1,2 to add to my Microsoft Access Database, i somehow managed to do it with substring but i screwed up my code and now i cant do it , can somebody help me please ? 现在我想做的就是像cha1一样隔离,将adidas stan Smith White,1,2添加到我的Microsoft Access数据库中,我设法用子字符串做到了,但是我搞砸了我的代码,现在我做不到了,有人可以请帮帮我 ?

My code ,that used to work , looks like this : 我曾经工作的代码如下所示:

 foreach (string item in lstpreview.Items)
            {
            //create the string to print on the reciept
            string nomeproduto = item;

            float quantidade = float.Parse(item.Substring(item.Length -5, 5));
            float precounitario = float.Parse(item.Substring(item.Length - 5, 5));
            string totalproduto = item.Substring(item.Length - 6, 6);

            txt1.Text = Convert.ToString(quantidade);
            txt2.Text = Convert.ToString(precounitario);
            //MessageBox.Show(item.Substring(item.Length - 5, 5) + "PROD TOTAL: " + totalproduto);

            //float totalprice = 0.00f;





        }

You say that the line is padded, but do not give any details. 您说该行已填充,但未提供任何详细信息。 If you know that the first field is always the first 4 characters of the line, you can isolate it with string.Substring : 如果您知道第一个字段始终是该行的前4个字符,则可以使用string.Substring对其进行隔离

string field1 = line.Substring(0, 4);

and similarly for the other fields. 其他领域也是如此。

PS Please edit your post and remove the swear word. 附言:请编辑您的帖子,并删除脏话。

Edit after parsing code added 添加解析代码后进行编辑

I don't understand your comment, what is "your negative value"? 我不理解您的评论,“您的负值”是什么? Run the code in the debugger and find which line causes the error. 在调试器中运行代码,并查找导致错误的行。 Please post the exact error message. 请发布确切的错误消息。

Is there a reason for converting the substring to a float and then back to a string? 是否有理由将子字符串转换为浮点数,然后再转换回字符串? I can imagine that you might want to validate that the field is numeric, but then you would be better to use TryParse . 我可以想象您可能想验证该字段为数字,但是最好使用TryParse

Your second comment is helpful. 您的第二条评论很有帮助。 The last 5 characters of the line are not all numeric, that's the problem. 该行的最后5个字符并非全都是数字,这就是问题所在。

Done it with this snippet of code together with a for each loop. 使用此代码段以及每个循环的来完成该操作。

string[] caracteresnastring = item.Split(new char[] { ',' }.ToArray());
            string code = caracteresnastring[0];
            string name = caracteresnastring[1];
            string price = caracteresnastring[2];
            string quantity = caracteresnastring[3];

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

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