Use winform to implement automatic roll call function

Article directory


Preface

Hello everyone! I believe you have all seen the lottery system in your daily life or the function where the teacher uses the computer to call and ask questions during class. It is also a good example for those who are new to winform. Today I will share with you the roll call system I made.

1. Drag controls

The controls needed here are: TextBox, Button and a timer control.

 This is how I placed it, and you can also place it according to your own design.

2. Usage steps

1.timer code

code show as below:

Here we need to generate a random number and an array to store names (note: the length of the array is determined by how many names there are, and the random number changes with the length of the array)

private void timer1_Tick(object sender, EventArgs e)
        {
            string[] names = { "小明", "小强", "张三", "李四", "王五" };
            Random random = new Random();
            int i = random.Next(0, 5);
            this.textBox1.Text = names[i];
        }

Similarly, the same code must be written in the Button click event 

private void button1_Click(object sender, EventArgs e)
        {
            string[] names = { "小明", "小强", "张三", "李四", "王五" };
            Random random = new Random();
            int i = random.Next(0, 5);
            this.textBox1.Text = names[i];
        }

2. Improve the code and implement functions

Only half of the above code has been written. There is still one function that needs to be implemented, that is, how to pause the text in the text box when the button is clicked and the text of the button also changes to Stop Lottery. When the button is clicked again, the text box starts to change and the text of the button Change back to start the lottery.

code show as below:

        int count = 1;
        private void button1_Click(object sender, EventArgs e)
        {
            count++;
            if (count % 2 == 0)
            {
                timer1.Enabled = true;
                timer1.Start();
                button1.Text = "停止抽奖";
            }
            else
            {
                timer1.Enabled = false;
                timer1.Stop();
                button1.Text = "开始抽奖";
            }
            string[] names = { "小明", "小强","张三","李四","王五" };
            Random random = new Random();
            int i = random.Next(0,5);
            this.textBox1.Text = names[i];
           
        }

What should be noted here is to declare an int variable outside to record the number of times the button is clicked, with an initial value of 1. The variable count will be incremented every time the button is clicked, thus realizing the roll call function.

 
 

Summarize


The above is the content shared today. This article only briefly introduces the implementation of the roll call function, and the convenience and some fun that the roll call function brings to us. thanks for watching!

Guess you like

Origin blog.csdn.net/m0_66403070/article/details/126828948