Home > OS >  How to hide/close Current Form and opens a new form using a button inside the UserControl ? C#
How to hide/close Current Form and opens a new form using a button inside the UserControl ? C#

Time:06-21

enter image description here

code:

using System;
using System.Windows.Forms;

namespace WindowsFormsControlLibrary1
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "admin" && this.textBox2.Text == "admin")
            {
                MessageBox.Show("Login Successfully");
                UserForm dashboard = new UserForm();
                this.ParentForm.Hide();
                dashboard.ShowDialog();
                this.ParentForm.Close();
            }
            else
            {
                MessageBox.Show("Login Error");
            }
        }
    }
}

Mainform:

enter image description here

Output:

enter image description here

  • Related