创建登录页面,登录成功跳转测试页面。
第一步, VS创建窗体。
第二步, 进入程序,开始编辑。
第三步,从“视图”找出“工具箱”,拉出对应控件,排布登录页面。
第四步,从“视图”找出“属性窗口”
编辑控件的Text 文字改动如下 ,对应的(Name)可改可不改,但是为了便于辨认,最好改一下,方便敲代码。
第五步,程序主目录右键添加创建另外一个窗体
第六步, 参考前面步骤,编辑如下:
第七步,敲代码。
1.双击第一页面”登录“按钮,进去程序,编辑。
双击第一页面”重置“按钮,进去程序,编辑。
双击第二页面”确定“按钮,进去程序,编辑。
新建一个类,编辑页面转换代码,让主程序做简单的事的。
最后,综合全部代码如下;
登录页面:
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 WindowsFormsTest2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void buttonName_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(textName.Text) || string.IsNullOrEmpty(textPassword.Text))
{
MessageBox.Show("用户名、密码不能为空");
}
else if (textName.Text == "Admin" && textPassword.Text == "Admin")
{
MessageBox.Show("登录成功,开始转换窗体");
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
MessageBox.Show("账户、密码错误,请重置确认,再输入");
}
}
catch(Exception exp)
{
MessageBox.Show($"异常:{exp}");
}
}
private void buttonReset_Click(object sender, EventArgs e)
{
textName.Text = "";
textPassword.Text = "";
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textPassword_TextChanged(object sender, EventArgs e)
{
}
}
}
测试页面:
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 WindowsFormsTest2
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "测试完成";
}
}
}
转换类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsTest2
{
public class TestClass
{
Form1 Form1 = new Form1();
public void Test()
{
try
{
Form1.ShowDialog();
if (Form1.DialogResult==DialogResult.OK)
{
Application.Run(new MainForm());
}
else
{
return;
}
}
catch (Exception exp)
{
MessageBox.Show($"异常:{exp}");
}
}
}
}
主函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsTest2
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
TestClass Test = new TestClass();
Test.Test();
}
}
}