0

有没有办法让我的主表单 form1 在表单 2 加载后显示?自动加载这是表格 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");
            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());
        }
    }

我希望表格 1 在表格 2 之后自动加载 在此先感谢

4

4 回答 4

1

验证登录凭据并且正确后,您可以使用以下内容:

Hide();
Form1 mainForm = new Form1();
mainForm.Show();
于 2013-04-16T07:08:10.840 回答
0

哦,我想这就是你需要的

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");
        }
于 2013-04-16T07:04:43.380 回答
0

我怀疑您在这里追求的是一种仅在用户经过验证时才显示 form1 的方式?
如果是这样,只需在此处显示表格:

if (this.textBox1.Text == username1 && this.textBox2.Text == password1)
{
    Form1 form1 = new Form1();
    form1.Show();
    // close form2?  
}
else
 ...

您可能想要保留一个计数器,如果用户在 x 次尝试中没有正确获得密码/用户名组合,只需卸载 form2(即从 else 中)。

于 2013-04-16T07:09:12.313 回答
0

如果Form1是您的主要选项,则首先加载并显示为对话框Form可能是一个更好的选择,如果从不退出应用程序,则继续。Form1Form2DialogResultForm2OK

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;
    }
}
于 2013-04-16T07:10:24.190 回答