0

I'm struggling with passing data from child form "Prihlaseni" to parent form "Skladovka".

In Skladovka, I have created a public void named onUserLogin, which is called on userLogin event and should reveal some hidden buttons etc... But when I try to call it from "Prihlaseni" it only says CS0120 "An object reference is required for the nonstatic field, method, or property Skladovka.onUserLogin(bool)"

Skladovka.cs

public void onUserLogin(bool userLogged)
{
    //Check if user is logged
    if (userLogged == true)
    {
        iconLogin.Text = "Admin [Jan Dvořák]";
        OpenChildForm(new Forms.Sklad());
        iconSklad.Visible = true;
        iconButton1.Visible = true;
        iconButton2.Visible = true;
        iconButton3.Visible = true;
        homeBtn.Visible = true;
    }
    else
    {
        OpenChildForm(new Forms.Prihlaseni());
        ActivateButton(iconLogin, RGBColors.color5);
        lblTitleChildForm.Text = "Přihlášení";
        iconSklad.Visible = false;
        iconButton1.Visible = false;
        iconButton2.Visible = false;
        iconButton3.Visible = false;
        homeBtn.Visible = false;
    }
}

Prihlaseni.cs

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

namespace E_Skladovka.cz.Forms
{
    public partial class Prihlaseni : Form
    {
        public Prihlaseni()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            MySqlConnection conn = new MySqlConnection(@"SERVER=localhost;DATABASE=sklad;UID=root;PASSWORD=;");
            conn.Open();
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM users WHERE username = '" + usernameInput.Text + "' AND password ='" + passwordInput.Text + "'", conn);
            MySqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                MessageBox.Show("Přihlášení proběhlo úspěšně", "E-Skladovka.cz - Přihlášení", MessageBoxButtons.OK, MessageBoxIcon.Information);              
                Skladovka.onUserLogin(true);
            }
            else
            {
                MessageBox.Show("Jméno nebo heslo se neshodují!", "E-Skladovka.cz - Přihlášení", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            usernameInput.Text = String.Empty;
            passwordInput.Text = String.Empty;
            reader.Close();
            cmd.Dispose();
            conn.Close();
        }
    }
}

Both forms are on the same Namespace (same project).

dvorja97
  • 23
  • 2
  • Duplicate of many posts. - When opening f2 you can pass a refeence to f1 in the constructor and store it. now make all data you want to share internal. done. [examples](https://stackoverflow.com/questions/22728201/two-picturebox-in-two-different-forms/22729125?s=1|33.5023#22729125) – TaW Feb 05 '22 at 14:35
  • @JohnG, thanks a lot. It worked well. – dvorja97 Feb 05 '22 at 14:57

0 Answers0