如何将列表中的每个项目发送到C#中的函数? [英] How to send each item from a list to a function in C# ?

查看:52
本文介绍了如何将列表中的每个项目发送到C#中的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我再去一次!

我有一个手动和自动打印的项目,但需要从列表中开始打印!



我发送了itens我想要一个主表单中的列表,这很好!

但是在打印每个项目之前,如1251000012它通过一个获取其信息的事件产品名称,价格,分类和其他要打印的产品!



Hi guys here I go again!
I have a project that print manually and auto but the scenary need to start to print from a list!

I got send the itens I want to a list in a Primary Form that's fine!
but Before to print each item like "1251000012" it pass by an event that gets its information like product name,price,classification and others to print!

number_TextChanged_1(object sender, EventArgs e)
{
  if(number.TextLenth >=10)
  {
    // start to attribute each number's information
  }
  if(checkbox_autoprint.Checked ==true)
  {
     button_print.PerformClick();
  }

}

What I have tried:





我试着这样做...但是还没有工作!

我注意到这个过程太快没花时间来根据需要获取数量的信息...

云服务器中的Sql Server需要这些数字信息,并且需要几毫秒来检索





I tried to do like this...but didn't work yet!
I noticed that this process goes too fast not taking time enough to get the numbe's information as needed...
these number's informations is required by Sql Server in a Cloud server and takes some milli senconds to retrieve

foreach(string n in mylist)
                {
                  number.text = l.toString();
                }





下一步该事件将在下面调用......但它不是



The Next step Shoud be called by this Event Below...but it's not

number_TextChanged_1(object sender, EventArgs e)
{
  if(number.TextLenth >=10)
  {





任何想法都会受到赞赏...谢谢你们!



Any Idea will be appreciated...thanks guys!

推荐答案

那里可以在这里做几点:

There are several points that can be made here:
if(checkbox_autoprint.Checked ==true)



您永远不必将布尔值与布尔值进行比较;你可以直接使用第一个布尔值。所以:


You never have to compare a boolean to a boolean; you can use the first boolean directly. So:

if (checkbox_autoprint.Checked)



足够且更优雅。

----------


is both sufficient and more elegant.
----------

foreach(string n in mylist)
   {
   number.text = l.toString();
   }



这里你永远不会使用 foreach 循环的字符串变量(名词)。此外,您继续将相同的值( l.ToString())分配给 number.Text 。什么是 l 无论如何?

你想在这部分代码中做些什么?

- --------

在运行时,if块中的代码永远不会被执行:



嗯,这意味着条件 number.TextLenth> = 10 为false。

了解原因的唯一方法是以调试模式启动项目(从VS),在事件处理程序的开头放置一个断点,然后按F5。一旦执行到达断点,它就会暂停,您必须逐行执行代码。这允许精确地检查哪些值由变量保持。



调试远非可选技能,你应该很早就开始使用它。

首先,它会迫使您更准确地定义标准和约束;第二,你将学习对代码执行的理解,它将为你提供有关如何编写更高效代码的线索。



也许高级调试在Visual Studio 中[ ^ ]可能是一个很好的起点。

--------- -

使用 TextChanged 事件处理程序时,每次更改控件中的文本时都会执行该事件处理程序。即,在每次击键时。

也许你只有在控制完全合格时才能执行它?在这种情况下,您应该使用验证验证事件。

但是我我不太确定完全理解你的要求。

----------

我希望对你有用。亲切的。


Here you never use the string variable of the foreach loop (n). Moreover, you keep assigning the same value (l.ToString()) to number.Text. What is l anyway?
What do you want to do exactly in this part of the code?
----------
At runtime, the code inside the if block is never executed:

Well, that means that the condition number.TextLenth >= 10 is false.
The only way to know why, is to launch your project in debug mode (from VS), by putting a breakpoint at the beginning of your event handler and by pressing F5. Once execution reaches the breakpoint, it is paused, and you have to execute the code line by line. This allows to check precisely which values are holded by variables.

Debugging is far from an optional skill, and you should have a go at it quite early.
First, it will force you to define your criterias and constraints more precisely; second, you will learn understanding of code execution, and it will give you clues about how to write more efficient code.

Maybe Advanced Debugging in Visual Studio[^] could be a good starting point.
----------
When you use the TextChanged event handler, it is executed every time the text in the control is changed; i.e., at each key stroke.
Maybe you could execute it only when the control has been fully qualified? In that case, you should use the Validating and Validated events instead.
But I'm not very sure to have fully understood your requirement on this.
----------
I hope all that makes sense for you. Kindly.


伙计们花了几个小时三分!我得到了一个解决方案!



列表中的所有itens都是表单2中的令牌,并通过公共列表加载到Form1中它工作正常!


我有一个复选框用于自动打印(当用户使用条形码扫描仪时)



所以在活动上

Hi guys after spent some hours triyng! I got a Solution!

All the itens from a list are token in the Form 2 and loaded in the Form1 by a public List it was working fine!

in the Form1 I had a checkbox in which was used to auto print (when the user handle with a barcode scanner)

So on the Event
private void cbxautoprint_CheckedChanged(object sender, EventArgs e)



我设置一个条件来检查是否有itens该列表如果是这样我使用了这个...




I set a condition to check if there's itens on the list yet if so I used this...

foreach (string number in Frm1.MyListClass.List)
  {
     var startTime = DateTime.UtcNow; 

         UserSet.Number = number; //UserSet.Number is on a public static class
         Counting_Numbers(1);     // it's a function to subtract -1 from the count   of the Frm1.MyListClass.List.Count and Set -1 to a UserSet.Number_Counter                                              
       

          while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(3))
               {
                                    
                btnautoprint.PerformClick(); //execute the printing performance

               }
                                                           
    }


这篇关于如何将列表中的每个项目发送到C#中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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