C# winform实现一个超简单的登陆界面跳转及其进度条

这是一个基于C# winform窗体应用的登陆界面,包含账号密码的输入,输入错误次数的检验,以及超过三次输入错误后程序自动退出,登录跳转页面的实现,账号密码正确后的进度条登录效果
(ps:做项目的时候正好碰到了,顺手记录一下)

最终效果:

***过程***
1、登陆界面的设计,这部分比较简单,我就不再详细说明了,大概设计成下图的这个效果就可以了(所需:textbox*2,label*2,button*1,picturebox*1,panel*2,timer*1)

注意:别忘了放上timer控件!

2、再新建一个主界面用于演示跳转界面的效果(Main.cs):

**ps:这里放上一个label用来代替主界面的实际内容

****需要修改参数的控件(没提到的就不用改):

*panel:

改改两个panel的颜色就行

e.g:

*密码部分的textbox:

*picturebox:

2、代码部分:

(注意:我这里修改了一些控件的名字,在使用代码的时候,应当替换成你自己的实际控件的名字)

(1)页面跳转效果:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Digital_tanks
{
    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new login());
            login login = new login();
            if (login.ShowDialog() == DialogResult.OK) 
                Application.Run(new Main());
            else
                Application.Exit();

        }
    }
}

(2)登录界面的代码:

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 Digital_tanks
{
    public partial class login : Form
    {
        private bool buttonClicked = false;//初始化登录按钮状态
        private int errorCount = 0;//初始化错误次数

        public login()
        {
            InitializeComponent();
            textBoxPassword.KeyDown += TextBoxPassword_KeyDown; // 绑定KeyDown事件处理程序
        }

        private void TextBoxPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) // 判断按下的键是否为回车键
            {
                button1_Click(sender, e); // 调用button1_Click方法
            }
        }

        private void timerProgressbar_Tick(object sender, EventArgs e)
        {
            if (buttonClicked)
            {
                panelProgressbar.Width += 5;//每次计时溢出,进度条前进五

                if (panelProgressbar.Width > 980)
                {
                    timerProgressbar.Stop();//计时器停止
                    this.DialogResult = DialogResult.OK;//进度条跑到头时,对话框设为OK状态
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String st1 = textBoxUsername.Text.Trim();
            String st2 = textBoxPassword.Text.Trim();
            if (st1 == "admin" && st2 == "123456789")
            {
                MessageBox.Show("账户、密码正确,欢迎!");
                buttonClicked = true;
            }
            else
            {
                errorCount++;
                if (errorCount >= 3)
                {
                    MessageBox.Show("错误次数超过三次,程序将退出!");
                    Application.Exit();
                }
                else
                {
                    MessageBox.Show("账户或密码错误,请重新输入!\n错误次数:" + errorCount);
                    textBoxUsername.Text = "";
                    textBoxPassword.Text = "";
                }
            }
        }
    }
}

ps:整篇文章写的比较赶,有什么错漏的地方,还请各位指出,方便我进行后续修改

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
平面调车系统是指对于一片平面上的车辆,通过调节其方向和速度,使其到达指定目的地的系统。下面是一个简单的使用C# Winform实现平面调车系统的示例: 1.创建一个Winform应用程序,并添加一个PictureBox控件作为车辆。 2.添加两个文本框用于输入目的地的坐标。 3.添加两个按钮,一个用于启动调车系统,另一个用于停止调车系统。 4.在启动调车系统按钮的Click事件中,获取目的地坐标,并计算出车辆需要移动的距离和角度。 ```csharp private double destX, destY; private double deltaX, deltaY; private double distance, angle; private void btnStart_Click(object sender, EventArgs e) { destX = double.Parse(txtDestX.Text); destY = double.Parse(txtDestY.Text); deltaX = destX - picCar.Left; deltaY = destY - picCar.Top; distance = Math.Sqrt(deltaX * deltaX + deltaY * deltaY); angle = Math.Atan2(deltaY, deltaX) * 180 / Math.PI; timer1.Start(); } ``` 5.在停止调车系统按钮的Click事件中,停止计时器。 ```csharp private void btnStop_Click(object sender, EventArgs e) { timer1.Stop(); } ``` 6.在计时器的Tick事件中,根据车辆当前的位置和目的地的位置,计算出车辆需要移动的距离和角度,并将车辆移动到新的位置。 ```csharp private void timer1_Tick(object sender, EventArgs e) { double currX = picCar.Left; double currY = picCar.Top; double currAngle = picCar.Rotation; double radians = angle * Math.PI / 180.0; double deltaX = Math.Cos(radians) * distance / 10.0; double deltaY = Math.Sin(radians) * distance / 10.0; double newAngle = Math.Atan2(deltaY, deltaX) * 180 / Math.PI; double deltaAngle = newAngle - currAngle; if (deltaAngle > 180) deltaAngle -= 360; else if (deltaAngle < -180) deltaAngle += 360; picCar.Rotation += deltaAngle; picCar.Left += (int)deltaX; picCar.Top += (int)deltaY; distance = Math.Sqrt((destX - picCar.Left) * (destX - picCar.Left) + (destY - picCar.Top) * (destY - picCar.Top)); if (distance < 5) timer1.Stop(); } ``` 在上述代码中,根据车辆当前的位置和目的地的位置,计算出车辆需要移动的距离和角度,然后根据车辆当前的角度和需要旋转的角度,计算出车辆需要旋转的角度,并将车辆移动到新的位置。在车辆到达目的地后,停止计时器。 通过以上步骤,就可以实现一个简单的平面调车系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值