c# - 如何在 C# 中将用户从登录表单重定向到另一个表单

标签 c# forms visual-studio

我是编程和使用窗口表单应用程序的新手,我有 2 个表单应用程序。第一个是 Form1,第二个是 Form2。第一个是登录表单,它工作正常,成功登录的用户会弹出一个消息,显示登录成功。当弹出此窗口并且用户单击“确定”时,我希望我的应用程序重定向到 form2,其中该应用程序仅对登录用户可用。

这是我的源代码:

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;
using MySql.Data.MySqlClient; 

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try

            {
                string myConnection = "datasource=s59.hekko.pl;port=3306;username=truex2_kuba;password=xxx";
                MySqlConnection myConn = new MySqlConnection(myConnection);
                MySqlCommand SelectCommand = new MySqlCommand("select * from truex2_kuba.users where uid='" + this.uid.Text + "' and pwd='" + this.pwd.Text + "' ;", myConn);
                MySqlDataReader myReader;
                myConn.Open();
                myReader = SelectCommand.ExecuteReader();
                int count = 0;
                while (myReader.Read())
                {
                    count = count + 1;
                }
                if (count == 1)
                {
                    MessageBox.Show("Nazwa uzytkownika i haslo sa poprawne");


                }
                else if (count > 1)
                {
                    MessageBox.Show("Wpisano zle dane uzytkownika");

                }
                else
                    MessageBox.Show("Wpisano zle dane uzytkownika");
                myConn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }
    }
}

最佳答案

您需要做的就是创建 Form2 的实例,然后调用该实例的 Show() 方法:

Form2 formName = new Form2();
formName.Show();

您可以选择隐藏登录Form1:

this.Hide();

关于c# - 如何在 C# 中将用户从登录表单重定向到另一个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37468778/

相关文章:

c# - ComboBox.SelectedIndexChanging?

c# - 没有给出对应于 Dictionary<string, string>.Add(string, string) 所需形式参数 'value' 的参数

html - Angular:找不到路径为: 'variable-> 0 -> id' 的控件

php - 如何在 1 个提交按钮上执行 2 个操作

forms - Typo3 表单显示为 ###LABEL### ###FIELD###

c++ - 尝试在 directx11 中渲染三角形后 Windows 卡住

c# - HttpWebRequest 正确的异常处理

c# - 每个 ID 查找多个日期的有效方法

visual-studio - VS2010 sp1安装报错 "The feature is on a network resource that is unavailable"

visual-studio - 从 Visual Studio 2010 中的选项卡名称中删除路径