如何制作快速程序,通过C#Windows窗体应用程序中的列表? [英] How do I make a quick program which goes through a list in a C# Windows Form Application?

查看:113
本文介绍了如何制作快速程序,通过C#Windows窗体应用程序中的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验C#控制台应用程序中的列表,特别是随机化的int列表,其数字顺序是随机的。在这个实验中,当我按下Enter键并且显示它将停止的所有随机值时,我想要查看列表中的随机值。它按照我的意图工作: http://i.imgur.com/bNOYrZp.png [ ^ ]



< pre lang =c#>随机r = new Random();

int tempValue;

List< int> number = new List< int>();
number.Add( 1 );
number.Add( 2 );
number.Add( 3 );
number.Add( 4 );
number.Add( 5 );
number.Add( 6 );
number.Add( 7 );
number.Add( 8 );
number.Add( 9 );
number.Add( 10 );

for int i = 0 ; i < 10 ; i ++)
{
tempValue = r.Next( 0 ,number.Count);
Console.WriteLine(number [tempValue]);
number.RemoveAt(tempValue);
Console.ReadLine();
}





现在我如何在C#的Windows窗体应用程序中执行类似的操作?我没有按回车来查看列表,而是按下按钮浏览列表,每次按下此按钮时,值的顺序都会显示在标签上。



我使用了类似的代码,但它没有按预期工作。它不是经过随机值,而是每次点击按钮时都会保持新的值顺序。我想要它做的是通过随机值,并在显示所有10个随机值后,没有重复,它停止。



  private   void  button1_Click( object  sender,EventArgs e)
{
List< int> number = new List< int>();
随机r = Random();

int tempValue;

number.Add( 1 );
number.Add( 2 );
number.Add( 3 );
number.Add( 4 );
number.Add( 5 );
number.Add( 6 );
number.Add( 7 );
number.Add( 8 );
number.Add( 9 );
number.Add( 10 );

for int i = 0 ; i < 10 ; i ++)
{
tempValue = r.Next( 0 ,number.Count);
label1.Text = number [tempValue] .ToString();
number.Remove(number [tempValue]);

}

}

解决方案

这个问题与一般用户界面,特别是 System.Windows.Forms ,但你没有做任何合理的事情来显示结果,甚至远程。你怎么能期望像标签这样的控件可以充当控制台呢?您只需将值分配给循环中的某个属性( Text ,int this case),但为什么?在每次迭代中,属性的旧值被丢弃,并将其替换为另一个值。很难想象你怎么会遇到这样一个奇怪的想法。



但问题不简单。想一想:如果你已经在使用某些列表,那么使用一些与列表相关的控件来展示它是不是很自然?您可以使用 System.Windows.Forms.ListBox ,这是最简单的事情:

https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox %28v = vs.110%29.aspx



另外,由于某些原因只与某些图形渲染细微之处有关,我经常使用 TreeView 相反,只有一级层次结构,没有任何子节点。



通过这种方式,你可以添加( Items.Add )新项目:

https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.items%28v=vs。 110%29.aspx

https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.ob jectcollection%28v = vs.110%29.aspx



此外,您可能希望绑定某个列表到列表框或其他控件:

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.bindingcontext(v = vs.110).aspx

https://msdn.microsoft .com / zh-cn / library / system.windows.forms.control.databindings(v = vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.windows .forms.control.resetbindings(v = vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.windows。 forms.listcontrol.onbindingcontextchanged(v = vs.110).aspx



-SA


I was experimenting with lists in C#'s console application, specifically a randomized int list which had its number order randomized. In this experiment I wanted to go through the randomized values from the list when I pressed enter and when it had shown all the randomized values it would stop. And it worked just as I intended: http://i.imgur.com/bNOYrZp.png[^]

Random r = new Random();
           
            int tempValue;

            List<int> number = new List<int>();
            number.Add(1);
            number.Add(2);
            number.Add(3);
            number.Add(4);
            number.Add(5);
            number.Add(6);
            number.Add(7);
            number.Add(8);
            number.Add(9);
            number.Add(10);

            for (int i = 0; i < 10; i++)
            {
                tempValue = r.Next(0, number.Count);
                Console.WriteLine(number[tempValue]);               
                number.RemoveAt(tempValue);
                Console.ReadLine();
            }



Now how do I do a similar thing in C#'s Windows Form Application? Instead of pressing enter to go through the list, I press a button to go through the list, and the order of the values are displayed on a label every time I press this button.

I used a similar code, but it did not work as intended. Instead of going through the randomized values it kept making a new order of values which it kept doing every time I clicked the button. What I want it to do is to go through the randomized values and after it has showed all the 10 randomized values, without duplicates, it stops.

private void button1_Click(object sender, EventArgs e)
        {
            List<int> number = new List<int>();
            Random r = new Random();

            int tempValue;

            number.Add(1);
            number.Add(2);
            number.Add(3);
            number.Add(4);
            number.Add(5);
            number.Add(6);
            number.Add(7);
            number.Add(8);
            number.Add(9);
            number.Add(10);

            for (int i = 0; i < 10; i++)
            {
                tempValue = r.Next(0, number.Count);
                label1.Text = number[tempValue].ToString();
                number.Remove(number[tempValue]);
                
            }
            
        }

解决方案

This problem has little to do with the UI in general and System.Windows.Forms in particular, but you haven't done anything reasonable to show the results, not even remotely. How could you expect that some control like a label would act as a console? You simply assign the value to some property (Text, int this case) in the cycle, but why? In each iteration, old value of the property gets discarded, and you replace it with another value. It's hard to imagine how could you even come to such a weird idea.

But isn't the problem simple. Just to think about it: if you are already using some list, wouldn't it be natural to use some list-related control to present things? You could use System.Windows.Forms.ListBox, as a simplest thing:
https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox%28v=vs.110%29.aspx.

Alternatively, by some reasons related exclusively to some graphic rendering subtleties, I often use TreeView instead, with just one level of hierarchy, without any child nodes.

In this way, you can just add (Items.Add) new items:
https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.items%28v=vs.110%29.aspx,
https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.objectcollection%28v=vs.110%29.aspx.

Also, you may want to bind some list to a list box or other control:
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.bindingcontext(v=vs.110).aspx,
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.databindings(v=vs.110).aspx,
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.resetbindings(v=vs.110).aspx,
https://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.onbindingcontextchanged(v=vs.110).aspx.

—SA


这篇关于如何制作快速程序,通过C#Windows窗体应用程序中的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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