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

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日

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置