流水灯程序与计算输入校验(C#)

本文探讨了在代码中实现高效字符串操作的方法,并通过实例展示了性能测试的重要性与实践步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace frmexe01
{
    public partial class frmExe03 : Form
    {
        public frmExe03()
        {
            
            InitializeComponent();

        }

        private void frmExe03_Load(object sender, EventArgs e)
        {
            txtText.Text = "我爱北京***,***上太阳升.";
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < txtText.Text.Length; i++)
            {
                txtText.Text = txtText.Text.Substring(1) + txtText.Text.Substring(0, 1);
                txtText.Update();
                //for(int j = 0;j <10000000; j++)
                //{

                //}
                System.Threading.Thread.Sleep(500);
            }
               
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for(int i = 1;i <= 1000; i++)
            {
                textBox1.Text = i.ToString();
                //textBox1.Update();
            }
            watch.Stop();
            MessageBox.Show(string.Format("共用时间:{0}", watch.Elapsed.Milliseconds.ToString()));
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
           

            for (int i = 0; i < txtText.Text.Length; i++)
            {
                txtText.Text = txtText.Text.Substring(txtText.Text.Length - 1) +
               txtText.Text.Substring(0, txtText.Text.Length - 1);
                txtText.Update();
                //for(int j = 0;j <10000000; j++)
                //{

                //}
                System.Threading.Thread.Sleep(500);
            }
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace frmexe01
{
    public partial class frmExe02 : Form
    {
        public frmExe02()
        {
            InitializeComponent();
        }

        private void btnCompute_Click(object sender, EventArgs e)
        {
            int start,end;

            if(!int.TryParse(txtStart.Text, out start))
            {
                MessageBox.Show("起始值输入有误,请重新输入!");
                
                txtStart.Focus();
                txtStart.SelectAll();
                return;
            }

            if (!int.TryParse(txtEnd.Text, out end))
            {
                MessageBox.Show("终止值输入有误,请重新输入!");
               
                txtEnd.Focus();
                txtEnd.SelectAll();
                return;
            }

            if(start>end)
            {
                MessageBox.Show("起始值必须小于终止值!请重新输入!");
                
                txtStart.Focus();
                txtStart.SelectAll();
                return;
            }

            int sum = 0;
            for(int i = start; i <= end; i++)
            {
                sum = sum + i;
            }
            txtResult.Text = sum.ToString();
        }

        private void txtStart_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtStart_KeyPress(object sender, KeyPressEventArgs e)
        {

            TextBox texBox = sender as TextBox;//注意触发事件

           // if(e.KeyChar < 48 || e.KeyChar > 57)
            if(e.KeyChar < '0'|| e.KeyChar > '9')
            {
                e.Handled = true;
            }

            //MessageBox.Show(txtStart.SelectionStart.ToString());
            if(texBox.SelectionStart == 0 && e.KeyChar == '0')
            {
                //SelectionStart光标所处位置
                e.Handled = true;
            }

            if(e.KeyChar == 8)
            {
                e.Handled = false;
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值