[可视化编程]窗体设计及其应用

Posted Spring-_-Bear

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[可视化编程]窗体设计及其应用相关的知识,希望对你有一定的参考价值。

实验目的

掌握 C# 的 MDI 窗体设计,一个主窗体、多个子窗体间的切换。掌握窗体中基本的控件设计方法,label、textbox、groupbox、radiobutton、comboBox、button 等。掌握 Messagebox 的使用方法

实验要求

登录窗口

(1)设计 MDI 窗体,一个主窗体,登录子窗体输入用户名和密码(系统设置默认用户登录信息:用户名:whut;密码:1234),如果用户名密码正确,消息框提示登录成功。如果用户名不正确,提示需先注册。如果密码错误,提示密码有误,重新输入

注册窗口

(2)注册子窗体设计,包括用户名、密码、确认密码(TextBox)、性别(GroupBox 和 RadioButton)、专业(ComboBox)等信息。点击 “确认” 按钮,如果用户名不为空,密码与确认密码一致,提示注册成功,并显示注册信息。否则提示错误信息。点击 “重置” 按钮,清空输入框中的内容。点击 “登录” 按钮,返回到登录界面

源码

BackFrom.cs

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 UserMessageManagementSystem

    public partial class BackForm : Form
    
        public BackForm()
        
            InitializeComponent();
            LoginForm loginForm = new LoginForm();
            loginForm.MdiParent = this;
            loginForm.Show();

        

        private void BackForm_Load(object sender, EventArgs e)
        

        
    

BackForm.Designer.cs


namespace UserMessageManagementSystem

    partial class BackForm
    
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        
            if (disposing && (components != null))
            
                components.Dispose();
            
            base.Dispose(disposing);
        

        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        
            this.SuspendLayout();
            // 
            // BackForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(482, 453);
            this.IsMdiContainer = true;
            this.Name = "BackForm";
            this.Text = "用户信息管理系统";
            this.Load += new System.EventHandler(this.BackForm_Load);
            this.ResumeLayout(false);

        

        #endregion
    

LoginForm.cs

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

namespace UserMessageManagementSystem

    public partial class LoginForm : Form
    
        public LoginForm()
        
            InitializeComponent();
        

        private void Login_Click(object sender, EventArgs e)
        
            //从用户名和密码文本框中读取字符串
            String userName = UserName.Text;
            String userPassword = UserPassword.Text;

            //如果用户没有输入用户名就点击登录按键,提示需先注册
            if (userName.Length == 0)
            
                MessageBox.Show("用户名不能为空,请输入用户名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

            
            else
            
                //如果密码框为空
                if (userPassword.Length == 0)
                
                    MessageBox.Show("请输入密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                
                else
                
                    //匹配用户名和密码的正确性,如果正确
                    if (String.Equals(userName, UserInfo.userName) && String.Equals(userPassword, UserInfo.userPassword))
                    
                        MessageForm messageForm = new MessageForm();
                        messageForm.MdiParent = this.MdiParent;
                        messageForm.Show();
                        this.Hide();
                    
                    //如果密码错误
                    else if (String.Equals(userName, UserInfo.userName) && !String.Equals(userPassword, UserInfo.userPassword))
                    
                        MessageBox.Show("密码错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    
                    //如果该用户名不存在
                    else
                    
                        MessageBox.Show("该用户不存在,请先进行注册!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    
                
            
        

        private void LRegister_Click(object sender, EventArgs e)
        
            RegisterForm registerForm = new RegisterForm();
            registerForm.MdiParent = this.MdiParent;
            registerForm.Show();
            this.Hide();
        
    

    //全局变量,初始化默认用户信息
    static class UserInfo
    
        public static String userName = "whut";
        public static String userPassword = "1234";
        public static String userSex = "男";
        public static String Major = "计算机学院";

    

LoginForm.Designer.cs


namespace UserMessageManagementSystem

    partial class LoginForm
    
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        
            if (disposing && (components != null))
            
                components.Dispose();
            
            base.Dispose(disposing);
        

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.UserName = new System.Windows.Forms.TextBox();
            this.UserPassword = new System.Windows.Forms.TextBox();
            this.Login = new System.Windows.Forms.Button();
            this.LRegister = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(36, 62);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(58, 20);
            this.label1.TabIndex = 0;
            this.label1.Text = "用户名:";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(36, 125);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(55, 20);
            this.label2.TabIndex = 1;
            this.label2.Text = "密   码:";
            // 
            // UserName
            // 
            this.UserName.Location = new System.Drawing.Point(113, 59);
            this.UserName.Name = "UserName";
            this.UserName.Size = new System.Drawing.Size(125, 27);
            this.UserName.TabIndex = 2;
            // 
            // UserPassword
            // 
            this.UserPassword.Location = new System.Drawing.Point(113, 122);
            this.UserPassword.Name = "UserPassword";
            this.UserPassword.PasswordChar = '*';
            this.UserPassword.Size = new System.Drawing.Size(125, 27);
            this.UserPassword.TabIndex = 3;
            // 
            // Login
            // 
            this.Login.Location = new System.Drawing.Point(113, 182);
            this.Login.Name = "Login";
            this.Login.Size = new System.Drawing.Size(50, 30);
            this.Login.TabIndex = 4;
            this.Login.Text = "登录";
            this.Login.UseVisualStyleBackColor = true;
            this.Login.Click += new System.EventHandler(this.Login_Click);
            // 
            // LRegister
            // 
            this.LRegister.Location = new System.Drawing.Point(188, 182);
            this.LRegister.Name = "LRegister";
            this.LRegister.Size = new System.Drawing.Size(50, 30);
            this.LRegister.TabIndex = 5;
            this.LRegister.Text = "注册";
            this.LRegister.UseVisualStyleBackColor = true;
            this.LRegister.Click += new System.EventHandler(this.LRegister_Click);
            // 
            // LoginForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(282, 253);
            this.Controls.Add(this.LRegister);
            this.Controls.Add(this.Login);
            this.Controls.Add(this.UserPassword);
            this.Controls.Add(this.UserName);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "LoginForm";
            this.Text = "欢迎登录";
            this.ResumeLayout(false);
            this.PerformLayout();

        

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox UserName;
        private System.Windows.Forms.TextBox UserPassword;
        private System.Windows.Forms.Button Login;
        private System.Windows.Forms.Button LRegister;
    

MessageForm.cs

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

namespace UserMessageManagementSystem

    public partial class MessageForm : Form
    
        public MessageForm()
        
            InitializeComponent();
        

        private void MessageForm_Load(object sender, EventArgs e)
        
            usermassage.Text = UserInfo.userName;
            userpassword.Text = UserInfo.userPassword;
            usersex.Text = UserInfo.userSex;
            useracademy.Text = UserInfo.Major;
        

        private void Register_Click(object sender, EventArgs e)
        
            RegisterForm registerForm = new RegisterForm();
            registerForm.MdiParent = this.MdiParent;
            registerForm.Show();
            this.Hide();
        

        private void Login_Click(object sender, EventArgs e)
        
            LoginForm loginForm = new LoginForm();
            loginForm.MdiParent = this.MdiParent;
            loginForm.Show();
            this.Hide();
        
    

MessageForm.Designer.cs


namespace UserMessageManagementSystem

    partial class MessageForm
    
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        
            if以上是关于[可视化编程]窗体设计及其应用的主要内容,如果未能解决你的问题,请参考以下文章

c#窗体

基本的应用类型

Visual Basic程序设计应用教程(第2版)pdf

寒假 1

Devexpress之LayoutControl的使用及其控件布局设计

✥2-GUI应用程序设计基础