简体   繁体   English

将数组的值传递给按钮C#WinForm

[英]Passing a value of an array to button C# WinForm

I know about arrays. 我知道数组。 But I only know it using console, now I don't know how to implement it in windows form using visual studio, and I cannot saw anything online about what I wan't to do. 但是我只使用控制台知道它,现在我不知道如何使用Visual Studio在Windows窗体中实现它,而且我看不到任何关于我不愿做的事情的信息。 Let's say for example I initialise an array of int: 举例来说,我初始化了一个int数组:

int[] n = new int[5] {1, 2, 3, 4, 5};

Now what I want is, for example, there are 5 buttons, let's say when I click on button1, it will output: 现在我想要的是,例如,有5个按钮,假设当我单击button1时,它将输出:

MessageBox.Show("Number " + ... // random value in an array n

The same will happen to other 5 buttons but with no repetition. 其他5个按钮也一样,但是没有重复。 I know my question can be flagged as inappropriate but, I am asking this question since yesterday, an took a research the whole night but still nothing. 我知道我的问题可以被标记为不合适,但是,自昨天以来,我一直在问这个问题,整个晚上都进行了研究,但仍然一无所获。

So you need to grab a random value from the array, then remove that value so that the other buttons can not show the same number. 因此,您需要从数组中获取一个随机值,然后删除该值,以使其他按钮不能显示相同的数字。 A List<int> would be easier to work with. List<int>将更易于使用。

private void formLoad(object sender, EventArgs e) {  
    // wire this handler up to your Form.Load event

    List<int> numbers = new List<int>() {101,22,373,414,625};
    Random random = new Random();

    Button1.Click += clickHandler;
    Button2.Click += clickHandler;
    Button3.Click += clickHandler;
    Button4.Click += clickHandler;
    Button5.Click += clickHandler;
}

private void clickHandler(object sender, EventArgs e) {
    if (0 == numbers.Count) { 
        MessageBox.Show("There are no more numbers in the list.");
        return;
    }
    int index = random.Next(0, numbers.Count);
    MessageBox.Show("Number " + numbers[index].ToString());
    numbers.RemoveAt(index);
}

I would like to suggest Sam Axe's solution. 我想提出山姆·阿克斯的解决方案。 it is very simple and easy to understand. 它非常简单易懂。 The only reason to provide this solution is if you want to maintain the selected item history then this solution might be helpful for you. 提供此解决方案的唯一原因是,如果您想维护选定的项目历史记录,则此解决方案可能对您有所帮助。

You can stored the used value in a list which can be checked while next random number is being fetched. 您可以将使用的值存储在一个列表中,在获取下一个随机数时可以检查该列表。

To get the random integer number. 获取随机整数。 You can use Random.Next(int32, int32) method of Random class. 您可以使用Random类的Random.Next(int32,int32)方法。

int[] n = new int[5] {1, 2, 3, 4, 5};
List<int> usedN = new List<int>();
Random rnd = new Random();

private void button1_click(object sender, EventArgs e)
{        
    int iIndex = rnd.Next(0, n.Length-1);
    int iRnd = n[iIndex];

    if (usedN.Count == n.Length)
        return;

    usedN.Add(iRnd);
    MessageBox.Show("Number " + iRnd.ToString());
}

This answer has similarities to Sam Axe's answer, but was created independently. 该答案与Sam Axe的答案相似,但是是独立创建的。 It may help someone. 它可以帮助某人。 Code is available on https://github.com/zedfoxus/StackOverflow29203245 可以在https://github.com/zedfoxus/StackOverflow29203245上找到代码

The form has 5 buttons and an extra button to reset the numbers after they are all used up. 表单上有5个按钮和一个额外的按钮,可在数字用完后重置数字。

Form1.cs Form1.cs的

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace StackOverflow29203245
{
    public partial class Form1 : Form
    {
        private readonly List<int> _numbers  = new List<int> {1, 2, 3, 4, 5};
        private readonly List<int> _asked = new List<int>();

        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Select random integer from the list of integers. If no numbers are left, throw an exception
        /// </summary>
        /// <returns>Random integer from the list</returns>
        private int RandomInteger()
        {
            if (_numbers.Count == 0)
            {
                throw new Exception("All numbers have been selected");
            }

            var random = new Random();
            var index = random.Next(_numbers.Count);
            var selectedNumber = _numbers[index];
            _asked.Add(selectedNumber);
            _numbers.Remove(selectedNumber);
            return selectedNumber;
        }


        /// <summary>
        /// Show the message box. If all numbers are selected, show appropriate message
        /// </summary>
        private void ShowMessage(object sender, EventArgs e)
        {
            try
            {
                MessageBox.Show(@"Number " + RandomInteger());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        /// <summary>
        /// Reset numbers
        /// </summary>
        private void button6_Click(object sender, EventArgs e)
        {
            _numbers.AddRange(_asked);
            _asked.Clear();
        }
    }
}

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

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