【问题标题】:Create a button for multiple events on every single click每次单击时为多个事件创建一个按钮
【发布时间】:2017-11-05 04:18:20
【问题描述】:

我想要一个名为forward 的按钮,它可以帮助程序在下次单击时将字符串OLabel1 转换为Label2Label2 转换为Label3,依此类推。

【问题讨论】:

  • 您不必使用多个事件。您可以在单个事件本身中完成它

标签: c# winforms visual-studio-2015


【解决方案1】:

以下是前进按钮的示例代码,您可以自己后退。希望这可以帮到你。

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 = "";
        }
    }
}

【讨论】:

  • 我还发现你有开始和停止按钮,所以如果你想让字符串在这些标签之间缓慢移动,那么你需要处理 timer_Tick() 函数来计算速度和位置一个字符串。
  • @McMillan Cheng 非常感谢,你的回答中了靶心。
猜你喜欢
  • 2014-01-26
  • 2013-08-22
  • 1970-01-01
  • 1970-01-01
  • 2018-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多