Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在每次单击时为多个事件创建一个按钮_C#_Winforms_Visual Studio 2015 - Fatal编程技术网

C# 在每次单击时为多个事件创建一个按钮

C# 在每次单击时为多个事件创建一个按钮,c#,winforms,visual-studio-2015,C#,Winforms,Visual Studio 2015,我想要一个名为forward的按钮,它可以帮助程序在下一次单击时将字符串O从Label1移到Label2,并将Label2移到Label3,依此类推 下面是“前进”按钮的示例代码,您可以自己执行“后退”操作。希望这能帮助你 using System; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { enum stringLoc

我想要一个名为forward的按钮,它可以帮助程序在下一次单击时将字符串O从Label1移到Label2,并将Label2移到Label3,依此类推


下面是“前进”按钮的示例代码,您可以自己执行“后退”操作。希望这能帮助你

using System;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        enum stringLocation : int
        {
            label1,label2, label3, label4
        }
        int location =(int) stringLocation.label1;
        string contentToMove= "0";
        public Form1()
        {
            InitializeComponent();
        }

        private void forward_Click(object sender, EventArgs e)
        {
            location = (location+1) % 4;
            switch (location){
            case (int) stringLocation.label1:
                    label1.Text = contentToMove;
                    label2.Text = "";
                    label3.Text = "";
                    label4.Text = "";
                    break;
            case (int) stringLocation.label2:
                    label1.Text = "";
                    label2.Text = contentToMove;
                    label3.Text = "";
                    label4.Text = "";
                    break;
            case (int) stringLocation.label3:
                    label1.Text =  "";
                    label2.Text = "";
                    label3.Text = contentToMove;
                    label4.Text = "";
                    break;
            case (int)stringLocation.label4:
                    label1.Text = "";
                    label2.Text = "";
                    label3.Text = "";
                    label4.Text = contentToMove; 
                    break;
            default:
                break;
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = contentToMove;
            label2.Text = "";
            label3.Text = "";
            label4.Text = "";
        }
    }
}

您不必使用多个事件。我还发现你有开始和停止按钮,因此,如果你想让字符串在这些标签之间缓慢移动,那么你需要使用定时器滴答函数来计算字符串的速度和位置。@McMillan Cheng非常感谢,你的回答很中肯。