搜索页 2022-02-28 09:37 采纳率: 61.5%
浏览 36
已结题

C# Label控件问题

问题描述:label控件更新为下一次更大的数字,1到2,2到3,→,逐渐循环。18为最大,到了18再回到1。

img

  • 写回答

3条回答 默认 最新

  • 码老头 2022-02-28 10:17
    关注

    我更愿意以码为例来说明,先看效果:

    img

    示例代码如下:

    using System;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1.Forms.Demo2
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            // 声明一个当前索引的字段
            private int _currentIndex = 1;
            // 递增的最大值
            private const int MaxIndex = 18;
            private void Form2_Load(object sender, EventArgs e)
            {
    
            }
    
            /// <summary>
            /// 监听文本框的KeyPress事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                // 当前键盘输入为回车时
                if (e.KeyChar == 13)
                {
                    // 当前索引值加1
                    _currentIndex++;
                    // 当当前索引值大于最大值是,重置为1
                    if (_currentIndex > MaxIndex)
                    {
                        _currentIndex = 1;
                    }
    
                    label1.Text = $"当前索引值:{_currentIndex}";
                }
            }
        }
    }
    

    说明都在注释里,另外,请自行处理你的逻辑。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月8日
  • 已采纳回答 2月28日
  • 请提交代码 2月28日
  • 创建了问题 2月28日