C# 加载c语言中给定的两个主窗体
有什么方法可以让我的主表单(即表单1)在加载表单2后显示吗?我创建了两个表单表单表单1是主表单,表单2是登录表单,当用户具有正确的信用时,表单1应自动加载 这是表格2代码C# 加载c语言中给定的两个主窗体,c#,C#,有什么方法可以让我的主表单(即表单1)在加载表单2后显示吗?我创建了两个表单表单表单1是主表单,表单2是登录表单,当用户具有正确的信用时,表单1应自动加载 这是表格2代码 private void button1_Click(object sender, EventArgs e) { string username1 = "Richard"; string password1 = "Peugeot"; i
private void button1_Click(object sender, EventArgs e)
{
string username1 = "Richard";
string password1 = "Peugeot";
if (this.textBox1.Text == username1 && this.textBox2.Text == password1)
MessageBox.Show("Welcome Richard!", "Welcome");
else
MessageBox.Show("Incorrect username or password", "Bad credentials");
}
这是form1代码
private void Form1_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder(String.Empty);
sb.AppendLine("Operation System Information");
sb.AppendLine("----------------------------");
sb.AppendLine(String.Format("Name = {0}", OSVersionInfo.Name));
sb.AppendLine(String.Format("Edition = {0}", OSVersionInfo.Edition));
if (OSVersionInfo.ServicePack!=string.Empty)
sb.AppendLine(String.Format("Service Pack = {0}", OSVersionInfo.ServicePack));
else
sb.AppendLine("Service Pack = None");
sb.AppendLine(String.Format("Version = {0}", OSVersionInfo.VersionString));
sb.AppendLine(String.Format("ProcessorBits = {0}", OSVersionInfo.ProcessorBits));
sb.AppendLine(String.Format("OSBits = {0}", OSVersionInfo.OSBits));
sb.AppendLine(String.Format("ProgramBits = {0}", OSVersionInfo.ProgramBits));
textBox1.Text = sb.ToString();
}
这是program.cs代码
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form2());
}
}
静态类程序
{
///
///应用程序的主要入口点。
///
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form2());
}
}
我希望表格1在表格2之后自动加载。提前谢谢。哦,我想这就是你需要的
private void button1_Click(object sender, EventArgs e)
{
string username1 = "Richard";
string password1 = "Peugeot";
if (this.textBox1.Text == username1 && this.textBox2.Text == password1)
{
MessageBox.Show("Welcome Richard!", "Welcome");
Form1 frm = new Form1();
frm.Show();
this.Hide(); //If you want to hide the log in form after form1 has loaded
}
else
MessageBox.Show("Incorrect username or password", "Bad credentials");
}
验证登录凭据并确认其正确后,您可以使用以下各项:
Hide();
Form1 mainForm = new Form1();
mainForm.Show();
我怀疑您想要的是一种仅在用户经过验证时才显示form1的方式?
如果是,请在此处显示表格:
if (this.textBox1.Text == username1 && this.textBox2.Text == password1)
{
Form1 form1 = new Form1();
form1.Show();
// close form2?
}
else
...
您可能希望保留一个计数器,如果用户在x次尝试中没有正确获得密码/用户名组合,只需卸载form2(即从else中) 如果
Form1
是您的主要表单
,最好先加载Form1
并将Form2
显示为对话框,如果Form2
的对话框结果
不是OK
退出应用程序,否则继续
public Form1()
{
if (new Form2().ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
Close();
}
InitializeComponent();
}
Form2按钮事件
private void button1_Click(object sender, EventArgs e)
{
string username1 = "Richard";
string password1 = "Peugeot";
if (this.textBox1.Text == username1 && this.textBox2.Text == password1)
{
MessageBox.Show("Welcome Richard!", "Welcome");
DialogResult = System.Windows.Forms.DialogResult.OK;
}
else
{
MessageBox.Show("Incorrect username or password", "Bad credentials");
DialogResult = System.Windows.Forms.DialogResult.No;
}
}
新表单1().Show()
?我不明白你的问题哦哦哦我明白了让我试试