c#使用forms实现helloworld和login登录

说明:

c#使用windows forms app实现helloworld和login登录

效果图:

step1:登录 C:\Users\wangrusheng\RiderProjects\WinFormsApp1\WinFormsApp1\Form1.cs

csharp 复制代码
 namespace WinFormsApp1;

public partial class Form1 : Form
{
    private TextBox txtUsername;
    private TextBox txtPassword;
    private Label lblError;

    public Form1()
    {
        InitializeComponent();
        CreateLoginControls();
    }

    private void CreateLoginControls()
    {
        // 窗口配置
        this.Text = "用户登录";
        this.ClientSize = new Size(800, 600);
        this.StartPosition = FormStartPosition.CenterScreen; // 屏幕居中
        
        // 使用Panel容器统一管理布局
        Panel mainPanel = new Panel();
        mainPanel.Size = new Size(400, 300);
        mainPanel.Location = new Point(
            (this.ClientSize.Width - mainPanel.Width) / 2,
            (this.ClientSize.Height - mainPanel.Height) / 2
        );

        // 用户名标签
        Label lblUser = new Label();
        lblUser.Text = "用户名:";
        lblUser.Font = new Font("微软雅黑", 12F);
        lblUser.Location = new Point(50, 40);
        lblUser.AutoSize = true;

        // 用户名输入框(加宽)
        txtUsername = new TextBox();
        txtUsername.Font = new Font("微软雅黑", 12F);
        txtUsername.Location = new Point(50, 70);
        txtUsername.Size = new Size(300, 30); // 宽度300

        // 密码标签
        Label lblPass = new Label();
        lblPass.Text = "密 码:";
        lblPass.Font = new Font("微软雅黑", 12F);
        lblPass.Location = new Point(50, 130);
        lblPass.AutoSize = true;

        // 密码输入框(与用户名对齐)
        txtPassword = new TextBox();
        txtPassword.Font = new Font("微软雅黑", 12F);
        txtPassword.Location = new Point(50, 160);
        txtPassword.Size = new Size(300, 30);
        txtPassword.PasswordChar = '●';

        // 登录按钮(加大尺寸)
        Button btnLogin = new Button();
        btnLogin.Text = "登  录";
        btnLogin.Font = new Font("微软雅黑", 12F, FontStyle.Bold);
        btnLogin.Size = new Size(300, 40);
        btnLogin.Location = new Point(50, 210);
        btnLogin.BackColor = Color.DodgerBlue;
        btnLogin.ForeColor = Color.White;
        btnLogin.Click += BtnLogin_Click;

        // 错误提示(居中显示)
        lblError = new Label();
        lblError.ForeColor = Color.Red;
        lblError.Font = new Font("微软雅黑", 10F);
        lblError.AutoSize = true;
        lblError.Location = new Point(
            (mainPanel.Width - lblError.Width) / 2,
            260
        );
        lblError.Visible = false;

        // 添加控件到Panel
        mainPanel.Controls.AddRange(new Control[] {
            lblUser, txtUsername,
            lblPass, txtPassword,
            btnLogin, lblError
        });

        // 添加Panel到窗体
        this.Controls.Add(mainPanel);
    }

   
	
	    private void BtnLogin_Click(object sender, EventArgs e)
    {
	  // 错误提示自动居中
        lblError.Location = new Point(
            (this.Controls[0].Width - lblError.Width) / 2,
            lblError.Location.Y
        );
        // 简单验证逻辑
        if (string.IsNullOrEmpty(txtUsername.Text) || 
            string.IsNullOrEmpty(txtPassword.Text))
        {
            lblError.Text = "用户名和密码不能为空!";
            lblError.Visible = true;
            return;
        }

        // 模拟验证(实际应连接数据库)
        if (txtUsername.Text == "admin" && txtPassword.Text == "123456")
        {
            lblError.Visible = false;
            MessageBox.Show("登录成功!");
            // 此处可跳转到主窗体:new MainForm().Show();
        }
        else
        {
            lblError.Text = "用户名或密码错误!";
            lblError.Visible = true;
        }
    }
	
}

step2:helloworld

csharp 复制代码
namespace WinFormsApp1;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    
        // 添加Label控件
        Label label = new Label();
        label.Text = "Hello World!";
        label.AutoSize = true; // 自动调整标签大小
        label.Location = new System.Drawing.Point(20, 20); // 设置位置
        this.Controls.Add(label); // 将标签添加到窗体
    }
}

end

相关推荐
luj_17684 小时前
残熵算法实时化三大瓶颈突破
c语言·开发语言·网络·经验分享·算法
不听话坏5 小时前
Ignition篇(下 一) 动态执行前的事情
开发语言·前端·javascript
likeyi075 小时前
require 和 import的区别
开发语言·前端
远离UE45 小时前
UE5 compute shader 原子加
开发语言·c++·ue5
C+-C资深大佬5 小时前
C++ 显式类型转换详解:static_cast、dynamic_cast、const_cast、reinterpret_cast
开发语言·c++
KaMeidebaby6 小时前
卡梅德生物技术快报|抗体亲和力成熟工业化调控新机制:差异性浆细胞增殖工艺优化思路
java·开发语言·人工智能·算法·机器学习·架构·spark
luj_17687 小时前
心形曲线轨迹控制三大关键技术
c语言·开发语言·c++·经验分享·算法
Mininglamp_27187 小时前
Claude Code 封禁中国开发者之后:本地 AI 编程工具的替代方案实测
开发语言·人工智能·windows·开源软件·ai-native
思麟呀7 小时前
C++17(三)if constexpr+折叠表达式
开发语言·c++
醉城夜风~8 小时前
Java详解经典算法题:接雨水(三种实现方案+原理剖析)
java·开发语言·算法