简体   繁体   中英

How to Redirect The User From Login Form to Another Form in c#

I am newbie to programming and using window form application I have 2 form application. First one is Form1 and second is Form2. First one is a log in form and it works fine, successfully logged in users get a pop up saying, logged in successfully. When this pops up and user clicks 'ok', I want my app to redirect to form2 where is the app only available to logged in users.

That's my source code:

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);

            }
        }
    }
}

All you need to do is create an instance of your Form2 and then call the Show() method of that instance:

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

Optionally, you can hide the login Form1 with:

this.Hide();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM