简体   繁体   English

ProcessBar计数器C#

[英]ProcessBar counter C#

I have a question about the ProcessBar on C#, How would I add the value of 1 to a label if the method used when passing an item within a list box was successful ? 我对C#上的ProcessBar有疑问,如果在列表框中传递项目时使用的方法成功,如何将1的值添加到标签?

I have a method like this 我有这样的方法

private static  Form1 f1 = Application.OpenForms["Form1"] as Form1;
public static void GroupList1(processBar bar)
{
 f1.listBox1.Items.Add("User1");
 bar.Value = 100;
}

public static void GroupList2(processBar bar2)
{
 f1.listBox1.Items.Add("User2");
 bar.Value = 100;
} // Etc, etc - up to GroupList6

I would also like to have a label that tells me how many user's were successfully added (using the bar), I was thinking of adding a method like this : 我还想有一个标签,告诉我成功添加了多少用户(使用栏),我在考虑添加这样的方法:

if (bar.Value = 100)
{
  f1.label1.Text = "" + 1; 
}

Inside of my GroupList1/2 method, but the label always appears as the value 1 . 在我的GroupList1 / 2方法内部,但标签始终显示为值1。

This method within the main form of my code loads a separate label : 我的代码主要形式内的此方法会加载一个单独的标签:

for(int i = 0; int i < listBox1.Items.Count; i++)
{ 
 label2.Text = i.ToString();
} 

So, I would like label 1 to increase by 1 if the user has been loaded into my list box successfully, how would I do this ? 因此,如果用户已成功加载到列表框中,我希望标签1增加1,我该怎么做?

Obviously this isn't actually the code I'm using within my program, a method is used if the selected index changes (which is why I want to increase by 1, to ensure the user parsed the method successfully), but the question still remains as described, thanks. 显然,这实际上不是我在程序中使用的代码,如果所选索引发生更改,则使用一种方法(这就是为什么我要增加1以确保用户成功解析该方法),但是问题仍然存在仍然如前所述,谢谢。

It's a bad idea to store the actual count in the label's Text property, instead, create a variable: 将实际计数存储在标签的Text属性中是一个坏主意,而是创建一个变量:

private static int count;

Now, change your code to something like this: 现在,将代码更改为如下所示:

if (bar.Value = 100)
{
    // Add 1
    count += 1; 
    // Update the UI
    f1.label1.Text = count.ToString(); 
}

I dont quite understand well what are you trying to achieve, but if you only look in increasing the value of such label then you can easily do this by 我不太了解您要达到的目标,但是如果您只是希望增加此类标签的价值,则可以通过以下方式轻松实现此目的

if (bar.Value = 100)
{
  f1.label1.Text = ""+(int.Parse(f1.label1.Text)+1); 
}

or even a better way to initialize the string if for some reason the Text of the label is not an integer 甚至是一种更好的初始化字符串的方法,如果由于某种原因,标签的文本不是整数

if (bar.Value == 100)
     {
       int value;
        if(!int.TryParse(f1.label1.Text,out value))
         {
           f1.label1.Text = "1";
         }
        else
        {
             f1.label1.Text = ""+(value+1); 
        }
     }

but the best way to do this is to keep track of the value in a separate variable and just update the content of the label. 但最好的方法是在单独的变量中跟踪值,然后仅更新标签的内容。

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

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