Making Button be assigned to make only one random label visible

时间:2021-03-31 19:31:57

I have created an array of labels to be visible with a buttons click but yet since I have many buttons I want to assign a button to make only one label visible I am having trouble with making a button making more than one label visible

我创建了一个标签数组,点击按钮可见,但由于我有很多按钮,我想分配一个按钮,只能看到一个标签我制作一个按钮使多个标签可见

This is the code I used :

这是我使用的代码:

var labels = Controls.OfType<Label>().ToArray();
//And then randomly make on of them visible.
var random = new Random();
var label = labels[random.Next(0, labels.Count - 1)];
label.Visible = true;

1 个解决方案

#1


0  

In Winforms you can simply declare a private Random variable, and then in the Click event of one of your buttons, you can choose a random number that's within the valid index range of the label array, something like:

在Winforms中,您可以简单地声明一个私有的随机变量,然后在其中一个按钮的Click事件中,您可以选择一个在标签数组的有效索引范围内的随机数,例如:

private Label[] labels = new Label[10];  // Presumably this array is filled somewhere
private Random rnd = new Random();

private void Form1_Load(object sender, EventArgs e)
{
    for(int i = 0; i < labels.Length; i++)
    {
        labels[i] = new Label
        {
            Height = 20,
            Left = 10,
            Name = $"Label{i}",
            Tag = i,
            Text = $"Label{i}",
            Top = 10 + 20 * i,
            Visible = false
        };

        this.Controls.Add(labels[i]);
    }
}

private void button1_Click(object sender, EventArgs e)
{
    if (labels != null && labels.Length > 0)
    {
        // If needed, this will hide any currently visible labels in the array
        foreach(var label in labels.Where(label => label != null && label.Visible))
        {
            label.Visible = false;
        }

        // Pick a random label and make it visible
        labels[rnd.Next(0, labels.Length)].Visible = true;
    }
}

#1


0  

In Winforms you can simply declare a private Random variable, and then in the Click event of one of your buttons, you can choose a random number that's within the valid index range of the label array, something like:

在Winforms中,您可以简单地声明一个私有的随机变量,然后在其中一个按钮的Click事件中,您可以选择一个在标签数组的有效索引范围内的随机数,例如:

private Label[] labels = new Label[10];  // Presumably this array is filled somewhere
private Random rnd = new Random();

private void Form1_Load(object sender, EventArgs e)
{
    for(int i = 0; i < labels.Length; i++)
    {
        labels[i] = new Label
        {
            Height = 20,
            Left = 10,
            Name = $"Label{i}",
            Tag = i,
            Text = $"Label{i}",
            Top = 10 + 20 * i,
            Visible = false
        };

        this.Controls.Add(labels[i]);
    }
}

private void button1_Click(object sender, EventArgs e)
{
    if (labels != null && labels.Length > 0)
    {
        // If needed, this will hide any currently visible labels in the array
        foreach(var label in labels.Where(label => label != null && label.Visible))
        {
            label.Visible = false;
        }

        // Pick a random label and make it visible
        labels[rnd.Next(0, labels.Length)].Visible = true;
    }
}