在按钮 Click (C#) 上创建一个带有随机数的数组

Posted

技术标签:

【中文标题】在按钮 Click (C#) 上创建一个带有随机数的数组【英文标题】:Creating an array with random numbers on button Click (C#) 【发布时间】:2020-01-16 11:07:06 【问题描述】:

啊..我是初学者。我想不出一种方法可以在单击按钮时生成随机数数组。再次单击应该会创建一个新数组,依此类推。

喜欢.. 第一次点击 TheNumbers = new int [3] 1, 2, 3;

第二次点击 TheNumbers2 = new int [3] 4, 5, 7;

等等。

【问题讨论】:

查看网上的文章,例如:c-sharpcorner.com/article/… 将其分解为更小的任务。例如,您可以搜索“生成随机数 c#”之类的内容,然后搜索“初始化数组 c#”,然后搜索“c# 按钮单击”。在搜索与 UI 相关的任何内容时,您还应该包括您正在使用的 UI 框架,而不仅仅是 c#。这可能是 WPF、asp.net (razor/blazor)、windows forms,.. 是的.. 措辞不好,抱歉。使用统一。我可以使用 Random.Range (x, x); 生成随机数;了解 UI 按钮的工作原理并可以制作简单的数组。问题是让它动态化,您可以使用简单的按钮添加一个或多个数组。 【参考方案1】:

此代码应该适合您。我假设你正在使用WinForms

阅读Random Constructors

Form1.cs

using System;
using System.Windows.Forms;

namespace WindowsFormsApp1

    public partial class Form1 : Form
    
        //https://docs.microsoft.com/en-us/dotnet/api/system.random.-ctor?view=netframework-4.8
        // The default seed value is derived from the system clock, which has finite resolution. 
        // As a result, on the .NET Framework only, different Random objects that are created in 
        // close succession by a call to the parameterless constructor will have identical default 
        // seed values and, therefore, will produce identical sets of random numbers. This problem 
        // can be avoided by using a single Random object to generate all random numbers.
        private readonly Random objRandom = new Random();

        //Everytime the button is clicked, this array is loaded with 3 random numbers
        private int[] theNumbers;

        public Form1()
        
            InitializeComponent();
        

        private void Button1_Click(object sender, EventArgs e)
        
            theNumbers = GenerateThreeRandomsEachLessThanTen();

            //just to demo the creation of the numbers
            textBox1.Text = String.Join(Environment.NewLine, theNumbers);
        

        private int[] GenerateThreeRandomsEachLessThanTen()
        
            return new int[]  objRandom.Next(10), objRandom.Next(10), objRandom.Next(10) ;
        
    

Form1.Designer.cs

namespace WindowsFormsApp1

    partial class Form1
    
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        
            if (disposing && (components != null))
            
                components.Dispose();
            
            base.Dispose(disposing);
        

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(86, 24);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "Go";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.Button1_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(19, 63);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(203, 91);
            this.textBox1.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(234, 179);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Name = "Form1";
            this.Text = "Randoms";
            this.ResumeLayout(false);
            this.PerformLayout();

        

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;
    

点击“开始”按钮 3 次的输出

【讨论】:

以上是关于在按钮 Click (C#) 上创建一个带有随机数的数组的主要内容,如果未能解决你的问题,请参考以下文章

c#如何完全禁用用户按钮

C#动态创建两个按钮,btn2复制btn1的Click事件,匿名委托

C#中怎样用代码实现按钮的click事件!最好能举个例子说明下!

如何将ContentView添加到Click事件?

标签从数据库c#中放置随机值

如何将 ContentView 添加到 Click 事件?