0

I have this project where you can login as a user and as an employee and if you log in as an employee certain buttons such as reports will show up on the homepage.

I have two forms named HomePage and frmSignIn. If the user signs in from frmSignIn I want certain button on HomePage to be visible. I have tried several ways but cannot get it to work.

I did make the button public to see if it works, although I know this is bad practice.

if (ValidCredentials(txtUsername.Text, txtPassword.Text) == true && checkBoxEmployee.Checked == true && txtBoxEmpPin.Text.Equals(employeePin))
{
    this.DialogResult = DialogResult.OK;
    this.Tag = _usersId;
    this.Close();
    MessageBox.Show("You have successfully logged in as an employee.");

    HomePage hp = new HomePage();

    hp.Button.Visible = true;   // !!!! Does not work !!!!
}
Yael
  • 1,566
  • 3
  • 18
  • 25

3 Answers3

1

One way to do this, is making your original homepage known to the frmSignIn.

To do this, you could create a property in your frmSignIn like

public MyHomePage Form { get; set; }

Then, when creating the SignIn form, also add the reference of the homepage to the new instance of frmSignIn. Something like:

var mySignInForm = new frmSignIn();
mySignInForm.MyHomePage = this;

Now, you have a decent reference in your frmSignIn to do whatever you want. You still would have to make the button public though.

Alternatively you could, instead of the complete form, just add a reference to the button you need in the same way.

This may not be the prettiest and most elegant solution, but it will work.

Frank
  • 431
  • 1
  • 5
  • 25
1

You can use public property where You keep reference to your button, try this example.

Form1

public partial class Form1 : Form
{



    public Form1()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 x = new Form2();
        x.Button1 = this.button1;
        x.Show();
    }
}

Form2

public partial class Form2 : Form
    {
        private Button _button1;

        public Button Button1
        {
            get { return _button1; }
            set { _button1 = value; }
        }

        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _button1.Visible = false;
        }
    }
M. Wiśnicki
  • 6,094
  • 3
  • 23
  • 28
0

Create a method for homepage which enables Button

public void EnableButton(){
   this.Button.Visible = true;
}

And Do this,

this.DialogResult = DialogResult.OK;
this.Tag = _usersId;
HomePage hp = new HomePage();  //here, creating instance of homepage
hp.EnableButton(); //This enables the required button
MessageBox.Show("You have successfully logged in as an employee.");
hp.showDialog(); //Shows HomePage
this.Hide(); //Hides login page.
Prajwal
  • 3,930
  • 5
  • 24
  • 50
  • This works but it opens another HomePage form which makes me have 2 up when I log in. I have an initial one that is up when logging in and the signin form pops up as a modal form but the first HomePage is never closed. – Mallory Rich Nov 29 '16 at 07:20
  • Yes so when I run the program there's a homepage screen that opens up. From this home page I can log in and the frmSignIn form opens. However, the homepage never closes so when I try your method it just opens another homepage. – Mallory Rich Nov 29 '16 at 07:26
  • Then don't create new instance. remove this --> `HomePage hp = new HomePage();` Also, where is the reference to that homepage instance? – Prajwal Nov 29 '16 at 07:30
  • Just call `EnableButton()` for that instance. It should work. And revert `button` to private. – Prajwal Nov 29 '16 at 07:32
  • No no i'm sorry. Maybe i'm not explaining this clearly. The program starts with the homepage. The homepage has a button called Sign In which opens frmSignIn but never closes the initial homepage form. If I sign in as an employee through frmSignIn I want the 'button' to be visible. – Mallory Rich Nov 29 '16 at 08:05