Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
After the login, I want to open the main form. The main form is made of 4 buttons on the left side where each represent one user control, each click on the button should open/show usercontrol which is located on the right side of the mainform. So what I want to do after login is to show/start with the first button which is Home button, other known as HomeUserControl.
When I start this code, the login works but it opens blank form

What I have tried:

I have this login file:

<pre>
        private void ButtonLogin(object sender, EventArgs e)
        {
            DB db = new DB();

            String username = textBoxUser.Text;
            String password = textBoxPass.Text;


            DataTable table = new DataTable();

            MySqlDataAdapter adapter = new MySqlDataAdapter();

            MySqlCommand command = new MySqlCommand("SELECT * FROM `korisnik` WHERE `username` = @usn and `lozinka` = @loz", db.getConnection());

            command.Parameters.Add("@usn", MySqlDbType.VarChar).Value = username;
            command.Parameters.Add("@loz", MySqlDbType.VarChar).Value = password;

            adapter.SelectCommand = command;

            adapter.Fill(table);

            //provjera postoji li korisnik

            if(table.Rows.Count > 0)
            {
                this.Hide();
                MainForm mainForm = new MainForm();
                mainForm.Show();
            }
            else
            {
                // provjera je li polje username prazno
                if (username.Trim().Equals(""))
                {
                    MessageBox.Show("Upišite username kako bi se uspješno ulogirali", "Prazan username", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                // provjera je li polje lozinka prazno
                else if(password.Trim().Equals(""))
                {
                    MessageBox.Show("Upišite lozinku kako bi se uspješno ulogirali", "Prazna lozinka", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                //provjera jesu li oba polja prazna
                else
                {
                    MessageBox.Show("Pogrešan username ili lozinka", "Pogrešni podaci", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }
    }
}





and this is the mainform:

namespace projekt
{
    public partial class MainForm : Form
    {
        private void button1_Click(object sender, EventArgs e)
        {
            if (!panel.Controls.Contains(HomeUserControl.Instanca))
            {
                panel.Controls.Add(HomeUserControl.Instanca);
                HomeUserControl.Instanca.Dock = DockStyle.Fill;
                HomeUserControl.Instanca.BringToFront();
            }
            else
                HomeUserControl.Instanca.BringToFront();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!panel.Controls.Contains(DogsUserControl.Instanca))
            {
                panel.Controls.Add(DogsUserControl.Instanca);
                DogsUserControl.Instanca.Dock = DockStyle.Fill;
                DogsUserControl.Instanca.BringToFront();
            }
            else
                DogsUserControl.Instanca.BringToFront();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (!panel.Controls.Contains(CatsUserControl.Instanca))
            {
                panel.Controls.Add(CatsUserControl.Instanca);
                CatsUserControl.Instanca.Dock = DockStyle.Fill;
                CatsUserControl.Instanca.BringToFront();
            }
            else
                CatsUserControl.Instanca.BringToFront();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (!panel.Controls.Contains(AddUserControl_Dodaj.Instanca))
            {
                panel.Controls.Add(AddUserControl_Dodaj.Instanca);
                AddUserControl_Dodaj.Instanca.Dock = DockStyle.Fill;
                AddUserControl_Dodaj.Instanca.BringToFront();
            }
            else
                AddUserControl_Dodaj.Instanca.BringToFront();
        }
    }
}


I have been told I should make a default constructor but I'm not sure how.
Posted
Updated 12-Sep-19 0:39am
v2
Comments
Richard Deeming 13-Sep-19 12:25pm    
You appear to be storing passwords in plain text. Don't do that!

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

You can do this with Application.Run in Program.cs, example:
Application.Run(loginForm);
Application.Run(mainForm);
 
Share this answer
 
v2
All forms need a constructor which normally calls InitializeComponent - and when you create a form in the VS designer by adding a form to your project the defautl constructor is created and does exactly that.

If you don't have one - and your code doesn't show one - then MainForm will work in the designer to add buttons and so forth, as it uses the method itself from the .designer.cs file working to "draw" the controls on screen for you to edit.

So start by checking if you have a MainForm.designer.cs file, and if it contains the InitializeComponent method, If it does, you can just recreate your default constructor:
C#
public MainForm() => InitializeComponent();
Will do.
 
Share this answer
 
Comments
[no name] 12-Sep-19 6:55am    
Thank you! Solved the problem!
OriginalGriff 12-Sep-19 7:06am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900